Package cache contains caching functions. https://maze.io/x/cache.v1
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.

hash.go 731B

1234567891011121314151617181920212223242526272829303132333435
  1. package cache
  2. import (
  3. "bytes"
  4. "encoding/gob"
  5. "hash"
  6. "hash/fnv"
  7. )
  8. // Hasher is the hash interface used for computing hashes by the Hash function.
  9. // By default an FNV-1a hash function is used.
  10. var Hasher func() hash.Hash
  11. // Hash an arbitrary object.
  12. func Hash(value interface{}) ([]byte, error) {
  13. return HashUsing(value, Hasher())
  14. }
  15. // HashUsing hashes an arbitrary object using the provider hasher.
  16. func HashUsing(value interface{}, hasher hash.Hash) ([]byte, error) {
  17. var buf bytes.Buffer
  18. enc := gob.NewEncoder(&buf)
  19. err := enc.Encode(value)
  20. if err != nil {
  21. return nil, err
  22. }
  23. hasher.Reset()
  24. return hasher.Sum(buf.Bytes()), nil
  25. }
  26. func init() {
  27. Hasher = func() hash.Hash {
  28. return hash.Hash(fnv.New64a())
  29. }
  30. }