Skip to content

Instantly share code, notes, and snippets.

@TripleDogDare
Last active December 23, 2015 05:49
Show Gist options
  • Save TripleDogDare/6589491 to your computer and use it in GitHub Desktop.
Save TripleDogDare/6589491 to your computer and use it in GitHub Desktop.
Revel Cache Viewer creates a text revel.Result that displays the cache in a readable format if it is the "InMemoryCache" (not using memchached).
package controllers
import (
"encoding/gob"
"fmt"
"github.com/robfig/revel"
"github.com/robfig/revel/cache"
"os"
"path/filepath"
"time"
)
type Dev struct {
*revel.Controller
}
type item struct {
Object interface{}
Expiration *time.Time
}
// test function
func (c Dev) Cache() revel.Result {
var myType string
var test int
cache.Get("test", &test)
test++
cache.Set("test", test, time.Duration(30*time.Second))
path := filepath.Join(revel.BasePath, "files", "cache")
switch i := cache.Instance.(type) {
case cache.InMemoryCache:
myType = "InMemoryCache"
i.Cache.SaveFile(path)
default:
return c.RenderText("Type: Unknown")
}
fd, err := os.Open(path)
defer fd.Close()
if err != nil {
return c.RenderText(err.Error())
}
dec := gob.NewDecoder(fd)
items := map[string]*item{}
err = dec.Decode(&items)
var result string
if err != nil {
return c.RenderText(err.Error())
}
for k, v := range items {
result = fmt.Sprintf("%s [%s]\n Expiration = %+v\n Object = %+v\n\n", result, k, v.Expiration, v.Object)
}
return c.RenderText(
"Type: %s\n\n"+
"Decode:\n%v\n\n"+
"Results:\n%v\n",
myType, items, result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment