gitee.com/woood2/luca@v1.0.4/internal/cache/key.go (about) 1 package cache 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 ) 8 9 func NewKey(prefix string, keyParams []string, duration time.Duration) *Key { 10 return &Key{ 11 prefix: prefix, 12 keyParams: keyParams, 13 Duration: duration, 14 } 15 } 16 17 type Key struct { 18 prefix string 19 keyParams []string 20 Duration time.Duration 21 } 22 23 func (k *Key) BuildKey(v ...interface{}) string { 24 if len(v) != len(k.keyParams) { 25 log.Panicf("key build error, expected params: %v, actual values: %v\n", k.keyParams, v) 26 } 27 fullKey := k.prefix 28 for _, p := range v { 29 fullKey += "_" + fmt.Sprintf("%v", p) 30 } 31 return fullKey 32 }