used response cache in responder

This commit is contained in:
Radu Ioan Fericean
2015-07-20 12:15:24 +03:00
parent 835f6533da
commit a7cf099c5c
3 changed files with 51 additions and 7 deletions

View File

@@ -8,20 +8,25 @@ import (
var ErrNotFound = errors.New("NOT_FOUND")
type CacheItem struct {
Value interface{}
Err error
}
type ResponseCache struct {
ttl time.Duration
cache map[string]interface{}
cache map[string]*CacheItem
mu sync.RWMutex
}
func NewResponseCache(ttl time.Duration) *ResponseCache {
return &ResponseCache{
ttl: ttl,
cache: make(map[string]interface{}),
cache: make(map[string]*CacheItem),
}
}
func (rc *ResponseCache) Cache(key string, item interface{}) {
func (rc *ResponseCache) Cache(key string, item *CacheItem) {
rc.mu.Lock()
rc.cache[key] = item
rc.mu.Unlock()
@@ -33,7 +38,7 @@ func (rc *ResponseCache) Cache(key string, item interface{}) {
}()
}
func (rc *ResponseCache) Get(key string) (interface{}, error) {
func (rc *ResponseCache) Get(key string) (*CacheItem, error) {
rc.mu.RLock()
defer rc.mu.RUnlock()
item, ok := rc.cache[key]