go內(nèi)存緩存BigCache使用入門詳解
一、介紹
bigcache是一個內(nèi)存緩存系統(tǒng),用于存儲鍵值對數(shù)據(jù)。沒有g(shù)c操作。使用的時候需要序列化(反)。
bigcache的源代碼在 https://github.com/allegro/bigcache幾個特征,存儲通過[]byte,沒有過期時間。
二、安裝
我們安裝最新的v3版本
go get -u github.com/allegro/bigcache/v3
安裝完成后,我們就可以在go語言中使用bigcache了。下面是一些簡單的示例。
三、使用示例
func TestSetGet(t *testing.T) {
// new一個bigCache對象
cache, _ := bigcache.New(context.Background(), bigcache.DefaultConfig(10*time.Minute))
// get獲取一個無值的key
vNil, err := cache.Get("key")
t.Log(vNil, err) // [] Entry not found 值為空的[]字節(jié)slice
// set 存儲數(shù)據(jù)
cache.Set("key", []byte("value"))
// get 獲取數(shù)據(jù)
v, _ := cache.Get("key")
t.Log(v) // 輸出 [118 97 108 117 101]
t.Log(string(v)) // 輸出 value
}我們看一下 Set和Get方法的源代碼
// Set saves entry under the key
func (c *BigCache) Set(key string, entry []byte) error {
hashedKey := c.hash.Sum64(key)
shard := c.getShard(hashedKey)
return shard.set(key, hashedKey, entry)
}
// Get reads entry for the key.
// It returns an ErrEntryNotFound when
// no entry exists for the given key.
func (c *BigCache) Get(key string) ([]byte, error) {
hashedKey := c.hash.Sum64(key)
shard := c.getShard(hashedKey)
return shard.get(key, hashedKey)
}在Set()方法,存儲值為 []byte 字節(jié)slice類型,所以我們保存的時候,需要序列化數(shù)據(jù)成[]byte。
而在Get()方法,獲取的值為[]byte,我們此時需要反序列化[]byte成原來的類型。
3、1 string類型的 存儲與獲取以及修改
string類型可以用[]byte和類型,互相的方便轉(zhuǎn)換。
func TestSetGet(t *testing.T) {
// new一個bigCache對象
cache, _ := bigcache.New(context.Background(), bigcache.DefaultConfig(10*time.Minute))
// get獲取一個無值的key
vNil, err := cache.Get("key")
t.Log(vNil, err) // [] Entry not found 值為空的[]字節(jié)slice
// set 存儲數(shù)據(jù)
cache.Set("key", []byte("value"))
// get 獲取數(shù)據(jù)
v, _ := cache.Get("key")
t.Log(v) // 輸出 [118 97 108 117 101]
t.Log(string(v)) // 輸出 value
}3、2 非string類型的 存儲與獲取以及修改
非string類型,轉(zhuǎn)成[]byte,比較復(fù)雜,在go內(nèi)置的庫中,唯有json庫,提供了這樣的方法??梢园逊莝tring的任意類型,轉(zhuǎn)成[]byte,如下。
func Marshal(v any) ([]byte, error) // 序列化 func Unmarshal(data []byte, v any) error // 反序列化
在這里我們也使用json的序列化(反)方法,來和[]byte互轉(zhuǎn)。
3、2、1 slice切片類型的存儲與獲取以及修改
// TestSetGetSlice slice類型
func TestSetGetSlice(t *testing.T) {
// new一個bigCache對象
cache, _ := bigcache.New(context.Background(), bigcache.DefaultConfig(10*time.Minute))
// slice 存儲數(shù)據(jù)
s := []string{"1", "2"}
bs, _ := json.Marshal(s)
cache.Set("s", bs)
// get獲取值
bsv, _ := cache.Get("s")
var s2 []string = make([]string, 0)
_ = json.Unmarshal(bsv, &s2)
t.Log(s2)
// 修改數(shù)據(jù),修改s2,查看是否會影響內(nèi)存中的值
// 答案是不影響
s2 = append(s2, "3")
t.Log(s2)
var s3 []string = make([]string, 0)
_ = json.Unmarshal(bsv, &s3)
t.Log(s3)
}3、2、2 struct結(jié)構(gòu)體類型的存儲與獲取以及修改
// TestSetGetStruct 結(jié)構(gòu)體指針
func TestSetGetStruct(t *testing.T) {
// new一個bigCache對象
cache, _ := bigcache.New(context.Background(), bigcache.DefaultConfig(10*time.Minute))
// 結(jié)構(gòu)體struct 存儲數(shù)據(jù)
type st struct {
Value string
}
s := &st{
Value: "v1",
}
bs, _ := json.Marshal(s)
cache.Set("s", bs)
// get獲取值
bsv, _ := cache.Get("s")
var s2 st
_ = json.Unmarshal(bsv, &s2)
t.Log(s2) // {v1}
// 修改數(shù)據(jù),修改s2,查看是否會影響內(nèi)存中的值
// 答案是不影響
s2.Value = "v2"
t.Log(s2) // {v2}
var s3 st
_ = json.Unmarshal(bsv, &s3)
t.Log(s3) // {v1}
}結(jié)論
bigcache可以安全使用,修改獲取后的是,也不影響原來內(nèi)存中的值。
以上就是go內(nèi)存緩存BigCache使用入門詳解的詳細(xì)內(nèi)容,更多關(guān)于go內(nèi)存緩存BigCache的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Go關(guān)鍵字defer的使用和底層實現(xiàn)
defer是Go語言的關(guān)鍵字,一般用于資源的釋放和異常的捕捉,defer語句后將其后面跟隨的語句進(jìn)行延遲處理,就是說在函數(shù)執(zhí)行完畢后再執(zhí)行調(diào)用,也就是return的ret指令之前,本文給大家介紹了Go關(guān)鍵字defer的使用和底層實現(xiàn),需要的朋友可以參考下2023-11-11
GO語言判斷一個網(wǎng)段是否屬于另一個網(wǎng)段的子網(wǎng)
這篇文章主要介紹了GO語言判斷一個網(wǎng)段是否屬于另一個網(wǎng)段的子網(wǎng)的相關(guān)資料,內(nèi)容介紹詳細(xì),具有一定的參考價值,需要的朋友可任意參考一下2022-03-03
GoLang string與strings.Builder使用對比詳解
這篇文章主要介紹了GoLang string與strings.Builder使用對比,Builder 用于使用 Write 方法有效地構(gòu)建字符串。它最大限度地減少了內(nèi)存復(fù)制。零值可以使用了。不要復(fù)制非零生成器2023-03-03

