From d6163d12987b1a6e5dc074433987cf4524ed0706 Mon Sep 17 00:00:00 2001 From: phuslu Date: Tue, 2 Jan 2024 19:33:20 +0800 Subject: [PATCH] Revert "rename hasher to hash" This reverts commit f70f475b9725488cfbae0a7642c1c17a8a944058. --- cache.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/cache.go b/cache.go index 1e6226b..339ef42 100644 --- a/cache.go +++ b/cache.go @@ -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] } @@ -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) @@ -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) } @@ -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 { @@ -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 }) }