41 lines
642 B
Go
41 lines
642 B
Go
package models
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"sync"
|
|
)
|
|
|
|
type Page struct {
|
|
Data []byte
|
|
ETag string
|
|
}
|
|
|
|
type Cache struct {
|
|
Data map[string]*Page
|
|
Mu sync.RWMutex
|
|
}
|
|
|
|
func initCache() *Cache {
|
|
return &Cache{Data: make(map[string]*Page)}
|
|
}
|
|
|
|
func (c *Cache) Get(key string) (*Page, bool) {
|
|
c.Mu.RLock()
|
|
page, ok := c.Data[key]
|
|
c.Mu.RUnlock()
|
|
return page, ok
|
|
}
|
|
|
|
func (c *Cache) Set(key string, page *Page) {
|
|
c.Mu.Lock()
|
|
c.Data[key] = page
|
|
c.Mu.Unlock()
|
|
}
|
|
|
|
func GenerateETag(data []byte) string {
|
|
hash := md5.Sum(data)
|
|
return fmt.Sprintf(`"%s"`, hex.EncodeToString(hash[:]))
|
|
}
|