Sobes.tech
Junior — Senior

Creating a thread-safe cache with automatic TTL expiration for User objects

livecode

Task condition

It is necessary to implement an in-memory cache that: • Stores structures of type User, using the User.ID field as the key. • Automatically deletes entries after a specified time-to-live (default is 3 seconds). • Provides the following operations:

  • Set(user User, ttl time.Duration) – add an element to the cache with the specified TTL.
  • Get(id string) (User, bool) – retrieve an element by its identifier and a presence flag.
  • Delete(id string) – remove an element from the cache. • Ensures correct operation in a concurrent environment (thread safety).