// Copyright (C) 2019 Marius Schellenberger package cache import ( "git.giftfish.de/ston1th/gowiki/pkg/core" "sync" "time" ) type CachedPage struct { Page *core.Page CacheTime time.Time } type Cache struct { sync.RWMutex Cache map[string]*CachedPage } func NewCache() *Cache { return &Cache{Cache: make(map[string]*CachedPage)} } func (c *Cache) Add(title string, p *core.Page) { now := time.Now() c.Lock() defer c.Unlock() cp, ok := c.Cache[title] if ok && now.After(cp.CacheTime) { cp.Page = p cp.CacheTime = now c.Cache[title] = cp return } c.Cache[title] = &CachedPage{p, now} } func (c *Cache) Get(title string) (p *core.Page) { c.RLock() defer c.RUnlock() if cp, ok := c.Cache[title]; ok { p = new(core.Page) *p = *cp.Page return } return nil } func (c *Cache) Delete(title string) { c.Lock() delete(c.Cache, title) c.Unlock() }