Skip to content
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

Fix shard selection of ShardedLRU #20

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions shardedlru.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (lru *ShardedLRU[K, V]) Len() (length int) {
// Returns true, true if key was updated and eviction occurred.
func (lru *ShardedLRU[K, V]) AddWithLifetime(key K, value V, lifetime time.Duration) (evicted bool) {
hash := lru.hash(key)
shard := hash & lru.mask
shard := (hash >> 16) & lru.mask

lru.mus[shard].Lock()
evicted = lru.lrus[shard].addWithLifetime(hash, key, value, lifetime)
Expand All @@ -151,7 +151,7 @@ func (lru *ShardedLRU[K, V]) Add(key K, value V) (evicted bool) {
// recently used item.
func (lru *ShardedLRU[K, V]) Get(key K) (value V, ok bool) {
hash := lru.hash(key)
shard := hash & lru.mask
shard := (hash >> 16) & lru.mask

lru.mus[shard].Lock()
value, ok = lru.lrus[shard].get(hash, key)
Expand All @@ -163,7 +163,7 @@ func (lru *ShardedLRU[K, V]) Get(key K) (value V, ok bool) {
// Peek looks up a key's value from the cache, without changing its recent-ness.
func (lru *ShardedLRU[K, V]) Peek(key K) (value V, ok bool) {
hash := lru.hash(key)
shard := hash & lru.mask
shard := (hash >> 16) & lru.mask

lru.mus[shard].RLock()
value, ok = lru.lrus[shard].peek(hash, key)
Expand All @@ -175,7 +175,7 @@ func (lru *ShardedLRU[K, V]) Peek(key K) (value V, ok bool) {
// Contains checks for the existence of a key, without changing its recent-ness.
func (lru *ShardedLRU[K, V]) Contains(key K) (ok bool) {
hash := lru.hash(key)
shard := hash & lru.mask
shard := (hash >> 16) & lru.mask

lru.mus[shard].RLock()
ok = lru.lrus[shard].contains(hash, key)
Expand All @@ -188,7 +188,7 @@ func (lru *ShardedLRU[K, V]) Contains(key K) (ok bool) {
// The return value indicates whether the key existed or not.
func (lru *ShardedLRU[K, V]) Remove(key K) (removed bool) {
hash := lru.hash(key)
shard := hash & lru.mask
shard := (hash >> 16) & lru.mask

lru.mus[shard].Lock()
removed = lru.lrus[shard].remove(hash, key)
Expand Down
Loading