go内存缓存如何new一个bigcache对象示例详解

 更新时间:2023年09月05日 15:51:18   作者:海生  
这篇文章主要为大家介绍了go内存缓存如何new一个bigcache对象示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

一、下载源码

在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

比较重要的几个文件

queue目录

shard.go 分片

fnv.go Fnv hash算法

bigcache.go 初始化new结构体,以及set,get方法。

我们可以看上篇文档的示例:

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 值为空的[]字节slice
    // set 存储数据
    cache.Set("key", []byte("value"))
    // get 获取数据
    v, _ := cache.Get("key")
    t.Log(v)         // 输出 [118 97 108 117 101]
    t.Log(string(v)) // 输出 value
}

那么我们先找到这个,第一行对应的

cache, _ := bigcache.New(context.Background(), bigcache.DefaultConfig(10*time.Minute))

在源码的代码如下:

// New initialize new instance of BigCache
// New用来初始化一个新的BigCache实例。
func New(ctx context.Context, config Config) (*BigCache, error) {
    return newBigCache(ctx, config, &systemClock{})
}

我们在使用的时候,主要又涉及到一个 bigcache.DefaultConfig(10*time.Minute))我们看一下他的方法:

// DefaultConfig initializes config with default values.
// DefaultConfig 初始化Config,给定默认值。
// When load for BigCache can be predicted in advance then it is better to use custom config.
// 当load加载 BigCache 的时候,如果在使用之前就能预估到使用量,然后预设置config,它好于使用默认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(),
    }
}

设计一个Config结构体

// 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后,缓存对象被认为不活跃,但并不会删除对象
    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后,会删除被认为不活跃的对象(超过LifeWindow时间的对象),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.
    // 设置最大存储对象数量,仅在初始化时可以设置
    MaxEntriesInWindow int
    // Max size of entry in bytes. Used only to calculate initial size for cache shards.
    // 缓存对象的最大字节数,仅在初始化时可以设置
    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
    // 是否打印内存分配信息
    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.
    // 设置缓存最大值(单位为MB),0表示无限制
    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.
    // 在缓存过期或者被删除时,可设置回调函数,参数是(key、val),默认是nil不设置
    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.
    // 在缓存过期或者被删除时,可设置回调函数,参数是(key、val,reason)默认是nil不设置
    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的实现是 一种索引和数据分离的多阶hash。

主索引(多阶hash数组)的value保存的是在数据段的位置,通过二次定位拿到某个key对应的真实的value。

以上就是go内存缓存如何new一个bigcache对象示例详解的详细内容,更多关于go new bigcache对象的资料请关注脚本之家其它相关文章!

相关文章

  • Go语言 go程释放操作(退出/销毁)

    Go语言 go程释放操作(退出/销毁)

    这篇文章主要介绍了Go语言 go程释放操作(退出/销毁),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • Golang Type关键字的使用

    Golang Type关键字的使用

    Type关键字在Go语言中作用很重要,比如定义结构体,接口,还可以自定义类型,定义类型别名等,具有一定的参考价值,感兴趣的可以了解一下
    2023-11-11
  • Golang中的sync.WaitGroup用法实例

    Golang中的sync.WaitGroup用法实例

    这篇文章主要介绍了Golang中的sync.WaitGroup用法实例,WaitGroup的用途,它能够一直等到所有的goroutine执行完成,并且阻塞主线程的执行,直到所有的goroutine执行完成,需要的朋友可以参考下
    2015-07-07
  • Golang中使用Date进行日期格式化(沿用Java风格)

    Golang中使用Date进行日期格式化(沿用Java风格)

    这篇文章主要介绍了Golang中使用Date进行日期格式化,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • 深入了解Golang中Slice切片的使用

    深入了解Golang中Slice切片的使用

    本文主要为大家详细介绍了Golang中Slice切片的使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2023-02-02
  • Go操作redis与redigo的示例解析

    Go操作redis与redigo的示例解析

    这篇文章主要为大家介绍了Go操作redis与redigo的示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2022-04-04
  • Go语言开发redis封装及简单使用详解

    Go语言开发redis封装及简单使用详解

    这篇文章主要为大家介绍了Go语言开发redis的封装及简单使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2021-11-11
  • 用go gin server来做文件上传服务

    用go gin server来做文件上传服务

    今天小编就为大家分享一篇关于用go gin server来做文件上传服务,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-04-04
  • golang beego框架环境搭建过程

    golang beego框架环境搭建过程

    这篇文章主要为大家介绍了golang beego框架环境搭建的过程脚本,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2022-04-04
  • go语言编程之美自定义二进制文件实用指南

    go语言编程之美自定义二进制文件实用指南

    这篇文章主要介绍了go语言编程之美自定义二进制文件实用指南
    2023-12-12

最新评论