You need to enable JavaScript to run this app.
TrafficRoute DNS 套件

TrafficRoute DNS 套件

复制全文
集成 DNS 高可用插件
为缓存数据库 Redis 版集成 DNS 高可用插件
复制全文
为缓存数据库 Redis 版集成 DNS 高可用插件

本文档介绍如何在火山引擎缓存数据库 Redis 版的 Golang 客户端中集成 dns-stale-cache 插件。

前提条件

  • 您的 Golang 必须是 1.18 或更高版本。
  • 您的 Redis 客户端使用的 go-redis SDK 必须是 v6 、v7、v8 或 v9 版本。dns-stale-cache 不支持其他版本的 go-redis SDK。

把 dns-stale-cache 插件集成到 SDK 工程

不同版本的 go-redis SDK 的集成方式不同。参见以下示例代码。

go-redis SDK v6

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
}

go-redis SDK v7

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
}

go-redis SDK v8

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
}

go-redis SDK v9

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
}

API 参考

func NewDialerWithCache

func NewDialerWithCache(opt *redis.Options, cacheOpts ...Option)

输入参数

参数名称类型是否必选说明
opt*redis.OptionsRedis client 配置选项。参见 Options
cacheOptsOption缓存配置选项。参见 缓存配置选项


缓存配置选项

参数说明
func WithCacheFirst(preferUse bool) Option是否优先返回缓存中匹配的查询结果。
  • true:域名解析时,优先返回缓存中匹配的查询结果,同时异步向 DNS 服务器发起 DNS 查询请求,并使用查询结果刷新缓存。
  • false:(默认)域名解析时,优先从 DNS 服务器获取查询结果。如果获取失败,再使用缓存中匹配的查询结果。
func WithIPConsistance(preferUse bool) Option是否把在内存中缓存的查询结果保存到文件。
  • false:(默认)不把缓存的查询结果保存到文件。
  • true:把缓存的查询结果保存到文件。默认路径是 /home/ip_info
func WithDnsTimeout(timeout time.Duration) Option向 DNS 服务器发起 DNS 查询请求的超时时间。如果在超时前没有获取查询结果,会使用缓存中匹配的查询结果。默认值为 1 s。

返回参数

类型说明
func带有缓存功能的 Dialer 函数。不同版本的 go-redis SDK 返回的函数不同:
  • v7、v8、v9func(ctx context.Context, network, addr string) (net.Conn, error)
  • v6func() (net.Conn, error)
最近更新时间:2024.01.15 16:20:06
这个页面对您有帮助吗?
有用
有用
无用
无用