cache.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package cache
  2. import (
  3. "context"
  4. "reflect"
  5. )
  6. // Cache is the interface that describes a cache.
  7. type Cache interface {
  8. // Get returns the item with the given key, otherwise returns false.
  9. Get(ctx context.Context, key string) (Item, bool)
  10. // Set sets the item at the given key.
  11. Set(ctx context.Context, key string, i Item)
  12. // Deletes the item at the given key.
  13. Del(ctx context.Context, key string)
  14. // The number of items in the cache.
  15. Len() int
  16. // The size in bytes.
  17. Size() int
  18. }
  19. // Item is the interface that describes a cacheable item.
  20. type Item interface {
  21. // The size that the item occupies in a cache.
  22. Size() int
  23. }
  24. // Bytes represents a cacheable byte slice.
  25. type Bytes []byte
  26. func (b Bytes) Size() int {
  27. return len(b)
  28. }
  29. type String string
  30. func (s String) Size() int {
  31. return len(s)
  32. }
  33. type Int int
  34. func (i Int) Size() int {
  35. return intSize
  36. }
  37. type Float float64
  38. func (f Float) Size() int {
  39. return floatSize
  40. }
  41. var (
  42. intSize = int(reflect.TypeOf(42).Size())
  43. floatSize = int(reflect.TypeOf(23.42).Size())
  44. )