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 // Periodically trigger cache cleanup
go func() { go func() {
tmr := time.NewTicker(5 * time.Minute) tmr := time.NewTicker(15 * time.Minute)
defer tmr.Stop() defer tmr.Stop()
batchSize := 100
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return return
case <-tmr.C: case <-tmr.C:
userCache.CheckExpired(batchSize) userCache.CheckExpired()
jwtCache.CheckExpired(batchSize) jwtCache.CheckExpired()
linksCache.CheckExpired(batchSize) linksCache.CheckExpired()
} }
} }
}() }()

View File

@ -78,7 +78,7 @@ func (c *cacheInmem[K, V]) Del(key K) {
delete(c.data, key) delete(c.data, key)
} }
func (c *cacheInmem[K, V]) CheckExpired(batchSize int) { func (c *cacheInmem[K, V]) CheckExpired() {
if len(c.data) == 0 { if len(c.data) == 0 {
return return
} }
@ -90,10 +90,5 @@ func (c *cacheInmem[K, V]) CheckExpired(batchSize int) {
if time.Now().After(item.Expiration) { if time.Now().After(item.Expiration) {
delete(c.data, key) 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) c.getShard(key).Del(key)
} }
func (c *cacheInmemSharded[V]) CheckExpired(batchSize int) { func (c *cacheInmemSharded[V]) CheckExpired() {
size := batchSize / c.info.Shards
for _, shard := range c.shards { 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) Set(key K, value V, exp Expiration)
Del(key K) Del(key K)
CheckExpired(batchSize int) CheckExpired()
} }