使用Go Cache Fetcher客户端进行缓存提取
我制作了一个支持常见的用Redis或者memcache进行缓存的模式的Go客户端库。就是这个。
アクセスしたい情報や変数からキー名を生成する
if (Redisにデータがある) {
RedisからGetする
} else {
DatabaseからGetしたり、APIからfetchする
RedisへSetする
}
假设在Redis中使用,如果实现了Client,就可以用于任何类型的缓存。
通过传递函数,可以对其进行缓存,因此可以用于数据库或API访问。
还可以将数据序列化后进行设置和获取。
请参阅README和测试代码以了解详细使用方法。
进行缓存获取的客户端
这是一个用Golang编写的缓存提取器。
安装
go get github.com/peutes/cachefetcher
✅ 支持 Cash Fetcher 客户端的基本逻辑
你可以使用Redis、Memcached或其他缓存系统来缓存各种功能的响应。
例如,当首次从功能中获取响应时,可以将其保存在Redis中,并获取功能的响应,
然后,如果有缓存,可以从Redis中获取。
从要素中自动生成缓存键。
キャッシュキーの生成ロジックについて考える必要はありません。
キー生成ロジックはメインロジックから隠蔽できます。
✅ 简洁的现金控制
缓存控制非常简单,只需要设置键并获取提取器函数。
这个Fetcher客户端只需要使用SetKey和Fetch函数就足够了。Fetch函数中需要设置Fetcher函数、获取值的指针以及缓存的有效期限。
-
- SetKey()
- Fetch()
安装
go get github.com/peutes/go-cache-fetcher-client
用法 fǎ)
fetcher := cachefetcher.NewCacheFetcher(
&ClientImpl{
Rdb: redis.NewClient(&redis.Options{Addr: "localhost:6379"}),
},
nil
)
fetcher.SetKey([]string{"prefix", "str"}, "hoge")
// fetcher.Key() == "prefix_str_hoge"
// フェッチャー関数。たとえば、DBから読み込みを想定します。
read := func(s string) string {
return s + " fetch!!"
}
// 最初のフェッチはフェッチャー関数から呼ばれます。キャッシュは呼ばれません。
var dst string
err := fetcher.Fetch(10*time.Second, &dst, func() (string, error) {
return read("first"), nil
})
// dst == "first fetch!!" <- get from function
// 2回目のフェチは有効期限内のキャッシュから呼ばれます。フェッチャー関数からは呼ばれません。
err = fetcher.Fetch(10*time.Second, &dst, func() (string, error) {
return read("second"), nil
})
// dst == "first fetch!!" <- get from cache
被支持的类型
在中国只需要一个选择将以下内容用中文本地化:
关键要素包括 string 以外的 int、float、bool、complex、byte、time、slice、array 和有 String() 方法的 struct。
该客户端支持通过 gob serializer 进行序列化。
序列化后的字符串将被保存在缓存中。
fetcher.SetKey([]string{"prefix", "any"}, 1, 0.1, true, &[]string{"a", "b"}, time.Unix(0, 0).In(time.UTC))
_ = fetcher.Key() // "prefix_any_1_0.1_true_a_b_1970-01-01_00:00:00_+0000_UTC"
_ = fetcher.HashKey() // "prefix_any_c94a415eb6e20585f4fbc856b6edcf52007259522967c4bea548515e71531663"
read := func() ([]int, error) {
return []int{1, 2, 3, 4, 5}
}
var dst []int
err := fetcher.Fetch(10*time.Second, &dst, read)
// dst == []int{1, 2, 3, 4, 5}
要素可以支持除了字符串类型之外的其他类型。
如果希望使用interface{}或自定义类型,可以使用GobRegister()进行注册。
i := 10
b := true
s := "abc"
ft := 0.123
i8 := int8(20)
i64 := int64(30)
ui8 := uint8(40)
ui64 := uint64(50)
e := &testStruct{
I: i,
B: b,
S: s,
F: ft,
I8: i8,
I64: i64,
UI8: ui8,
UI64: ui64,
IP: &i,
BP: &b,
SP: &s,
FP: &ft,
I8P: &i8,
I64P: &i64,
UI8P: &ui8,
UI64P: &ui64,
IS: []int{i, i, i},
BS: []bool{b, b, b},
SS: []string{s, s, s},
FS: []float64{ft, ft, ft},
IM: map[int]int{1: i, 2: i, 3: i},
BM: map[bool]bool{true: b, false: b},
SM: map[string]string{"a": s, "bb": s, "ccc": s},
FM: map[float64]float64{0.1: ft, 0.2: ft, 0.3: ft},
}
var dst testStruct
f := cachefetcher.NewCacheFetcher(redisClient, options)
err := fetcher.SetKey([]string{"prefix", "key"}, "struct1")
err = fetcher.Set(e, 10*time.Second)
err = fetcher.Get(&dst)
其他缓存控制
如果需要散列键,可以使用 SetHashKey 而不是 SetKey。
每个元素都可以使用Set()、Get()和Del()方法。
如果需要获取键,则使用Key()方法。
如果需要以布尔值的形式获取缓存结果是否存在,则使用IsCached()方法。
-
- SetHashKey()
-
- Set()
-
- Get()
-
- SetString()
-
- GetString()
-
- Del()
-
- Key()
- IsCached()
– 注册魔法玉()
缓存客户端的实现
这个缓存获取器客户端需要实现缓存客户端,但是我们提供了一个简单的Redis客户端,这样你可以使用这个客户端来运行,而不需要自己实现。
// SimpleRedisClientImpl はサンプルクライアントの実装です。
type SimpleRedisClientImpl struct {
Rdb *redis.Client
}
// サンプルクライアントの `Set` 実装です。
func (i *SimpleRedisClientImpl) Set(key string, value interface{}, expiration time.Duration) error {
// You need an implementation to set from the cache.
return i.Rdb.Set(ctx, key, value, expiration).Err()
}
// サンプルクライアントの `Get` 実装です。
func (i *SimpleRedisClientImpl) Get(key string, dst interface{}) error {
// You need an implementation to get from the cache.
v, err := i.Rdb.Get(ctx, key).Result()
if err != nil {
return err
}
reflect.ValueOf(dst).Elem().SetString(v)
return nil
}
// サンプルクライアントの `Del` 実装です。
func (i *SimpleRedisClientImpl) Del(key string) error {
return i.Rdb.Del(ctx, key).Err()
}
// サンプルクライアントの `IsErrCacheMiss` 実装です。
// キャッシュミスエラーの判定を返してください。
func (i *SimpleRedisClientImpl) IsErrCacheMiss(err error) bool {
return errors.Is(err, redis.Nil)
}
请提供以下的中文本地化的备选项:选项
这个Fetcher Client可以使用设置中的Single Flight。
如果启用 DebugPrintMode,键将会在终端上输出。
cachefetcher.Options{
Group: &singleflight.Group{}, // default
GroupTimeout: 30 * time.Second, // default
DebugPrintMode: true, // default is false
})