github.com/openfga/openfga@v1.5.4-rc1/internal/keys/keys.go (about) 1 package keys 2 3 import ( 4 "github.com/cespare/xxhash/v2" 5 ) 6 7 // cacheKeyHasher implements a key hash using Hash64 for computing cache keys in a stable way. 8 type cacheKeyHasher struct { 9 hasher *xxhash.Digest 10 } 11 12 // NewCacheKeyHasher returns a hasher for string values. 13 func NewCacheKeyHasher(xhash *xxhash.Digest) *cacheKeyHasher { 14 return &cacheKeyHasher{hasher: xhash} 15 } 16 17 // WriteString writes the provided string to the hash. 18 func (c *cacheKeyHasher) WriteString(value string) error { 19 _, err := c.hasher.WriteString(value) 20 if err != nil { 21 return err 22 } 23 24 return nil 25 } 26 27 // Key returns the stableCacheKey that this key hash defines. 28 func (c cacheKeyHasher) Key() stableCacheKey { 29 return stableCacheKey{ 30 stableSum: c.hasher.Sum64(), 31 } 32 } 33 34 type stableCacheKey struct { 35 stableSum uint64 36 } 37 38 // ToUInt64 returns the cache key in the form of a stable uint64 value. 39 func (key stableCacheKey) ToUInt64() uint64 { 40 return key.stableSum 41 }