mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-06-03 01:50:14 +00:00
fix: fix bugs in cache order
This commit is contained in:
@@ -97,7 +97,9 @@ func (cs *CacheStore[T]) setCallback(key string, value T, ttl time.Duration) {
|
|||||||
expiresAt: expiresAt,
|
expiresAt: expiresAt,
|
||||||
}
|
}
|
||||||
|
|
||||||
cs.order = append(cs.order, key)
|
if !slices.Contains(cs.order, key) {
|
||||||
|
cs.order = append(cs.order, key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *CacheStore[T]) Set(key string, value T, ttl time.Duration) {
|
func (cs *CacheStore[T]) Set(key string, value T, ttl time.Duration) {
|
||||||
@@ -159,7 +161,7 @@ func (cs *CacheStore[T]) evictOne() bool {
|
|||||||
|
|
||||||
for k, e := range cs.cache {
|
for k, e := range cs.cache {
|
||||||
if e.expiresAt != nil && now.After(*e.expiresAt) {
|
if e.expiresAt != nil && now.After(*e.expiresAt) {
|
||||||
delete(cs.cache, k)
|
cs.deleteCallback(k)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if e.expiresAt != nil && (oldestExp == nil || e.expiresAt.Before(*oldestExp)) {
|
if e.expiresAt != nil && (oldestExp == nil || e.expiresAt.Before(*oldestExp)) {
|
||||||
@@ -169,13 +171,11 @@ func (cs *CacheStore[T]) evictOne() bool {
|
|||||||
|
|
||||||
// If we found an oldest key, evict it else we delete the first key in the order list
|
// If we found an oldest key, evict it else we delete the first key in the order list
|
||||||
if oldestKey != "" {
|
if oldestKey != "" {
|
||||||
delete(cs.cache, oldestKey)
|
cs.deleteCallback(oldestKey)
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
if len(cs.order) > 0 {
|
if len(cs.order) > 0 {
|
||||||
firstKey := cs.order[0]
|
cs.deleteCallback(cs.order[0])
|
||||||
cs.order = cs.order[1:]
|
|
||||||
delete(cs.cache, firstKey)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user