Overview
Ristretto是一种快速的,固定大小的内存中高速缓存,同时关注吞吐量和命中率性能。您可以轻松地将Ristretto添加到现有系统,并将最有价值的数据保留在需要的位置。
该软件包包括接纳/驱逐元数据所需的多个概率数据结构。多数是计数布隆过滤器的变化,但是还需要一种特定于缓存的功能,即“新鲜”机制,该机制基本上是一个“生命周期”过程。TinyLFU的原始论文中描述了这种新鲜度机制,但其他机制可能更适合某些数据分布。
type Cache
1
2
3
4
5
6
|
type Cache struct {
// Metrics contains a running log of important statistics like hits, misses,
// and dropped items.
Metrics *Metrics
// contains filtered or unexported fields
}
|
高速缓存是具有TinyLFU允许策略和Sampled LFU逐出策略的哈希映射的线程安全实现。您可以根据需要在多个goroutine中使用相同的Cache实例。
func NewCache
1
|
func NewCache(config *Config) (*Cache, error)
|
NewCache返回一个新的Cache实例和任何配置错误(如果有)。
func (*Cache) Clear
1
|
func (c *Cache) Clear()
|
清除清空哈希表,并将所有策略计数器归零。请注意,这不是原子操作(但这应该不成问题,因为假定直到此之后才进行Set / Get调用)。
func (*Cache) Close
1
|
func (c *Cache) Close()
|
关闭将停止所有goroutine,并关闭所有channel。
func (*Cache) Del
1
|
func (c *Cache) Del(key interface{})
|
Del从缓存中删除键值项(如果存在)。
func (*Cache) Get
1
|
func (c *Cache) Get(key interface{}) (interface{}, bool)
|
Get返回值(如果有)和一个布尔值,表示是否找到该值。值可以为nil,布尔值可以同时为true。
func (*Cache) Set
1
|
func (c *Cache) Set(key, value interface{}, cost int64) bool
|
set尝试将键值项添加到缓存中。如果返回false,则删除Set,并且不将键值项添加到缓存中。如果返回true,则如果确定键值项不值得保留,则该策略仍有可能被丢弃,但否则将添加该项,并逐出其他项以腾出空间。
要使用Config.Coster函数动态评估物料成本,请将cost参数设置为0,然后在需要时运行Coster,以找到物料的真实成本。
func (*Cache) SetWithTTL
1
|
func (c *Cache) SetWithTTL(key, value interface{}, cost int64, ttl time.Duration) bool
|
SetWithTTL的工作方式与Set相似,但是将键值对添加到缓存中,该键值对将在经过指定的TTL(生存时间)后过期。零值表示该值永不过期,与调用Set相同。负值是无操作,该值将被丢弃。
type Config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
type Config struct {
// NumCounters determines the number of counters (keys) to keep that hold
// access frequency information. It's generally a good idea to have more
// counters than the max cache capacity, as this will improve eviction
// accuracy and subsequent hit ratios.
//
// For example, if you expect your cache to hold 1,000,000 items when full,
// NumCounters should be 10,000,000 (10x). Each counter takes up 4 bits, so
// keeping 10,000,000 counters would require 5MB of memory.
NumCounters int64
// MaxCost can be considered as the cache capacity, in whatever units you
// choose to use.
//
// For example, if you want the cache to have a max capacity of 100MB, you
// would set MaxCost to 100,000,000 and pass an item's number of bytes as
// the `cost` parameter for calls to Set. If new items are accepted, the
// eviction process will take care of making room for the new item and not
// overflowing the MaxCost value.
MaxCost int64
// BufferItems determines the size of Get buffers.
//
// Unless you have a rare use case, using `64` as the BufferItems value
// results in good performance.
BufferItems int64
// Metrics determines whether cache statistics are kept during the cache's
// lifetime. There *is* some overhead to keeping statistics, so you should
// only set this flag to true when testing or throughput performance isn't a
// major factor.
Metrics bool
// OnEvict is called for every eviction and passes the hashed key, value,
// and cost to the function.
OnEvict func(key, conflict uint64, value interface{}, cost int64)
// KeyToHash function is used to customize the key hashing algorithm.
// Each key will be hashed using the provided function. If keyToHash value
// is not set, the default keyToHash function is used.
KeyToHash func(key interface{}) (uint64, uint64)
// Cost evaluates a value and outputs a corresponding cost. This function
// is ran after Set is called for a new item or an item update with a cost
// param of 0.
Cost func(value interface{}) int64
}
|
Config传递给NewCache以创建新的Cache实例。
type Metrics
1
2
3
|
type Metrics struct {
// contains filtered or unexported fields
}
|
指标是缓存实例生命周期内性能统计信息的快照。
func (*Metrics) Clear
1
|
func (p *Metrics) Clear()
|
清除将重置所有指标。
func (*Metrics) CostAdded
1
|
func (p *Metrics) CostAdded() uint64
|
CostAdded是已添加的费用总和(成功的Set调用)。
func (*Metrics) CostEvicted
1
|
func (p *Metrics) CostEvicted() uint64
|
CostEvicted是已收回的所有成本的总和。
func (*Metrics) GetsDropped
1
|
func (p *Metrics) GetsDropped() uint64
|
GetsDropped是在内部删除的Get计数器增量的数量。
func (*Metrics) GetsKept
1
|
func (p *Metrics) GetsKept() uint64
|
GetsKept是保留的Get计数器增量的数量。
func (*Metrics) Hits
1
|
func (p *Metrics) Hits() uint64
|
命中数是在其中找到对应键值的Get调用数。
func (*Metrics) KeysAdded
1
|
func (p *Metrics) KeysAdded() uint64
|
KeysAdded 是添加了新的键值项的Set调用的总数。
func (*Metrics) KeysEvicted
1
|
func (p *Metrics) KeysEvicted() uint64
|
KeysEvicted是已淘汰密钥的总数。
func (*Metrics) KeysUpdated
1
|
func (p *Metrics) KeysUpdated() uint64
|
KeysUpdated是更新值的Set调用的总数。
func (*Metrics) Misses
1
|
func (p *Metrics) Misses() uint64
|
Misses是未找到对应键值的Get调用数。
func (*Metrics) Ratio
1
|
func (p *Metrics) Ratio() float64
|
比率是所有访问的命中数(命中+未命中)。这是Get成功调用的百分比。
func (*Metrics) SetsDropped
1
|
func (p *Metrics) SetsDropped() uint64
|
SetsDropped是未进入内部缓冲区(由于争用或其他原因)的Set调用数。
func (*Metrics) SetsRejected
1
|
func (p *Metrics) SetsRejected() uint64
|
SetsRejected是策略(TinyLFU)拒绝的Set呼叫数。
func (*Metrics) String
1
|
func (p *Metrics) String() string
|
String返回指标的字符串表示形式。