在 Go 中使用接口進(jìn)行靈活緩存
緩存是編程中一種常見的技術(shù),通過存儲(chǔ)昂貴的計(jì)算或 IO 結(jié)果來快速查找,從而提高性能。在本篇文章中,我們將了解 Go 的接口如何幫助構(gòu)建靈活、可擴(kuò)展的緩存。
定義緩存接口
首先,讓我們定義一個(gè)接口,指定緩存功能:
type Cache interface {
Get(key string) interface{}
Set(key string, value interface{})
}
緩存接口有兩個(gè)方法:Get 用于按鍵查找緩存值,Set 用于存儲(chǔ)鍵值對(duì)。
通過定義接口,我們將緩存的使用與特定的實(shí)現(xiàn)分離開來。任何實(shí)現(xiàn)了這些方法的緩存庫(kù)都滿足接口的要求。
簡(jiǎn)單的內(nèi)存緩存
讓我們實(shí)現(xiàn)一個(gè)符合接口的簡(jiǎn)單內(nèi)存緩存:
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 在內(nèi)存中存儲(chǔ)條目,并且使用 sync.Mutex 來避免并發(fā)寫的發(fā)生。它實(shí)現(xiàn)了 Get 和 Set 方法來管理映射中的條目。
使用緩存
現(xiàn)在我們可以輕松使用緩存了:
mc := NewMemoryCache()
mc.Set("hello", "world")
mc.Get("hello") // world
通過該接口,我們可以調(diào)用 Set 和 Get,而不必?fù)?dān)心實(shí)現(xiàn)問題。
其他緩存實(shí)現(xiàn)
現(xiàn)在,假設(shè)我們想使用 Redis 而不是內(nèi)存緩存。我們可以創(chuàng)建一個(gè)實(shí)現(xiàn)相同接口的 RedisCache:
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
客戶端代碼保持不變。這就體現(xiàn)了接口的靈活性。
基于接口的緩存的好處
- 解耦 - 客戶端代碼無(wú)需與特定的緩存庫(kù)相耦合。
- 可維護(hù)性--無(wú)需修改客戶端代碼即可更改緩存實(shí)現(xiàn)。
- 可測(cè)試性--可對(duì)緩存進(jìn)行存根測(cè)試或模擬測(cè)試。
- 可重用性--通用緩存接口允許編寫可重用的緩存邏輯。
加料
這里我們看到上面的代碼,有兩個(gè)緩存器,也都實(shí)現(xiàn)了 Set 和 Get 方法,但是我們初始化的時(shí)候是初始化一個(gè)真正的對(duì)象:InMemoryCache 和 RedisCache 。實(shí)際上我們可以定義一個(gè) cache 接口:
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")
}
這樣當(dāng)我們又有其他緩存器需求時(shí),我們實(shí)際上無(wú)需再更改客戶端的代碼,只需要增加 cache 的實(shí)現(xiàn)即可。這樣改造之后,我們的客戶端調(diào)用就可以變成這樣:
func main() {
c, err := NewCache("")
if err != nil {
log.Fatalln(err)
}
c.Set("hello", "world")
c.Get("hello")
}
我們使用的對(duì)象并不是真正的緩存器對(duì)象,而是 cache 接口,而 InMemoryCache 和 RedisCache 都實(shí)現(xiàn)了 cache 接口,所以我們調(diào)用 Set 和 Get 方法的時(shí)候,實(shí)際上是對(duì)應(yīng)到緩存器真正的實(shí)現(xiàn)。
最后
Go 中的接口有助于構(gòu)建靈活的庫(kù)和應(yīng)用程序。定義簡(jiǎn)單的接口使代碼更整潔:
- 模塊化 - 可以插入不同的實(shí)現(xiàn)。
- 可擴(kuò)展 - 可以不間斷地添加新的實(shí)現(xiàn)。
- 可維護(hù) - 組件可以互換,便于維護(hù)。
- 可測(cè)試 - 可對(duì)組件單獨(dú)的單元測(cè)試。
通過以最小的開銷提供強(qiáng)大的抽象,接口在 Golang 中對(duì)于創(chuàng)建松散耦合和可擴(kuò)展的系統(tǒng)非常重要。