Skip to content

Commit

Permalink
Revert "rename hasher to hash"
Browse files Browse the repository at this point in the history
This reverts commit f70f475.
  • Loading branch information
phuslu committed Jan 2, 2024
1 parent c667a54 commit d6163d1
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type Cache[K comparable, V any] struct {
shards []shard[K, V]
mask uint32
hash func(K) uint64
hasher maphash_Hasher[K]
loader func(key K) (value V, ttl time.Duration, err error)
group singleflight_Group[K, V]
}
Expand All @@ -32,7 +32,7 @@ func newWithShards[K comparable, V any](shardcount, shardsize int) *Cache[K, V]
c := &Cache[K, V]{
shards: make([]shard[K, V], shardcount),
mask: uint32(shardcount) - 1,
hash: maphash_NewHasher[K]().Hash,
hasher: maphash_NewHasher[K](),
}
for i := range c.shards {
c.shards[i] = *newshard[K, V](shardsize)
Expand All @@ -43,31 +43,31 @@ func newWithShards[K comparable, V any](shardcount, shardsize int) *Cache[K, V]

// Get returns value for key.
func (c *Cache[K, V]) Get(key K) (value V, ok bool) {
hash := uint32(c.hash(key))
hash := uint32(c.hasher.Hash(key))
return c.shards[hash&c.mask].Get(hash, key)
}

// Peek returns value for key, but does not modify its recency.
func (c *Cache[K, V]) Peek(key K) (value V, ok bool) {
hash := uint32(c.hash(key))
hash := uint32(c.hasher.Hash(key))
return c.shards[hash&c.mask].Peek(hash, key)
}

// Set inserts key value pair and returns previous value, if cache was full.
func (c *Cache[K, V]) Set(key K, value V) (prev V, replaced bool) {
hash := uint32(c.hash(key))
return c.shards[hash&c.mask].Set(hash, c.hash, key, value, 0)
hash := uint32(c.hasher.Hash(key))
return c.shards[hash&c.mask].Set(hash, c.hasher.Hash, key, value, 0)
}

// SetWithTTL inserts key value pair with ttl and returns previous value, if cache was full.
func (c *Cache[K, V]) SetWithTTL(key K, value V, ttl time.Duration) (prev V, replaced bool) {
hash := uint32(c.hash(key))
return c.shards[hash&c.mask].Set(hash, c.hash, key, value, ttl)
hash := uint32(c.hasher.Hash(key))
return c.shards[hash&c.mask].Set(hash, c.hasher.Hash, key, value, ttl)
}

// Delete method deletes value associated with key and returns deleted value (or empty value if key was not in cache).
func (c *Cache[K, V]) Delete(key K) (prev V) {
hash := uint32(c.hash(key))
hash := uint32(c.hasher.Hash(key))
return c.shards[hash&c.mask].Delete(hash, key)
}

Expand All @@ -91,7 +91,7 @@ func NewWithLoader[K comparable, V any](size int, loader func(K) (value V, ttl t
// GetOrLoad returns value for key, Call loader function if value was not in cache by singleflight.
// If loader parameter is nil, use global loader function provided by NewWithLoader instead.
func (c *Cache[K, V]) GetOrLoad(key K, loader func(K) (V, time.Duration, error)) (value V, err error, ok bool) {
hash := uint32(c.hash(key))
hash := uint32(c.hasher.Hash(key))
value, ok = c.shards[hash&c.mask].Get(hash, key)
if !ok {
if loader == nil {
Expand All @@ -106,7 +106,7 @@ func (c *Cache[K, V]) GetOrLoad(key K, loader func(K) (V, time.Duration, error))
if err != nil {
return v, err
}
c.shards[hash&c.mask].Set(hash, c.hash, key, v, ttl)
c.shards[hash&c.mask].Set(hash, c.hasher.Hash, key, v, ttl)
return v, nil
})
}
Expand Down

0 comments on commit d6163d1

Please sign in to comment.