del cache cleanup items limit

This commit is contained in:
Sergey Chubaryan 2025-02-20 04:13:10 +03:00
parent 0db1564012
commit b434b8a455
4 changed files with 8 additions and 16 deletions

View File

@ -129,19 +129,17 @@ func (a *App) Run(p RunParams) {
// Periodically trigger cache cleanup
go func() {
tmr := time.NewTicker(5 * time.Minute)
tmr := time.NewTicker(15 * time.Minute)
defer tmr.Stop()
batchSize := 100
for {
select {
case <-ctx.Done():
return
case <-tmr.C:
userCache.CheckExpired(batchSize)
jwtCache.CheckExpired(batchSize)
linksCache.CheckExpired(batchSize)
userCache.CheckExpired()
jwtCache.CheckExpired()
linksCache.CheckExpired()
}
}
}()

View File

@ -78,7 +78,7 @@ func (c *cacheInmem[K, V]) Del(key K) {
delete(c.data, key)
}
func (c *cacheInmem[K, V]) CheckExpired(batchSize int) {
func (c *cacheInmem[K, V]) CheckExpired() {
if len(c.data) == 0 {
return
}
@ -90,10 +90,5 @@ func (c *cacheInmem[K, V]) CheckExpired(batchSize int) {
if time.Now().After(item.Expiration) {
delete(c.data, key)
}
batchSize--
if batchSize <= 0 {
return
}
}
}

View File

@ -45,10 +45,9 @@ func (c *cacheInmemSharded[V]) Del(key string) {
c.getShard(key).Del(key)
}
func (c *cacheInmemSharded[V]) CheckExpired(batchSize int) {
size := batchSize / c.info.Shards
func (c *cacheInmemSharded[V]) CheckExpired() {
for _, shard := range c.shards {
shard.CheckExpired(size)
shard.CheckExpired()
}
}

View File

@ -7,5 +7,5 @@ type Cache[K comparable, V any] interface {
Set(key K, value V, exp Expiration)
Del(key K)
CheckExpired(batchSize int)
CheckExpired()
}