跳到主要内容
版本: valkey_v0.x.x

Coherence

使用 https://github.com/oracle/coherence-go-client 的 Coherence 存储驱动程序。

目录

签名

func New(config ...Config) (*Storage, error)
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) Reset() error
func (s *Storage) Close() error
func (s *Storage) Conn() *Session

安装

Go 1.19 及以上版本支持 Coherence

安装 coherence 实现

go get github.com/gofiber/storage/coherence

在运行或测试此实现之前,您必须确保 Coherence 集群可用。对于本地开发,我们建议使用 Coherence CE Docker 镜像;它包含客户端正常运行所需的一切。

要使用 Docker 启动 Coherence 集群,请执行以下命令

docker run -d -p 1408:1408 ghcr.io/oracle/coherence-ce:24.09

请参阅此处关于创建 Coherence 会话时的连接选项文档。

示例

导入存储包。

import "github.com/gofiber/storage/coherence"

您可以使用以下方式创建存储

// Initialize default config, to connect to localhost:1408 using plain text
store, err := coherence.New()

// Initialize custom config to connect to a different host/port and use plain text and expiry of 5 minutes.
store, err := coherence.New(coherence.Config{
Address: "my-host:myport",
Expiration: time.Duration(300) * time.Second, // 5 minutes
})

// Initialize to connect with TLS enabled with your own tls.Config
tlsConfig := config := &tls.Config{...}

store, err := coherence.New(coherence.Config{
Address: "my-host:myport",
TLSConfig: tlsConfig,
})

注意:如果您使用 coherence.New() 创建两个存储,它们实际上是相同的。如果您希望有两个单独的存储,则可以使用

store1, err := coherence.New(Config{ScopeName: "scope1"})
store2, err := coherence.New(Config{ScopeName: "scope2"})

近端缓存

最新版本的 Coherence Go 客户端引入了近端缓存支持,用于在 Go 客户端中缓存频繁访问的数据,以避免通过网络发送请求。

如果您通过 LBR 使用粘性会话,这将特别有用,因为它将在 Go 进程中缓存会话,并且 Get() 操作会快得多。

当会话在服务器上过期时,它将自动从近端缓存中移除。

要为您的会话启用此功能,您可以将 NearCacheTimeout 设置为小于过期时间的持续时间。

// Initialize default config, to connect to localhost:1408 using plain text
store, err := coherence.New()

// Use plain text with default expiry of 5 minutes, and a near cache expiry of 2 minutes
store, err := coherence.New(coherence.Config{
Address: "my-host:myport",
Expiration: time.Duration(300) * time.Second, // 5 minutes
NearCacheTimeout: time.Duration(120) * time.Second, // 2 minutes
})

注意:您必须确保您的近端缓存超时时间小于会话超时时间。

配置

// Config defines configuration options for Coherence connection.
type Config struct {
// Address to connect to, defaults to "localhost:1408"
Address string

// Timeout is the default session timeout to connect to Coherence, defaults to 30s
Timeout time.Duration

// ScopeName defines a scope allowing for multiple storage sessions
ScopeName string

// Reset indicates if the store should be reset after being created
Reset bool

// TLSConfig specifies tls.Config to use when connecting, if nil then plain text is used
TLSConfig *tls.Config

// NearCacheTimeout defines the timeout for a near cache. Is this is set, then a near cache
// with the timeout is created. Note: this must be less than the session timeout or any timeout you specify
// when using Set().
NearCacheTimeout time.Duration
}

默认配置

var DefaultConfig = Config{
Address: "localhost:1408",
Timeout: time.Duration(120) * time.Seconds,
ScopeName: defaultScopeName,
Reset: false,
NearCacheTimeout: time.Duration(60) * time.Seconds,
}