-
Notifications
You must be signed in to change notification settings - Fork 286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move the cache implementation behind an interface #614
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cache | ||
|
||
// Config for caching. | ||
// See: https://github.com/dgraph-io/ristretto#Config | ||
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 roughly | ||
// 3 bytes (4 bits for each counter * 4 copies plus about a byte per | ||
// counter for the bloom filter). Note that the number of counters is | ||
// internally rounded up to the nearest power of 2, so the space usage | ||
// may be a little larger than 3 bytes * NumCounters. | ||
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 | ||
} | ||
|
||
// Cache defines an interface for a generic cache. | ||
type Cache interface { | ||
// Get returns the value for the given key in the cache, if it exists. | ||
Get(key interface{}) (interface{}, bool) | ||
|
||
// Set sets a value for the key in the cache, with the given cost. | ||
Set(key interface{}, entry interface{}, cost int64) bool | ||
|
||
// Wait waits for the cache to process and apply updates. | ||
Wait() | ||
|
||
// Close closes the cache's background workers (if any). | ||
Close() | ||
|
||
// GetMetrics returns the metrics block for the cache. | ||
GetMetrics() Metrics | ||
} | ||
|
||
// Metrics defines metrics exported by the cache. | ||
type Metrics interface { | ||
// Hits is the number of cache hits. | ||
Hits() uint64 | ||
|
||
// Misses is the number of cache misses. | ||
Misses() uint64 | ||
|
||
// CostAdded returns the total cost of added items. | ||
CostAdded() uint64 | ||
|
||
// CostEvicted returns the total cost of evicted items. | ||
CostEvicted() uint64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//go:build !wasm | ||
// +build !wasm | ||
|
||
package cache | ||
|
||
import ( | ||
"github.com/dgraph-io/ristretto" | ||
) | ||
|
||
// NewCache creates a new ristretto cache from the given config. | ||
func NewCache(config *Config) (Cache, error) { | ||
cache, err := ristretto.NewCache(&ristretto.Config{ | ||
NumCounters: config.NumCounters, | ||
MaxCost: config.MaxCost, | ||
BufferItems: config.BufferItems, | ||
Metrics: config.Metrics, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return wrapped{cache}, nil | ||
} | ||
|
||
type wrapped struct { | ||
*ristretto.Cache | ||
} | ||
|
||
func (w wrapped) GetMetrics() Metrics { | ||
return w.Cache.Metrics | ||
} | ||
jakedt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var _ Cache = &wrapped{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we stick to the "return structs accept interfaces" mantra here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can, but it does mean that
NewCache
's signature will change based on the platform, which may cause issues in certain compilation targetsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get it. In this case it's always going to be a ristretto cache coming out of
cache_ristretto
. The method should probably be calledNewRistrettoCache
, and it will be totally missing on the wasm arch. If there were a wasm compatible version it would be calledNewHashmapCache
or something, and its version ofwrapped
would be specific to that cache impl.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, no. The whole point is that it is
NewCache
regardless of platform, so that when it is invoked outside of the package, the caller does not need to know on which platform the code is operating. Otherwise, it would fail to compile.