dgraph-io / ristretto

A high performance memory-bound Go cache

Home Page:https://dgraph.io/blog/post/introducing-ristretto-high-perf-go-cache/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NewDefaultCache to encapsulate defaults

WBare opened this issue · comments

The current NewCache constructor requires the user to hard code ristretto defaults in their code.

Specifically, NumCounters is 100 x estimated items. The interface should allow the user to avoid hardcoding this into their code. Similarly, BufferItems: 64 is just a good number that works. The user should not need to hardcode this into client software either.

If any implementation changes in the future affect these currently valid defaults, the lack of encapsulation will require unnecessary changes to client code.

I suggest adding something like the following code (which also encapsulates the most likely default panic behavior on constructor errors). This allows the client to use defaults appropriate to their current version of ristretto. Also, it provides a simplified constructor that does not require client code knowledge of typically encapsulated implementation details.

As a small side benefit, the example code will also be cleaner and clearer.

// CountersPerItem is a good multiple of the number of expected items to track
const CountersPerItem = 100
// DefaultBufferItems is a good default size for Get buffers
const DefaultBufferItems = 64

func NewDefaultCache(expectedItems, avgCostPerItem int64) (cache *Cache) {
	
	cache, err := NewCache(&Config{
		NumCounters: expectedItems * CountersPerItem,
		MaxCost:     expectedItems * avgCostPerItem,
		BufferItems: DefaultBufferItems,  // number of keys per Get buffer.
	})
	if err != nil {
		panic(err)
	}
	return
} 

Github issues have been deprecated.
This issue has been moved to discuss. You can follow the conversation there and also subscribe to updates by changing your notification preferences.

drawing