mostly finished db pages
This commit is contained in:
parent
c5d41b0965
commit
38f7c31517
10 changed files with 278 additions and 125 deletions
|
|
@ -1,49 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CachedArticle struct {
|
||||
Article *Article
|
||||
CacheTime time.Time
|
||||
}
|
||||
|
||||
type IndexCache struct {
|
||||
sync.RWMutex
|
||||
Cache map[string]*CachedArticle
|
||||
}
|
||||
|
||||
func NewIndexCache() *IndexCache {
|
||||
return &IndexCache{Cache: make(map[string]*CachedArticle)}
|
||||
}
|
||||
|
||||
func (ic IndexCache) Add(title string, a *Article) {
|
||||
now := time.Now()
|
||||
ic.Lock()
|
||||
defer ic.Unlock()
|
||||
c, ok := ic.Cache[title]
|
||||
if ok && now.After(c.CacheTime) {
|
||||
c.Article = a
|
||||
c.CacheTime = now
|
||||
ic.Cache[title] = c
|
||||
return
|
||||
}
|
||||
ic.Cache[title] = &CachedArticle{a, now}
|
||||
}
|
||||
|
||||
func (ic IndexCache) Get(title string) *Article {
|
||||
ic.RLock()
|
||||
defer ic.RUnlock()
|
||||
if a, ok := ic.Cache[title]; ok {
|
||||
return a.Article
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ic *IndexCache) Delete(title string) {
|
||||
ic.Lock()
|
||||
delete(ic.Cache, title)
|
||||
ic.Unlock()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue