mostly finished db pages
This commit is contained in:
parent
c5d41b0965
commit
38f7c31517
10 changed files with 278 additions and 125 deletions
49
pkg/cache/cache.go
vendored
Normal file
49
pkg/cache/cache.go
vendored
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CachedPage struct {
|
||||
Page *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 *Page) {
|
||||
now := time.Now()
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
cp, ok := c.Cache[title]
|
||||
if ok && now.After(c.CacheTime) {
|
||||
cp.Page = p
|
||||
cp.CacheTime = now
|
||||
c.Cache[title] = cp
|
||||
return
|
||||
}
|
||||
c.Cache[title] = &CachedPage{p, now}
|
||||
}
|
||||
|
||||
func (c *Cache) Get(title string) *Page {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
if cp, ok := c.Cache[title]; ok {
|
||||
return cp.Page
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) Delete(title string) {
|
||||
c.Lock()
|
||||
delete(c.Cache, title)
|
||||
c.Unlock()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue