2
0

namespace.go 682 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package cachex
  2. import "strings"
  3. // Namespace isolates keys between different cache use-cases. (e.g. "channel_affinity:v1").
  4. type Namespace string
  5. func (n Namespace) prefix() string {
  6. ns := strings.TrimSpace(string(n))
  7. ns = strings.TrimRight(ns, ":")
  8. if ns == "" {
  9. return ""
  10. }
  11. return ns + ":"
  12. }
  13. func (n Namespace) FullKey(key string) string {
  14. key = strings.TrimSpace(key)
  15. if key == "" {
  16. return ""
  17. }
  18. p := n.prefix()
  19. if p == "" {
  20. return strings.TrimLeft(key, ":")
  21. }
  22. if strings.HasPrefix(key, p) {
  23. return key
  24. }
  25. return p + strings.TrimLeft(key, ":")
  26. }
  27. func (n Namespace) MatchPattern() string {
  28. p := n.prefix()
  29. if p == "" {
  30. return "*"
  31. }
  32. return p + "*"
  33. }