Sobes.tech
Middle — Senior

Thread-safe in-memory cache with TTL and auto-cleanup

livecode

Task condition

It is necessary to create a thread-safe in-memory cache that implements the Cacher interface. The cache should store elements with a specified time-to-live (TTL) and automatically remove expired entries through garbage collection. The following operations are required:

  • Set(key string, value any) — add or update a value by key.
  • Get(k string) (any, bool) — retrieve a value, checking if its TTL has not expired.
 type Cacher interface {
    Set(key string, value any)
    Get(k string) (any, bool)
}

func NewCacher(duration time.Duration) Cacher {
    
}