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.
This commit is contained in:
Musab Gültekin
2019-07-07 12:18:40 +03:00
parent 0d6c2a6864
commit 90d2be2210
13 changed files with 2349 additions and 16 deletions

39
cache/memorycache/memorycache.go vendored Normal file
View File

@ -0,0 +1,39 @@
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
}