W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
緩存組件同時(shí)提供了?gcache
?的?Redis
?緩存適配實(shí)現(xiàn)。?Redis
?緩存在多節(jié)點(diǎn)保證緩存的數(shù)據(jù)一致性時(shí)非常有用,特別是?Session
?共享、數(shù)據(jù)庫(kù)查詢緩存等場(chǎng)景中。
func ExampleCache_SetAdapter() {
var (
err error
ctx = gctx.New()
cache = gcache.New()
redisConfig = &gredis.Config{
Address: "127.0.0.1:6379",
Db: 9,
}
cacheKey = `key`
cacheValue = `value`
)
// Create redis client object.
redis, err := gredis.New(redisConfig)
if err != nil {
panic(err)
}
// Create redis cache adapter and set it to cache object.
cache.SetAdapter(gcache.NewAdapterRedis(redis))
// Set and Get using cache object.
err = cache.Set(ctx, cacheKey, cacheValue, time.Second)
if err != nil {
panic(err)
}
fmt.Println(cache.MustGet(ctx, cacheKey).String())
// Get using redis client.
fmt.Println(redis.MustDo(ctx, "GET", cacheKey).String())
// Output:
// value
// value
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: