Sobes.tech
Principal

Basit bir bellek içi önbellek kütüphanesi yazmanız gerekiyor. type Cache arayüz { Set(k, v string) Get(k string) (v string, ok bool) }

sobes.tech yapay zeka

AI'dan gelen yanıt

Aşağıda, Go dilinde basit bir bellek içi önbellek uygulaması örneği bulunmaktadır ve bu, arayüfe uygundur:

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
}

Burada, harf güvenli erişim için mutex kullanılır. Bu önbellek, uygulama yeniden başlatılana kadar bellekte veri tutar.