File size: 682 Bytes
daa8246
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package cachex

import "strings"

// Namespace isolates keys between different cache use-cases. (e.g. "channel_affinity:v1").
type Namespace string

func (n Namespace) prefix() string {
	ns := strings.TrimSpace(string(n))
	ns = strings.TrimRight(ns, ":")
	if ns == "" {
		return ""
	}
	return ns + ":"
}

func (n Namespace) FullKey(key string) string {
	key = strings.TrimSpace(key)
	if key == "" {
		return ""
	}
	p := n.prefix()
	if p == "" {
		return strings.TrimLeft(key, ":")
	}
	if strings.HasPrefix(key, p) {
		return key
	}
	return p + strings.TrimLeft(key, ":")
}

func (n Namespace) MatchPattern() string {
	p := n.prefix()
	if p == "" {
		return "*"
	}
	return p + "*"
}