最近更新时间:2024.01.15 16:20:06
首次发布时间:2024.01.15 16:20:06
本文档介绍如何在火山引擎缓存数据库 Redis 版的 Golang 客户端中集成 dns-stale-cache 插件。
不同版本的 go-redis SDK 的集成方式不同。参见以下示例代码。
import ( "fmt" "time" "github.com/go-redis/redis" . "github.com/volcengine/dns-stale-cache/common" . "github.com/volcengine/dns-stale-cache/redis/v6" ) func ExampleClient() { opt := &redis.Options{ Addr: "localhost:6379", } opt.Dialer = NewDialerWithCache(opt, WithCacheFirst(true), WithIPConsistance(true), WithDnsTimeout(2*time.Second), ) rdb := redis.NewClient(opt) err := rdb.Set("key1", "value1", 0).Err() if err != nil { panic(err) } val, err := rdb.Get("key1").Result() if err != nil { panic(err) } fmt.Println("key", val) val2, err := rdb.Get("key2").Result() if err == redis.Nil { fmt.Println("key2 does not exist") } else if err != nil { panic(err) } else { fmt.Println("key2", val2) } // Output: key value // key2 does not exist }
import ( "fmt" "time" "github.com/go-redis/redis/v7" . "github.com/volcengine/dns-stale-cache/common" . "github.com/volcengine/dns-stale-cache/redis/v7" ) func ExampleClient() { opt := &redis.Options{ Addr: "localhost:6379", } opt.Dialer = NewDialerWithCache(opt, WithCacheFirst(true), WithIPConsistance(true), WithDnsTimeout(2*time.Second), ) rdb := redis.NewClient(opt) err := rdb.Set("key1", "value1", 0).Err() if err != nil { panic(err) } val, err := rdb.Get("key1").Result() if err != nil { panic(err) } fmt.Println("key", val) val2, err := rdb.Get("key2").Result() if err == redis.Nil { fmt.Println("key2 does not exist") } else if err != nil { panic(err) } else { fmt.Println("key2", val2) } // Output: key value // key2 does not exist }
import ( "context" "fmt" "time" "github.com/go-redis/redis/v8" . "github.com/volcengine/dns-stale-cache/common" . "github.com/volcengine/dns-stale-cache/redis/v8" ) var ctx = context.Background() func ExampleClient() { opt := &redis.Options{ Addr: "localhost:6379", } opt.Dialer = NewDialerWithCache(opt, WithCacheFirst(true), WithIPConsistance(true), WithDnsTimeout(2*time.Second), ) rdb := redis.NewClient(opt) err := rdb.Set(ctx, "key1", "value1", 0).Err() if err != nil { panic(err) } val, err := rdb.Get(ctx, "key1").Result() if err != nil { panic(err) } fmt.Println("key", val) val2, err := rdb.Get(ctx, "key2").Result() if err == redis.Nil { fmt.Println("key2 does not exist") } else if err != nil { panic(err) } else { fmt.Println("key2", val2) } // Output: key value // key2 does not exist }
import ( "context" "fmt" "time" "github.com/redis/go-redis/v9" . "github.com/volcengine/dns-stale-cache/common" . "github.com/volcengine/dns-stale-cache/redis/v9" ) var ctx = context.Background() func ExampleClient() { opt := &redis.Options{ Addr: "localhost:6379", } opt.Dialer = NewDialerWithCache(opt, WithCacheFirst(true), WithIPConsistance(true), WithDnsTimeout(2*time.Second), ) rdb := redis.NewClient(opt) err := rdb.Set(ctx, "key1", "value1", 0).Err() if err != nil { panic(err) } val, err := rdb.Get(ctx, "key1").Result() if err != nil { panic(err) } fmt.Println("key", val) val2, err := rdb.Get(ctx, "key2").Result() if err == redis.Nil { fmt.Println("key2 does not exist") } else if err != nil { panic(err) } else { fmt.Println("key2", val2) } // Output: key value // key2 does not exist }
func NewDialerWithCache(opt *redis.Options, cacheOpts ...Option)
参数名称 | 类型 | 是否必选 | 说明 |
---|---|---|---|
opt | *redis.Options | 是 | Redis client 配置选项。参见 Options。 |
cacheOpts | Option | 否 | 缓存配置选项。参见 缓存配置选项。 |
参数 | 说明 |
---|---|
func WithCacheFirst(preferUse bool) Option | 是否优先返回缓存中匹配的查询结果。
|
func WithIPConsistance(preferUse bool) Option | 是否把在内存中缓存的查询结果保存到文件。
|
func WithDnsTimeout(timeout time.Duration) Option | 向 DNS 服务器发起 DNS 查询请求的超时时间。如果在超时前没有获取查询结果,会使用缓存中匹配的查询结果。默认值为 1 s。 |
类型 | 说明 |
---|---|
func | 带有缓存功能的 Dialer 函数。不同版本的 go-redis SDK 返回的函数不同:
|