本文整理汇总了Golang中github.com/app-kit/go-appkit.Registry.Cache方法的典型用法代码示例。如果您正苦于以下问题:Golang Registry.Cache方法的具体用法?Golang Registry.Cache怎么用?Golang Registry.Cache使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/app-kit/go-appkit.Registry
的用法示例。
在下文中一共展示了Registry.Cache方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: serverRenderer
func serverRenderer(registry kit.Registry, r kit.Request) kit.Response {
url := r.GetHttpRequest().URL
// Build the url to query.
if url.Scheme == "" {
url.Scheme = "http"
}
if url.Host == "" {
url.Host = registry.Config().UString("host", "localhost") + ":" + registry.Config().UString("port", "8000")
}
q := url.Query()
q.Set("no-server-render", "1")
url.RawQuery = q.Encode()
strUrl := url.String()
cacheKey := "serverrenderer_" + strUrl
cacheName := registry.Config().UString("serverRenderer.cache")
var cache kit.Cache
// If a cache is specified, try to retrieve it.
if cacheName != "" {
cache = registry.Cache(cacheName)
if cache == nil {
registry.Logger().Errorf("serverRenderer.cache is set to %v, but the cache is not registered with app", cacheName)
}
}
// If a cache was found, try to retrieve cached response.
if cache != nil {
item, err := cache.Get(cacheKey)
if err != nil {
registry.Logger().Errorf("serverRenderer: cache retrieval error: %v", err)
} else if item != nil {
// Cache item found, return response with cache item.
status, _ := strconv.ParseInt(item.GetTags()[0], 10, 64)
data, _ := item.ToString()
return &kit.AppResponse{
HttpStatus: int(status),
RawData: []byte(data),
}
}
}
// Either no cache or url not yet cached, so render it.
// First, ensure that the tmp directory exists.
tmpDir := path.Join(registry.Config().TmpDir(), "phantom")
if ok, _ := utils.FileExists(tmpDir); !ok {
if err := os.MkdirAll(tmpDir, 0777); err != nil {
return &kit.AppResponse{
Error: &apperror.Err{
Code: "create_tmp_dir_failed",
Message: fmt.Sprintf("Could not create the tmp directory at %v: %v", tmpDir, err),
},
}
}
}
// Build a unique file name.
filePath := path.Join(tmpDir, utils.UUIdv4()+".html")
// Execute phantom js.
// Find path of phantom script.
_, filename, _, _ := runtime.Caller(1)
scriptPath := path.Join(path.Dir(path.Dir(filename)), "phantom", "render.js")
start := time.Now()
phantomPath := registry.Config().UString("serverRenderer.phantomJsPath", "phantomjs")
args := []string{
"--web-security=false",
"--local-to-remote-url-access=true",
scriptPath,
"10",
strUrl,
filePath,
}
result, err := exec.Command(phantomPath, args...).CombinedOutput()
if err != nil {
registry.Logger().Errorf("Phantomjs execution error: %v", string(result))
return &kit.AppResponse{
Error: apperror.Wrap(err, "phantom_execution_failed"),
}
}
// Get time taken as milliseconds.
timeTaken := int(time.Now().Sub(start) / time.Millisecond)
registry.Logger().WithFields(log.Fields{
"action": "phantomjs_render",
"milliseconds": timeTaken,
}).Debugf("Rendered url %v with phantomjs", url)
content, err2 := utils.ReadFile(filePath)
if err2 != nil {
//.........这里部分代码省略.........