Sobes.tech
Middle+

What are cache problems and how to deal with them?

sobes.tech AI

Answer from AI

Cache problems are often related to outdated data, race conditions during updates, and excessive memory consumption. To address these issues, the following are used:

  • Cache invalidation: timely removal or updating of outdated data.
  • TTL (time-to-live): setting a lifespan for cached objects.
  • Access synchronization: using mutexes or atomic operations to prevent race conditions.
  • Cache size limiting: to avoid memory overflow.

Example of a simple TTL cache in Go:

import (
    "sync"
    "time"
)

type cacheItem struct {
    value      interface{}
    expiration int64
}

type Cache struct {
    items map[string]cacheItem
    mu    sync.RWMutex
}

func NewCache() *Cache {
    return &Cache{
        items: make(map[string]cacheItem),
    }
}

func (c *Cache) Set(key string, value interface{}, duration time.Duration) {
    c.mu.Lock()
    defer c.mu.Unlock()
    c.items[key] = cacheItem{
        value:      value,
        expiration: time.Now().Add(duration).UnixNano(),
    }
}

func (c *Cache) Get(key string) (interface{}, bool) {
    c.mu.RLock()
    defer c.mu.RUnlock()
    item, found := c.items[key]
    if !found || time.Now().UnixNano() > item.expiration {
        return nil, false
    }
    return item.value, true
}