forked from mad-gooze/jaderender
/
render_cache.go
44 lines (34 loc) · 1.24 KB
/
render_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Package jaderender is a template renderer that can be used with the Gin
// web framework https://github.com/gin-gonic/gin it uses the gojade template
// library https://github.com/zdebeer99/gojade
package jaderender
import (
"github.com/hashicorp/golang-lru"
"fmt"
)
// Some package-internal structure helping to implemlement render cache
type renderCache struct {
cache *lru.Cache
}
func newRenderCache(maxCacheEnties int) *renderCache {
arcCache, _ := lru.New(maxCacheEnties)
return &renderCache{
cache: arcCache,
}
}
func (c *renderCache) Get(templateName string, context interface{}) ([]byte, bool) {
rendered, alreadyInCache := c.cache.Get(c.generateCacheKey(templateName, context))
if alreadyInCache {
return rendered.([]byte), true
} else {
return nil, false
}
}
func (c *renderCache) Add(templateName string, context interface{}, rendered []byte) {
c.cache.Add(c.generateCacheKey(templateName, context), rendered)
}
// Probabbly not the best cache key in terms of performance but
// that Go has no way of defining custom hashable types so no elegant simple solution is avaliable here
func (c *renderCache) generateCacheKey(templateName string, context interface{}) string {
return fmt.Sprintf("%s %v", templateName, context)
}