自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在 Go 中使用接口進(jìn)行靈活緩存

開發(fā) 后端
Go 中的接口有助于構(gòu)建靈活的庫(kù)和應(yīng)用程序。在本篇文章中,我們將了解 Go 的接口如何幫助構(gòu)建靈活、可擴(kuò)展的緩存。

緩存是編程中一種常見的技術(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)非常重要。

責(zé)任編輯:趙寧寧 來源: 愛發(fā)白日夢(mèng)的后端
相關(guān)推薦

2021-11-29 22:59:34

Go Dockertest集成

2023-11-27 19:39:46

Goprotobuf

2024-12-11 08:41:18

2009-03-20 13:40:15

JDK線程并發(fā)

2024-02-04 00:00:00

Go貨幣接口

2011-12-08 10:24:53

JavaNIO

2024-02-07 11:44:20

NestJSRxJS異步編程

2023-10-23 20:03:02

Go緩存

2023-05-24 16:41:41

React前端

2019-12-12 13:50:27

strace追蹤系統(tǒng)調(diào)用Linux

2024-05-06 13:34:28

WireGoogleGo

2021-02-20 09:14:35

PythonPygal可視化

2021-09-21 15:44:02

LinuxOBSWayland

2021-01-05 08:39:51

容器前端流水線

2024-08-19 01:10:00

RedisGo代碼

2022-11-03 20:38:01

CMD命令Go

2022-01-03 08:06:15

函數(shù)Go數(shù)據(jù)

2023-03-30 07:48:46

接口鑒權(quán)SpringBoot

2010-06-23 15:21:45

SharepointExchange

2023-12-31 16:29:31

GoGoroutinesChannels
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)