This is an automated email from the ASF dual-hosted git repository.
hsluoyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/casbin.git
The following commit(s) were added to refs/heads/master by this push:
new 1e19095 fix: avoid cache key collisions in cached enforcer
1e19095 is described below
commit 1e19095c5287eff82ae0d5cc07502d87017944f2
Author: Yang Luo <[email protected]>
AuthorDate: Sat Oct 10 02:29:41 2026 +0800
fix: avoid cache key collisions in cached enforcer
---
enforcer_cached.go | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/enforcer_cached.go b/enforcer_cached.go
index deff218..0fc930f 100644
--- a/enforcer_cached.go
+++ b/enforcer_cached.go
@@ -18,6 +18,7 @@
package casbin
import (
+ "strconv"
"strings"
"sync"
"sync/atomic"
@@ -166,17 +167,24 @@ func GetCacheKey(params ...interface{}) (string, bool) {
for _, param := range params {
switch typedParam := param.(type) {
case string:
- key.WriteString(typedParam)
+ writeCacheKeyPart(&key, 's', typedParam)
case CacheableParam:
- key.WriteString(typedParam.GetCacheKey())
+ writeCacheKeyPart(&key, 'c', typedParam.GetCacheKey())
default:
return "", false
}
- key.WriteString("$$")
}
return key.String(), true
}
+// writeCacheKeyPart length-prefixes each part so that different requests
never share a key.
+func writeCacheKeyPart(key *strings.Builder, kind byte, part string) {
+ key.WriteByte(kind)
+ key.WriteString(strconv.Itoa(len(part)))
+ key.WriteByte(':')
+ key.WriteString(part)
+}
+
// ClearPolicy clears all policy.
func (e *CachedEnforcer) ClearPolicy() {
if atomic.LoadInt32(&e.enableCache) != 0 {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]