Sobes.tech
Principal

Sadə yaddaşda önbellek kitabxanası yazmaq lazımdır. type Cache interface { Set(k, v string) Get(k string) (v string, ok bool) }

sobes.tech Süni İntellekt

AI-dan cavab

Aşağıda, Go dilində sadə bir yaddaş içi önbellek tətbiqi nümunəsi verilmişdir və bu, interfeysə uyğundur:

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, çoxluğa təhlükəsiz giriş üçün mutex istifadə olunur. Bu önbellek, tətbiq yenidən başlatılana qədər yaddaşda məlumat saxlayır.