Skip to content

Commit

Permalink
Add CacheableParam interface
Browse files Browse the repository at this point in the history
Signed-off-by: Закиров Алихан <zakirov@tutu.ru>
  • Loading branch information
alikhanzt committed Dec 3, 2021
1 parent 1f22100 commit 0b414a9
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions enforcer_cached.go
Expand Up @@ -31,6 +31,10 @@ type CachedEnforcer struct {
locker *sync.RWMutex
}

type CacheableParam interface {
GetCacheKey() string
}

// NewCachedEnforcer creates a cached enforcer via file or DB.
func NewCachedEnforcer(params ...interface{}) (*CachedEnforcer, error) {
e := &CachedEnforcer{}
Expand Down Expand Up @@ -145,12 +149,15 @@ func (e *CachedEnforcer) setCachedResult(key string, res bool, extra ...interfac
func (e *CachedEnforcer) getKey(params ...interface{}) (string, bool) {
key := strings.Builder{}
for _, param := range params {
if val, ok := param.(string); ok {
key.WriteString(val)
key.WriteString("$$")
} else {
switch param.(type) {
case string:
key.WriteString(param.(string))
case CacheableParam:
key.WriteString(param.(CacheableParam).GetCacheKey())
default:
return "", false
}
key.WriteString("$$")
}
return key.String(), true
}
Expand Down

0 comments on commit 0b414a9

Please sign in to comment.