Skip to content

Commit

Permalink
cache增加带Context版本,开放平台相关接口支持Context版本 (silenceper#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
okhowang authored Apr 3, 2023
1 parent 01784c2 commit 07b7dc4
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 43 deletions.
46 changes: 45 additions & 1 deletion cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cache

import "time"
import (
"context"
"time"
)

// Cache interface
type Cache interface {
Expand All @@ -9,3 +12,44 @@ type Cache interface {
IsExist(key string) bool
Delete(key string) error
}

// ContextCache interface
type ContextCache interface {
Cache
GetContext(ctx context.Context, key string) interface{}
SetContext(ctx context.Context, key string, val interface{}, timeout time.Duration) error
IsExistContext(ctx context.Context, key string) bool
DeleteContext(ctx context.Context, key string) error
}

// GetContext get value from cache
func GetContext(ctx context.Context, cache Cache, key string) interface{} {
if cache, ok := cache.(ContextCache); ok {
return cache.GetContext(ctx, key)
}
return cache.Get(key)
}

// SetContext set value to cache
func SetContext(ctx context.Context, cache Cache, key string, val interface{}, timeout time.Duration) error {
if cache, ok := cache.(ContextCache); ok {
return cache.SetContext(ctx, key, val, timeout)
}
return cache.Set(key, val, timeout)
}

// IsExistContext check value exists in cache.
func IsExistContext(ctx context.Context, cache Cache, key string) bool {
if cache, ok := cache.(ContextCache); ok {
return cache.IsExistContext(ctx, key)
}
return cache.IsExist(key)
}

// DeleteContext delete value in cache.
func DeleteContext(ctx context.Context, cache Cache, key string) error {
if cache, ok := cache.(ContextCache); ok {
return cache.DeleteContext(ctx, key)
}
return cache.Delete(key)
}
28 changes: 24 additions & 4 deletions cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ func (r *Redis) SetRedisCtx(ctx context.Context) {

// Get 获取一个值
func (r *Redis) Get(key string) interface{} {
result, err := r.conn.Do(r.ctx, "GET", key).Result()
return r.GetContext(r.ctx, key)
}

// GetContext 获取一个值
func (r *Redis) GetContext(ctx context.Context, key string) interface{} {
result, err := r.conn.Do(ctx, "GET", key).Result()
if err != nil {
return nil
}
Expand All @@ -56,17 +61,32 @@ func (r *Redis) Get(key string) interface{} {

// Set 设置一个值
func (r *Redis) Set(key string, val interface{}, timeout time.Duration) error {
return r.conn.SetEX(r.ctx, key, val, timeout).Err()
return r.SetContext(r.ctx, key, val, timeout)
}

// SetContext 设置一个值
func (r *Redis) SetContext(ctx context.Context, key string, val interface{}, timeout time.Duration) error {
return r.conn.SetEX(ctx, key, val, timeout).Err()
}

// IsExist 判断key是否存在
func (r *Redis) IsExist(key string) bool {
result, _ := r.conn.Exists(r.ctx, key).Result()
return r.IsExistContext(r.ctx, key)
}

// IsExistContext 判断key是否存在
func (r *Redis) IsExistContext(ctx context.Context, key string) bool {
result, _ := r.conn.Exists(ctx, key).Result()

return result > 0
}

// Delete 删除
func (r *Redis) Delete(key string) error {
return r.conn.Del(r.ctx, key).Err()
return r.DeleteContext(r.ctx, key)
}

// DeleteContext 删除
func (r *Redis) DeleteContext(ctx context.Context, key string) error {
return r.conn.Del(ctx, key).Err()
}
10 changes: 8 additions & 2 deletions cache/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ import (
"context"
"testing"
"time"

"github.com/alicebob/miniredis/v2"
)

func TestRedis(t *testing.T) {
server, err := miniredis.Run()
if err != nil {
t.Error("miniredis.Run Error", err)
}
t.Cleanup(server.Close)
var (
timeoutDuration = time.Second
ctx = context.Background()
opts = &RedisOpts{
Host: "127.0.0.1:6379",
Host: server.Addr(),
}
redis = NewRedis(ctx, opts)
err error
val = "silenceper"
key = "username"
)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/silenceper/wechat/v2
go 1.16

require (
github.com/alicebob/miniredis/v2 v2.30.0
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d
github.com/fatih/structs v1.1.0
github.com/go-redis/redis/v8 v8.11.5
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk=
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
github.com/alicebob/miniredis/v2 v2.30.0 h1:uA3uhDbCxfO9+DI/DuGeAMr9qI+noVWwGPNTFuKID5M=
github.com/alicebob/miniredis/v2 v2.30.0/go.mod h1:84TWKZlxYkfgMucPBf5SOQBYJceZeQRFIaQgNMiCX6Q=
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d h1:pVrfxiGfwelyab6n21ZBkbkmbevaf+WvMIiR7sr97hw=
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
Expand Down Expand Up @@ -71,6 +75,8 @@ github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JT
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64 h1:5mLPGnFdSsevFRFc9q3yYbBkB6tsm4aCwwQV/j1JQAQ=
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand All @@ -89,6 +95,7 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
Loading

0 comments on commit 07b7dc4

Please sign in to comment.