Skip to content

Commit

Permalink
feat: make the default cache expirable (#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
JalinWang authored Mar 4, 2023
1 parent b36df01 commit ac60b3d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
8 changes: 4 additions & 4 deletions enforcer_cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
"strings"
"sync"
"sync/atomic"
"time"

"github.com/casbin/casbin/v2/persist/cache"
)

// CachedEnforcer wraps Enforcer and provides decision cache
type CachedEnforcer struct {
*Enforcer
expireTime uint
expireTime time.Duration
cache cache.Cache
enableCache int32
locker *sync.RWMutex
Expand All @@ -45,8 +46,7 @@ func NewCachedEnforcer(params ...interface{}) (*CachedEnforcer, error) {
}

e.enableCache = 1
cache := cache.DefaultCache(make(map[string]bool))
e.cache = &cache
e.cache, _ = cache.NewDefaultCache()
e.locker = new(sync.RWMutex)
return e, nil
}
Expand Down Expand Up @@ -132,7 +132,7 @@ func (e *CachedEnforcer) getCachedResult(key string) (res bool, err error) {
return e.cache.Get(key)
}

func (e *CachedEnforcer) SetExpireTime(expireTime uint) {
func (e *CachedEnforcer) SetExpireTime(expireTime time.Duration) {
e.expireTime = expireTime
}

Expand Down
2 changes: 1 addition & 1 deletion persist/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var ErrNoSuchKey = errors.New("there's no such key existing in cache")

type Cache interface {
// Set puts key and value into cache.
// First parameter for extra should be uint denoting expected survival time.
// First parameter for extra should be time.Time object denoting expected survival time.
// If survival time equals 0 or less, the key will always be survival.
Set(key string, value bool, extra ...interface{}) error

Expand Down
31 changes: 28 additions & 3 deletions persist/cache/default-cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,38 @@

package cache

type DefaultCache map[string]bool
import "time"

type cacheItem struct {
value bool
expiresAt time.Time
ttl time.Duration
}

type DefaultCache map[string]cacheItem

func (c *DefaultCache) Set(key string, value bool, extra ...interface{}) error {
(*c)[key] = value
ttl := time.Duration(-1)
if len(extra) > 0 {
ttl = extra[0].(time.Duration)
}
(*c)[key] = cacheItem{
value: value,
expiresAt: time.Now().Add(ttl),
ttl: ttl,
}
return nil
}

func (c *DefaultCache) Get(key string) (bool, error) {
if res, ok := (*c)[key]; !ok {
return false, ErrNoSuchKey
} else {
return res, nil
if res.ttl > 0 && time.Now().After(res.expiresAt) {
delete(*c, key)
return false, ErrNoSuchKey
}
return res.value, nil
}
}

Expand All @@ -42,3 +62,8 @@ func (c *DefaultCache) Clear() error {
*c = make(DefaultCache)
return nil
}

func NewDefaultCache() (Cache, error) {
cache := make(DefaultCache)
return &cache, nil
}

0 comments on commit ac60b3d

Please sign in to comment.