Sobes.tech
Principal

You need to write a simple in-memory cache library. type Cache interface { Set(k, v string) Get(k string) (v string, ok bool) }

sobes.tech AI

Answer from AI

Below is an example of a simple in-memory cache implementation in Go, corresponding to the interface:

type Cache interface {
    Set(k, v string)
    Get(k string) (v string, ok bool)
}

type SimpleCache struct {
    data map[string]string
    mu   sync.RWMutex
}

func NewSimpleCache() *SimpleCache {
    return &SimpleCache{
        data: make(map[string]string),
    }
}

func (c *SimpleCache) Set(k, v string) {
    c.mu.Lock()
    defer c.mu.Unlock()
    c.data[k] = v
}

func (c *SimpleCache) Get(k string) (string, bool) {
    c.mu.RLock()
    defer c.mu.RUnlock()
    v, ok := c.data[k]
    return v, ok
}

This uses a mutex for thread-safe access to the map. Such a cache stores data in memory until the application is restarted.

You need to write a simple in-memory cache library… - sobes.tech