Configuration
ClientConfig 用于配置 CursorClient 的行为。
结构定义
go
type ClientConfig struct {
// 启用缓存
EnableCache bool
// 缓存过期时间
CacheTTL time.Duration
// 日志级别
LogLevel LogLevel
// 自定义存储路径(可选)
StoragePath string
// 只读模式(默认为 true)
ReadOnly bool
}配置选项
EnableCache
启用或禁用缓存功能。
类型: bool
默认值: false
go
config := &cursor.ClientConfig{
EnableCache: true,
}CacheTTL
缓存数据的过期时间。
类型: time.Duration
默认值: 5 * time.Minute
go
config := &cursor.ClientConfig{
EnableCache: true,
CacheTTL: 10 * time.Minute,
}LogLevel
日志输出级别。
类型: LogLevel
默认值: LogLevelInfo
可选值:
LogLevelDebug- 调试信息LogLevelInfo- 一般信息LogLevelWarn- 警告信息LogLevelError- 错误信息LogLevelNone- 不输出日志
go
config := &cursor.ClientConfig{
LogLevel: cursor.LogLevelDebug,
}StoragePath
自定义 Cursor 存储路径。如果不设置,SDK 会自动检测。
类型: string
默认值: 自动检测
go
config := &cursor.ClientConfig{
StoragePath: "/custom/path/to/cursor/storage",
}ReadOnly
只读模式。SDK 始终以只读模式运行,不会修改 Cursor 数据。
类型: bool
默认值: true
go
config := &cursor.ClientConfig{
ReadOnly: true,
}配置示例
默认配置
go
// 使用默认配置
client, err := cursor.NewCursorClient(nil)
if err != nil {
log.Fatal(err)
}
defer client.Close()开发环境配置
go
config := &cursor.ClientConfig{
EnableCache: false, // 禁用缓存以获取最新数据
LogLevel: cursor.LogLevelDebug, // 详细日志
}
client, err := cursor.NewCursorClient(config)
if err != nil {
log.Fatal(err)
}
defer client.Close()生产环境配置
go
config := &cursor.ClientConfig{
EnableCache: true, // 启用缓存提高性能
CacheTTL: 15 * time.Minute, // 较长的缓存时间
LogLevel: cursor.LogLevelWarn, // 只记录警告和错误
}
client, err := cursor.NewCursorClient(config)
if err != nil {
log.Fatal(err)
}
defer client.Close()高性能配置
go
config := &cursor.ClientConfig{
EnableCache: true,
CacheTTL: 30 * time.Minute, // 更长的缓存时间
LogLevel: cursor.LogLevelError, // 最少的日志输出
}
client, err := cursor.NewCursorClient(config)
if err != nil {
log.Fatal(err)
}
defer client.Close()自定义路径配置
go
config := &cursor.ClientConfig{
StoragePath: "/path/to/cursor/storage",
EnableCache: true,
CacheTTL: 10 * time.Minute,
}
client, err := cursor.NewCursorClient(config)
if err != nil {
log.Fatal(err)
}
defer client.Close()配置最佳实践
1. 根据使用场景选择缓存策略
go
// 实时数据监控:禁用缓存
config := &cursor.ClientConfig{
EnableCache: false,
}
// 数据分析:启用缓存
config := &cursor.ClientConfig{
EnableCache: true,
CacheTTL: 15 * time.Minute,
}2. 合理设置日志级别
go
// 开发调试
config := &cursor.ClientConfig{
LogLevel: cursor.LogLevelDebug,
}
// 生产环境
config := &cursor.ClientConfig{
LogLevel: cursor.LogLevelWarn,
}3. 使用环境变量
go
func getConfig() *cursor.ClientConfig {
config := &cursor.ClientConfig{
EnableCache: os.Getenv("ENABLE_CACHE") == "true",
LogLevel: getLogLevel(os.Getenv("LOG_LEVEL")),
}
if ttl := os.Getenv("CACHE_TTL"); ttl != "" {
if duration, err := time.ParseDuration(ttl); err == nil {
config.CacheTTL = duration
}
}
if path := os.Getenv("CURSOR_STORAGE_PATH"); path != "" {
config.StoragePath = path
}
return config
}
func getLogLevel(level string) cursor.LogLevel {
switch level {
case "debug":
return cursor.LogLevelDebug
case "info":
return cursor.LogLevelInfo
case "warn":
return cursor.LogLevelWarn
case "error":
return cursor.LogLevelError
case "none":
return cursor.LogLevelNone
default:
return cursor.LogLevelInfo
}
}配置验证
SDK 会自动验证配置:
go
config := &cursor.ClientConfig{
CacheTTL: -1 * time.Minute, // 无效的 TTL
}
client, err := cursor.NewCursorClient(config)
if err != nil {
// 处理配置错误
log.Fatal(err)
}运行时配置更新
某些配置可以在运行时更新:
go
// 更新日志级别
client.SetLogLevel(cursor.LogLevelDebug)
// 清除缓存
client.Cache().Clear()
// 更新缓存 TTL
client.Cache().SetTTL(20 * time.Minute)相关文档
- CursorClient - 客户端 API
- Cache - 缓存管理
- 性能优化 - 性能优化指南
- 入门指南 - 快速开始