NewDefaultCache to encapsulate defaults

Moved from GitHub ristretto/146

Posted by WBare:

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
}