闽公网安备 35020302035485号
type Cache interface {
Get(key string) interface{}
Set(key string, value interface{})
}
缓存接口有两个方法:Get 用于按键查找缓存值,Set 用于存储键值对。通过定义接口,我们将缓存的使用与特定的实现分离开来。任何实现了这些方法的缓存库都满足接口的要求。type InMemoryCache struct {
m sync.Mutex
store map[string]interface{}
}
func NewMemoryCache() *InMemoryCache {
return &InMemoryCache{
m: sync.Mutex{},
store: make(map[string]interface{}),
}
}
func (c *InMemoryCache) Get(key string) interface{} {
return c.store[key]
}
func (c *InMemoryCache) Set(key string, value interface{}) {
c.m.Lock()
defer c.m.Unlock()
c.store[key] = value
}
InMemoryCache 使用 map 在内存中存储条目,并且使用 sync.Mutex 来避免并发写的发生。它实现了 Get 和 Set 方法来管理映射中的条目。mc := NewMemoryCache()
mc.Set("hello", "world")
mc.Get("hello") // world
通过该接口,我们可以调用 Set 和 Get,而不必担心实现问题。type RedisCache struct {
client *redis.Client
}
func NewRedisCache() *RedisCache {
c := &RedisCache{client: redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})}
return c
}
func (c *RedisCache) Get(key string) interface{} {
ctx := context.Background()
return c.client.Get(ctx, key)
}
func (c *RedisCache) Set(key string, value interface{}) {
ctx := context.Background()
c.client.Set(ctx, key, value, -1)
}
使用方式:rc := NewRedisCache()
rc.Set("hello", "world")
rc.Get("hello") // world
客户端代码保持不变。这就体现了接口的灵活性。可重用性--通用缓存接口允许编写可重用的缓存逻辑。
type cache interface {
Set(key string, value interface{})
Get(key string) interface{}
}
func DefaultCache() cache {
return NewMemoryCache()
}
func NewCache(tp string) (cache, error) {
switch tp {
case "redis":
return NewRedisCache(), nil
default:
return DefaultCache(), nil
}
return nil, errors.New("can not found target cache")
}
这样当我们又有其他缓存器需求时,我们实际上无需再更改客户端的代码,只需要增加 cache 的实现即可。这样改造之后,我们的客户端调用就可以变成这样:func main() {
c, err := NewCache("")
if err != nil {
log.Fatalln(err)
}
c.Set("hello", "world")
c.Get("hello")
}
我们使用的对象并不是真正的缓存器对象,而是 cache 接口,而 InMemoryCache 和 RedisCache 都实现了 cache 接口,所以我们调用 Set 和 Get 方法的时候,实际上是对应到缓存器真正的实现。