Skip to content

MemoryCache 上限控制 #95

Open
Open
@QingsiLiu

Description

@QingsiLiu

我在一个小项目中使用了缓存相关,我没有使用 Redis,想仅仅使用 MemoryCache 即可,但我发现应用 MemoryCache 时内存会无序增长直到触发 OOM,所以我去看了相关的代码实现

源码中的初始化代码如下:

// NewMemoryCache create a memory cache
func NewMemoryCache(keyPrefix string, encode encoding.Encoding, newObject func() interface{}) Cache {
	// see: https://dgraph.io/blog/post/introducing-ristretto-high-perf-go-cache/
	//		https://www.start.io/blog/we-chose-ristretto-cache-for-go-heres-why/
	config := &ristretto.Config{
		NumCounters: 1e7,     // number of keys to track frequency of (10M).
		MaxCost:     1 << 30, // maximum cost of cache (1GB).
		BufferItems: 64,      // number of keys per Get buffer.
	}
	store, _ := ristretto.NewCache(config)
	return &memoryCache{
		client:    store,
		KeyPrefix: keyPrefix,
		encoding:  encode,
		newObject: newObject,
	}
}

关于 MemoryCache 每张表都是默认的 MaxCost 1GB,这一块没有提供自定义的能力,是我的用法有问题吗?还是最好提供内存使用限制呢,或者全局进行设置呢?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions