geziyor/cache/memorycache/memorycache.go
Musab Gültekin 90d2be2210 Caching policies added.
We used httpcache library to implement this. As it was not possible to support different policies, I mostly copied and modified it.
2019-07-07 12:18:40 +03:00

40 lines
831 B
Go

package memorycache
import (
"sync"
)
// Cache is an implementation of Cache that stores responses in an in-memory map.
type Cache struct {
mu sync.RWMutex
items map[string][]byte
}
// Get returns the []byte representation of the response and true if present, false if not
func (c *Cache) Get(key string) (resp []byte, ok bool) {
c.mu.RLock()
resp, ok = c.items[key]
c.mu.RUnlock()
return resp, ok
}
// Set saves response resp to the cache with key
func (c *Cache) Set(key string, resp []byte) {
c.mu.Lock()
c.items[key] = resp
c.mu.Unlock()
}
// Delete removes key from the cache
func (c *Cache) Delete(key string) {
c.mu.Lock()
delete(c.items, key)
c.mu.Unlock()
}
// New returns a new Cache that will store items in an in-memory map
func New() *Cache {
c := &Cache{items: map[string][]byte{}}
return c
}