go內(nèi)存緩存如何new一個(gè)bigcache對(duì)象示例詳解
一、下載源碼
在github上,地址https://github.com/allegro/bigcache,我們可以把代碼源碼clone到本地。
這里選擇分支v3.1.0的代碼。

二、源碼目錄
我們打開源碼
. ├── LICENSE ├── README.md ├── assert_test.go ├── bigcache.go ├── bigcache_bench_test.go ├── bigcache_test.go ├── bytes.go ├── bytes_appengine.go ├── clock.go ├── config.go ├── encoding.go ├── encoding_test.go ├── entry_not_found_error.go ├── examples_test.go ├── fnv.go ├── fnv_bench_test.go ├── fnv_test.go ├── go.mod ├── go.sum ├── hash.go ├── hash_test.go ├── iterator.go ├── iterator_test.go ├── logger.go ├── queue │?? ├── bytes_queue.go │?? └── bytes_queue_test.go ├── server │?? ├── README.md │?? ├── cache_handlers.go │?? ├── middleware.go │?? ├── middleware_test.go │?? ├── server.go │?? ├── server_test.go │?? └── stats_handler.go ├── shard.go ├── stats.go └── utils.go 2 directories, 36 files
比較重要的幾個(gè)文件
queue目錄
shard.go 分片
fnv.go Fnv hash算法
bigcache.go 初始化new結(jié)構(gòu)體,以及set,get方法。
我們可以看上篇文檔的示例:
func TestSetGet(t *testing.T) {
// new一個(gè)bigCache對(duì)象
cache, _ := bigcache.New(context.Background(), bigcache.DefaultConfig(10*time.Minute))
// get獲取一個(gè)無(wú)值的key
vNil, err := cache.Get("key")
t.Log(vNil, err) // [] Entry not found 值為空的[]字節(jié)slice
// set 存儲(chǔ)數(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
}那么我們先找到這個(gè),第一行對(duì)應(yīng)的
cache, _ := bigcache.New(context.Background(), bigcache.DefaultConfig(10*time.Minute))
在源碼的代碼如下:
// New initialize new instance of BigCache
// New用來(lái)初始化一個(gè)新的BigCache實(shí)例。
func New(ctx context.Context, config Config) (*BigCache, error) {
return newBigCache(ctx, config, &systemClock{})
}我們?cè)谑褂玫臅r(shí)候,主要又涉及到一個(gè) bigcache.DefaultConfig(10*time.Minute))我們看一下他的方法:
// DefaultConfig initializes config with default values.
// DefaultConfig 初始化Config,給定默認(rèn)值。
// When load for BigCache can be predicted in advance then it is better to use custom config.
// 當(dāng)load加載 BigCache 的時(shí)候,如果在使用之前就能預(yù)估到使用量,然后預(yù)設(shè)置config,它好于使用默認(rèn)config。
func DefaultConfig(eviction time.Duration) Config {
return Config{
Shards: 1024,
LifeWindow: eviction,
CleanWindow: 1 * time.Second,
MaxEntriesInWindow: 1000 * 10 * 60,
MaxEntrySize: 500,
StatsEnabled: false,
Verbose: true,
Hasher: newDefaultHasher(),
HardMaxCacheSize: 0,
Logger: DefaultLogger(),
}
}設(shè)計(jì)一個(gè)Config結(jié)構(gòu)體
// Config for BigCache
type Config struct {
// Number of cache shards, value must be a power of two
Shards int
// Time after which entry can be evicted
// LifeWindow后,緩存對(duì)象被認(rèn)為不活躍,但并不會(huì)刪除對(duì)象
LifeWindow time.Duration
// Interval between removing expired entries (clean up).
// If set to <= 0 then no action is performed. Setting to < 1 second is counterproductive — bigcache has a one second resolution.
// CleanWindow后,會(huì)刪除被認(rèn)為不活躍的對(duì)象(超過(guò)LifeWindow時(shí)間的對(duì)象),0代表不操作;
CleanWindow time.Duration
// Max number of entries in life window. Used only to calculate initial size for cache shards.
// When proper value is set then additional memory allocation does not occur.
// 設(shè)置最大存儲(chǔ)對(duì)象數(shù)量,僅在初始化時(shí)可以設(shè)置
MaxEntriesInWindow int
// Max size of entry in bytes. Used only to calculate initial size for cache shards.
// 緩存對(duì)象的最大字節(jié)數(shù),僅在初始化時(shí)可以設(shè)置
MaxEntrySize int
// StatsEnabled if true calculate the number of times a cached resource was requested.
StatsEnabled bool
// Verbose mode prints information about new memory allocation
// 是否打印內(nèi)存分配信息
Verbose bool
// Hasher used to map between string keys and unsigned 64bit integers, by default fnv64 hashing is used.
Hasher Hasher
// HardMaxCacheSize is a limit for BytesQueue size in MB.
// It can protect application from consuming all available memory on machine, therefore from running OOM Killer.
// Default value is 0 which means unlimited size. When the limit is higher than 0 and reached then
// the oldest entries are overridden for the new ones. The max memory consumption will be bigger than
// HardMaxCacheSize due to Shards' s additional memory. Every Shard consumes additional memory for map of keys
// and statistics (map[uint64]uint32) the size of this map is equal to number of entries in
// cache ~ 2×(64+32)×n bits + overhead or map itself.
// 設(shè)置緩存最大值(單位為MB),0表示無(wú)限制
HardMaxCacheSize int
// OnRemove is a callback fired when the oldest entry is removed because of its expiration time or no space left
// for the new entry, or because delete was called.
// Default value is nil which means no callback and it prevents from unwrapping the oldest entry.
// ignored if OnRemoveWithMetadata is specified.
// 在緩存過(guò)期或者被刪除時(shí),可設(shè)置回調(diào)函數(shù),參數(shù)是(key、val),默認(rèn)是nil不設(shè)置
OnRemove func(key string, entry []byte)
// OnRemoveWithMetadata is a callback fired when the oldest entry is removed because of its expiration time or no space left
// for the new entry, or because delete was called. A structure representing details about that specific entry.
// Default value is nil which means no callback and it prevents from unwrapping the oldest entry.
OnRemoveWithMetadata func(key string, entry []byte, keyMetadata Metadata)
// OnRemoveWithReason is a callback fired when the oldest entry is removed because of its expiration time or no space left
// for the new entry, or because delete was called. A constant representing the reason will be passed through.
// Default value is nil which means no callback and it prevents from unwrapping the oldest entry.
// Ignored if OnRemove is specified.
// 在緩存過(guò)期或者被刪除時(shí),可設(shè)置回調(diào)函數(shù),參數(shù)是(key、val,reason)默認(rèn)是nil不設(shè)置
OnRemoveWithReason func(key string, entry []byte, reason RemoveReason)
onRemoveFilter int
// Logger is a logging interface and used in combination with `Verbose`
// Defaults to `DefaultLogger()`
Logger Logger
}BigCache的實(shí)現(xiàn)是 一種索引和數(shù)據(jù)分離的多階hash。
主索引(多階hash數(shù)組)的value保存的是在數(shù)據(jù)段的位置,通過(guò)二次定位拿到某個(gè)key對(duì)應(yīng)的真實(shí)的value。
以上就是go內(nèi)存緩存如何new一個(gè)bigcache對(duì)象示例詳解的詳細(xì)內(nèi)容,更多關(guān)于go new bigcache對(duì)象的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Golang 實(shí)現(xiàn) RTP音視頻傳輸示例詳解
這篇文章主要為大家介紹了Golang實(shí)現(xiàn)RTP音視頻傳輸?shù)氖纠斀猓行枰呐笥芽梢越梃b參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
golang并發(fā)執(zhí)行的幾種方式小結(jié)
本文主要介紹了golang并發(fā)執(zhí)行的幾種方式小結(jié),主要包括了Channel,WaitGroup ,Context,使用這三種機(jī)制中的一種或者多種可以達(dá)到并發(fā)控制很好的效果,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
golang 實(shí)用庫(kù)gotable的具體使用
使用gotable框架以實(shí)現(xiàn)在CLI命令行界面中打印表格。本文就介紹一下golang 實(shí)用庫(kù)gotable的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
Go語(yǔ)言利用接口實(shí)現(xiàn)鏈表插入功能詳解
這篇文章主要為大家介紹了Go語(yǔ)言中的接口,以及如何利用接口實(shí)現(xiàn)鏈表插入功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-04-04
Go語(yǔ)言連接Oracle數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Go語(yǔ)言連接Oracle數(shù)據(jù)庫(kù)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
詳解Go語(yǔ)言如何判斷兩個(gè)對(duì)象是否相等
在編程中,判斷兩個(gè)對(duì)象是否相等是一項(xiàng)常見的任務(wù),同時(shí)判斷對(duì)象是否相等在很多情況下都非常重要,所以在接下來(lái)的內(nèi)容中,我們將詳細(xì)介紹在?Go?語(yǔ)言中如何判斷對(duì)象是否相等的方法和技巧,需要的可以參考一下2023-06-06
Go實(shí)現(xiàn)數(shù)據(jù)脫敏的方案設(shè)計(jì)
在一些常見的業(yè)務(wù)場(chǎng)景中可能涉及到用戶的手機(jī)號(hào),銀行卡號(hào)等敏感數(shù)據(jù),對(duì)于這部分的數(shù)據(jù)經(jīng)常需要進(jìn)行數(shù)據(jù)脫敏處理,就是將此部分?jǐn)?shù)據(jù)隱私化,防止數(shù)據(jù)泄露,所以本文給大家介紹了Go實(shí)現(xiàn)數(shù)據(jù)脫敏的方案設(shè)計(jì),需要的朋友可以參考下2024-05-05

