最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

go內(nèi)存緩存如何new一個(gè)bigcache對(duì)象示例詳解

 更新時(shí)間:2023年09月05日 15:51:18   作者:海生  
這篇文章主要為大家介紹了go內(nèi)存緩存如何new一個(gè)bigcache對(duì)象示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

一、下載源碼

在github上,地址https://github.com/allegro/bigcache,我們可以把代碼源碼clone到本地。

這里選擇分支v3.1.0的代碼。

二、源碼目錄

我們打開(kāi)源碼

.
├── 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基礎(chǔ)之函數(shù)使用(參數(shù)傳值)實(shí)例詳解

    Golang基礎(chǔ)之函數(shù)使用(參數(shù)傳值)實(shí)例詳解

    這篇文章主要為大家介紹了Golang基礎(chǔ)之函數(shù)使用(參數(shù)傳值)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Go使用path/filepath包處理文件路徑的完全指南

    Go使用path/filepath包處理文件路徑的完全指南

    本文詳細(xì)介紹了Go語(yǔ)言中path/filepath包的使用,涵蓋了路徑處理、拼接、規(guī)范化、遍歷和匹配等功能,適用于文件系統(tǒng)操作的各種場(chǎng)景,需要的朋友可以參考下
    2026-03-03
  • 使用Go開(kāi)發(fā)硬件驅(qū)動(dòng)程序的流程步驟

    使用Go開(kāi)發(fā)硬件驅(qū)動(dòng)程序的流程步驟

    Golang是一種簡(jiǎn)潔、高效的編程語(yǔ)言,它的強(qiáng)大并發(fā)性能和豐富的標(biāo)準(zhǔn)庫(kù)使得它成為了開(kāi)發(fā)硬件驅(qū)動(dòng)的理想選擇,在本文中,我們將探討如何使用Golang開(kāi)發(fā)硬件驅(qū)動(dòng)程序,并提供一個(gè)實(shí)例來(lái)幫助你入門(mén),需要的朋友可以參考下
    2023-11-11
  • 淺析Golang中變量與常量的聲明與使用

    淺析Golang中變量與常量的聲明與使用

    變量、常量的聲明與使用是掌握一門(mén)編程語(yǔ)言的基礎(chǔ),這篇文章主要為大家詳細(xì)介紹了Golang中變量與常量的聲明與使用,需要的可以參考一下
    2023-04-04
  • Go語(yǔ)言截取字符串函數(shù)用法

    Go語(yǔ)言截取字符串函數(shù)用法

    這篇文章主要介紹了Go語(yǔ)言截取字符串函數(shù)用法,實(shí)例分析了Go語(yǔ)言操作字符串的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-02-02
  • GoLang實(shí)現(xiàn)日志收集器流程講解

    GoLang實(shí)現(xiàn)日志收集器流程講解

    這篇文章主要介紹了GoLang實(shí)現(xiàn)日志收集器流程,看日志是開(kāi)發(fā)者平時(shí)排查BUG所必須的掌握的技能,但是日志冗雜,所以寫(xiě)個(gè)小工具來(lái)收集這些日志幫助我們排查BUG,感興趣想要詳細(xì)了解可以參考下文
    2023-05-05
  • Go語(yǔ)言中常用語(yǔ)法編寫(xiě)與優(yōu)化技巧小結(jié)

    Go語(yǔ)言中常用語(yǔ)法編寫(xiě)與優(yōu)化技巧小結(jié)

    為了充分利用?Go?的潛力,我們需要了解如何優(yōu)化?Go?程序,本文將介紹一些常見(jiàn)的?Go?語(yǔ)言?xún)?yōu)化技巧,并通過(guò)實(shí)際例子進(jìn)行說(shuō)明,希望對(duì)大家有所幫助
    2024-02-02
  • Go語(yǔ)言中select使用詳解

    Go語(yǔ)言中select使用詳解

    這篇文章主要介紹了Go語(yǔ)言中select使用的相關(guān)資料,select是Go語(yǔ)言中用于多路channel操作的控制結(jié)構(gòu),可以監(jiān)聽(tīng)多個(gè)channel的發(fā)送與接收操作,當(dāng)其中某一個(gè)可以進(jìn)行時(shí)就執(zhí)行對(duì)應(yīng)的語(yǔ)句,從而實(shí)現(xiàn)非阻塞并發(fā)通信,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-05-05
  • Golang使用gorm實(shí)現(xiàn)分頁(yè)功能的示例代碼

    Golang使用gorm實(shí)現(xiàn)分頁(yè)功能的示例代碼

    在提供列表接口時(shí)一般要用到分頁(yè),對(duì)于存儲(chǔ)在某些數(shù)據(jù)庫(kù)中的數(shù)據(jù)進(jìn)行分頁(yè)起來(lái)非常的方便,下文給出一個(gè)通過(guò)gorm進(jìn)行分頁(yè)并通過(guò)http返回?cái)?shù)據(jù)的例子,感興趣的小伙幫跟著小編一起來(lái)看看吧
    2024-10-10
  • Go語(yǔ)言中strings.HasPrefix、strings.Split、strings.SplitN()?函數(shù)

    Go語(yǔ)言中strings.HasPrefix、strings.Split、strings.SplitN()?函數(shù)

    本文主要介紹了Go語(yǔ)言中strings.HasPrefix、strings.Split、strings.SplitN()函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08

最新評(píng)論

河源市| 云浮市| 彩票| 诸暨市| 温州市| 察哈| 工布江达县| 澳门| 全州县| 岳普湖县| 平罗县| 平罗县| 花垣县| 连山| 招远市| 阿克苏市| 四会市| 敦煌市| 苏尼特左旗| 宜章县| 灵山县| 云南省| 甘泉县| 五峰| 东光县| 望奎县| 灵川县| 团风县| 渝中区| 宽城| 扶绥县| 塘沽区| 普兰县| 德庆县| 洪雅县| 漳平市| 乌海市| 富平县| 鸡西市| 武清区| 江北区|