Package cache contains caching functions.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
731 B

package cache
import (
"bytes"
"encoding/gob"
"hash"
"hash/fnv"
)
// Hasher is the hash interface used for computing hashes by the Hash function.
// By default an FNV-1a hash function is used.
var Hasher func() hash.Hash
// Hash an arbitrary object.
func Hash(value interface{}) ([]byte, error) {
return HashUsing(value, Hasher())
}
// HashUsing hashes an arbitrary object using the provider hasher.
func HashUsing(value interface{}, hasher hash.Hash) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(value)
if err != nil {
return nil, err
}
hasher.Reset()
return hasher.Sum(buf.Bytes()), nil
}
func init() {
Hasher = func() hash.Hash {
return hash.Hash(fnv.New64a())
}
}