Skip to content

Commit c7a62b2

Browse files
committed
feat: expose ClearCache method from Enforcer
1 parent cbeafe9 commit c7a62b2

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

enforcer.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ func (e *Enforcer) SetEffector(eft effector.Effector) {
310310
e.eft = eft
311311
}
312312

313+
func (e *Enforcer) ClearCache() {
314+
e.invalidateMatcherMap()
315+
}
316+
313317
// ClearPolicy clears all policy.
314318
func (e *Enforcer) ClearPolicy() {
315319
e.invalidateMatcherMap()
@@ -730,7 +734,6 @@ func (e *Enforcer) enforce(matcher string, explains *[]string, rvals ...interfac
730734

731735
result, err := expression.Eval(parameters)
732736
// log.LogPrint("Result: ", result)
733-
734737
if err != nil {
735738
return false, err
736739
}
@@ -787,7 +790,6 @@ func (e *Enforcer) enforce(matcher string, explains *[]string, rvals ...interfac
787790
parameters.pVals = make([]string, len(parameters.pTokens))
788791

789792
result, err := expression.Eval(parameters)
790-
791793
if err != nil {
792794
return false, err
793795
}
@@ -830,7 +832,7 @@ func (e *Enforcer) enforce(matcher string, explains *[]string, rvals ...interfac
830832
func (e *Enforcer) getAndStoreMatcherExpression(hasEval bool, expString string, functions map[string]govaluate.ExpressionFunction) (*govaluate.EvaluableExpression, error) {
831833
var expression *govaluate.EvaluableExpression
832834
var err error
833-
var cachedExpression, isPresent = e.matcherMap.Load(expString)
835+
cachedExpression, isPresent := e.matcherMap.Load(expString)
834836

835837
if !hasEval && isPresent {
836838
expression = cachedExpression.(*govaluate.EvaluableExpression)

enforcer_cached.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,17 @@ func GetCacheKey(params ...interface{}) (string, bool) {
173173
return key.String(), true
174174
}
175175

176+
func (e *CachedEnforcer) ClearCache() {
177+
if atomic.LoadInt32(&e.enableCache) != 0 {
178+
if err := e.cache.Clear(); err != nil {
179+
e.logger.LogError(err, "clear cache failed")
180+
return
181+
}
182+
}
183+
184+
e.Enforcer.ClearCache()
185+
}
186+
176187
// ClearPolicy clears all policy.
177188
func (e *CachedEnforcer) ClearPolicy() {
178189
if atomic.LoadInt32(&e.enableCache) != 0 {

enforcer_interface.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import (
2222
"github.com/casbin/govaluate"
2323
)
2424

25-
var _ IEnforcer = &Enforcer{}
26-
var _ IEnforcer = &SyncedEnforcer{}
27-
var _ IEnforcer = &CachedEnforcer{}
25+
var (
26+
_ IEnforcer = &Enforcer{}
27+
_ IEnforcer = &SyncedEnforcer{}
28+
_ IEnforcer = &CachedEnforcer{}
29+
)
2830

2931
// IEnforcer is the API interface of Enforcer.
3032
type IEnforcer interface {
@@ -41,6 +43,7 @@ type IEnforcer interface {
4143
GetRoleManager() rbac.RoleManager
4244
SetRoleManager(rm rbac.RoleManager)
4345
SetEffector(eft effector.Effector)
46+
ClearCache()
4447
ClearPolicy()
4548
LoadPolicy() error
4649
LoadFilteredPolicy(filter interface{}) error

enforcer_synced.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ func (e *SyncedEnforcer) LoadModel() error {
106106
return e.Enforcer.LoadModel()
107107
}
108108

109+
func (e *SyncedEnforcer) ClearCache() {
110+
e.m.Lock()
111+
defer e.m.Unlock()
112+
e.Enforcer.ClearCache()
113+
}
114+
109115
// ClearPolicy clears all policy.
110116
func (e *SyncedEnforcer) ClearPolicy() {
111117
e.m.Lock()

0 commit comments

Comments
 (0)