Sobes.tech
Senior

How was data caching implemented? What caching policy was used?

sobes.tech AI

Answer from AI

Data caching is usually implemented using a data structure that stores recently or frequently used objects for quick access.

The caching policy depends on the task, but commonly used policies include:

  • LRU (Least Recently Used) — removes the least recently used items.
  • LFU (Least Frequently Used) — removes the least frequently used items.
  • TTL (Time To Live) — removes items after a certain period.

In Go, you can use a map with additional logic for the removal policy or ready-made libraries (e.g., groupcache or ristretto).

Example of a simple LRU cache using a list container:

import (
    "container/list"
    "sync"
)

type CacheItem struct {
    key   string
    value interface{}
}

type LRUCache struct {
    capacity int
    cache    map[string]*list.Element
    list     *list.List
    mu       sync.Mutex
}

func NewLRUCache(cap int) *LRUCache {
    return &LRUCache{
        capacity: cap,
        cache:    make(map[string]*list.Element),
        list:     list.New(),
    }
}

func (c *LRUCache) Get(key string) (interface{}, bool) {
    c.mu.Lock()
    defer c.mu.Unlock()

    if elem, ok := c.cache[key]; ok {
        c.list.MoveToFront(elem)
        return elem.Value.(*CacheItem).value, true
    }
    return nil, false
}

func (c *LRUCache) Put(key string, value interface{}) {
    c.mu.Lock()
    defer c.mu.Unlock()

    if elem, ok := c.cache[key]; ok {
        c.list.MoveToFront(elem)
        elem.Value.(*CacheItem).value = value
        return
    }

    if c.list.Len() == c.capacity {
        back := c.list.Back()
        if back != nil {
            c.list.Remove(back)
            delete(c.cache, back.Value.(*CacheItem).key)
        }
    }

    item := &CacheItem{key, value}
    elem := c.list.PushFront(item)
    c.cache[key] = elem
}