Merge pull request #631 from trheyi/main
refactor: Update Request struct to include data caching functionality
This commit is contained in:
commit
34db76409e
4 changed files with 80 additions and 12 deletions
|
|
@ -107,6 +107,7 @@ func (r *Request) Render() (string, int, error) {
|
||||||
configText := ""
|
configText := ""
|
||||||
cacheStore := ""
|
cacheStore := ""
|
||||||
cacheTime := 0
|
cacheTime := 0
|
||||||
|
dateCacheTime := 0
|
||||||
|
|
||||||
configSel := doc.Find("script[name=config]")
|
configSel := doc.Find("script[name=config]")
|
||||||
if configSel != nil && configSel.Length() > 0 {
|
if configSel != nil && configSel.Length() > 0 {
|
||||||
|
|
@ -132,6 +133,7 @@ func (r *Request) Render() (string, int, error) {
|
||||||
// Cache store
|
// Cache store
|
||||||
cacheStore = conf.CacheStore
|
cacheStore = conf.CacheStore
|
||||||
cacheTime = conf.Cache
|
cacheTime = conf.Cache
|
||||||
|
dateCacheTime = conf.DataCache
|
||||||
}
|
}
|
||||||
|
|
||||||
dataText := ""
|
dataText := ""
|
||||||
|
|
@ -163,6 +165,7 @@ func (r *Request) Render() (string, int, error) {
|
||||||
Config: configText,
|
Config: configText,
|
||||||
CacheStore: cacheStore,
|
CacheStore: cacheStore,
|
||||||
CacheTime: time.Duration(cacheTime) * time.Second,
|
CacheTime: time.Duration(cacheTime) * time.Second,
|
||||||
|
DataCacheTime: time.Duration(dateCacheTime) * time.Second,
|
||||||
}
|
}
|
||||||
go core.SetCache(r.File, c)
|
go core.SetCache(r.File, c)
|
||||||
go log.Trace("[SUI] The page %s is cached file=%s", r.Request.URL.Path, r.File)
|
go log.Trace("[SUI] The page %s is cached file=%s", r.Request.URL.Path, r.File)
|
||||||
|
|
@ -174,7 +177,22 @@ func (r *Request) Render() (string, int, error) {
|
||||||
return "", code, err
|
return "", code, err
|
||||||
}
|
}
|
||||||
|
|
||||||
data := r.Request.NewData()
|
requestHash := r.Hash()
|
||||||
|
data := core.Data{}
|
||||||
|
dataCacheKey := fmt.Sprintf("data:%s", requestHash)
|
||||||
|
dataHitCache := false
|
||||||
|
|
||||||
|
// Read from data cache directly
|
||||||
|
if !r.Request.DisableCache() && c.DataCacheTime > 0 && c.CacheStore != "" {
|
||||||
|
data, dataHitCache = c.GetData(dataCacheKey)
|
||||||
|
if dataHitCache {
|
||||||
|
log.Trace("[SUI] The page %s data is cached %v file=%s key=%s", r.Request.URL.Path, c.DataCacheTime, r.File, dataCacheKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !dataHitCache {
|
||||||
|
// Request the data
|
||||||
|
data = r.Request.NewData()
|
||||||
if c.Data != "" {
|
if c.Data != "" {
|
||||||
err = r.Request.ExecStringMerge(data, c.Data)
|
err = r.Request.ExecStringMerge(data, c.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -190,12 +208,18 @@ func (r *Request) Render() (string, int, error) {
|
||||||
data["$global"] = global
|
data["$global"] = global
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save to The Cache
|
||||||
|
if c.DataCacheTime > 0 && c.CacheStore != "" {
|
||||||
|
go c.SetData(dataCacheKey, data, c.DataCacheTime)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Read from cache directly
|
// Read from cache directly
|
||||||
key := fmt.Sprintf("page:%s:%s", r.Hash(), data.Hash())
|
key := fmt.Sprintf("page:%s:%s", requestHash, data.Hash())
|
||||||
if !r.Request.DisableCache() && c.CacheTime > 0 && c.CacheStore != "" {
|
if !r.Request.DisableCache() && c.CacheTime > 0 && c.CacheStore != "" {
|
||||||
html, exists := c.GetHTML(key)
|
html, exists := c.GetHTML(key)
|
||||||
if exists {
|
if exists {
|
||||||
log.Trace("[SUI] The page %s is cached %v file=%s", r.Request.URL.Path, c.CacheTime, r.File)
|
log.Trace("[SUI] The page %s is cached %v file=%s key=%s", r.Request.URL.Path, c.CacheTime, r.File, key)
|
||||||
return html, 200, nil
|
return html, 200, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package core
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
"github.com/yaoapp/gou/store"
|
"github.com/yaoapp/gou/store"
|
||||||
"github.com/yaoapp/kun/log"
|
"github.com/yaoapp/kun/log"
|
||||||
)
|
)
|
||||||
|
|
@ -17,6 +18,7 @@ type Cache struct {
|
||||||
HTML string
|
HTML string
|
||||||
CacheStore string
|
CacheStore string
|
||||||
CacheTime time.Duration
|
CacheTime time.Duration
|
||||||
|
DataCacheTime time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -92,6 +94,46 @@ func (c *Cache) GetHTML(hash string) (string, bool) {
|
||||||
return v.(string), true
|
return v.(string), true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetData get the data
|
||||||
|
func (c *Cache) GetData(hash string) (Data, bool) {
|
||||||
|
store, has := store.Pools[c.CacheStore]
|
||||||
|
if !has {
|
||||||
|
log.Warn(`[SUI] The cache store "%s" is not found`, c.CacheStore)
|
||||||
|
return Data{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
v, has := store.Get(hash)
|
||||||
|
if !has {
|
||||||
|
return Data{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
data := Data{}
|
||||||
|
err := jsoniter.Unmarshal(v.([]byte), &data)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(`[SUI] The data is not a valid json: %s`, err.Error())
|
||||||
|
return Data{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
return data, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetData set the data
|
||||||
|
func (c *Cache) SetData(hash string, data Data, ttl time.Duration) {
|
||||||
|
store, has := store.Pools[c.CacheStore]
|
||||||
|
if !has {
|
||||||
|
log.Warn(`[SUI] The cache store "%s" is not found`, c.CacheStore)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
raw, err := jsoniter.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(`[SUI] The data is not a valid json: %s`, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
store.Set(hash, raw, ttl)
|
||||||
|
}
|
||||||
|
|
||||||
// SetHTML set the html
|
// SetHTML set the html
|
||||||
func (c *Cache) SetHTML(hash, html string, ttl time.Duration) {
|
func (c *Cache) SetHTML(hash, html string, ttl time.Duration) {
|
||||||
store, has := store.Pools[c.CacheStore]
|
store, has := store.Pools[c.CacheStore]
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,7 @@ func (page *Page) ExportConfig() string {
|
||||||
"guard": page.Config.Guard,
|
"guard": page.Config.Guard,
|
||||||
"cache_store": page.CacheStore,
|
"cache_store": page.CacheStore,
|
||||||
"cache": page.Config.Cache,
|
"cache": page.Config.Cache,
|
||||||
|
"data_cache": page.Config.DataCache,
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -298,6 +298,7 @@ type PageSetting struct {
|
||||||
Guard string `json:"guard,omitempty"`
|
Guard string `json:"guard,omitempty"`
|
||||||
CacheStore string `json:"cache_store,omitempty"`
|
CacheStore string `json:"cache_store,omitempty"`
|
||||||
Cache int `json:"cache,omitempty"`
|
Cache int `json:"cache,omitempty"`
|
||||||
|
DataCache int `json:"data_cache,omitempty"`
|
||||||
Description string `json:"description,omitempty"`
|
Description string `json:"description,omitempty"`
|
||||||
SEO *PageSEO `json:"seo,omitempty"`
|
SEO *PageSEO `json:"seo,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue