Revel provides a Cache library for server-side, temporary, low-latencystorage. It is a good replacement for frequent database access to slowlychanging data. It could also be used for implementing user sessions, if for example the cookie-based sessions are insufficient.
Implementations
The Cache may be configured to be backed by one of the following implementations:
- a list of memcached hosts
- a single redis host
- the ‘in-memory’ implementation. Note the in memory stores the object in memoryand returns the same object when you Get it (if available). This means thatupdating an object retrieved from the cache will update the stored cache object as well.
Expiration
Cache items are set with an expiration time, in one of three forms:
- a time.Duration
- cache.DefaultExpiryTime - the application-wide default expiration time, one hour by default (see cache config)
- cache.ForEverNeverExpiry - will cause the item to never expire
Important: Callers can not rely on items being present in the cache, as the data is not durable, and a cache restart may clear all data.
Serialization
The Cache getters and setters automatically serialize values for callers, toand from any type, with the exception of the inmemory model which stores and returns the object verbatim. It uses the following mechanisms:
- If the value is already of type []byte, the data is not touched
- If the value is of any integer type, it is stored as the ASCII representation
- Otherwise, the value is encoded using encoding/gob
Configuration
Configure the cache using these keys in conf/app.conf
:
- cache.expires
- a string accepted by time.ParseDuration to specify the default expiration duration. (default 1 hour)
- cache.memcached
- a boolean indicating whether or not memcached should be used. (default false)
- cache.redis
- a boolean indicating whether or not redis should be used. (default false)
- cache.hosts
Here’s an example of the common operations. Note that callers may invoke cacheoperations in a new goroutine if they do not require the result of theinvocation to process the request.
import("github.com/revel/revel""github.com/revel/revel/cache")func(cApp)ShowProduct(idstring)revel.Result{varproduct*Productiferr:=cache.Get("product_"+id,&product);err!=nil{product=loadProduct(id)gocache.Set("product_"+id,product,30*time.Minute)}returnc.Render(product)}func(cApp)AddProduct(namestring,priceint)revel.Result{product:=NewProduct(name,price)product.Save()returnc.Redirect("/products/%d",product.id)}func(cApp)EditProduct(id,namestring,priceint)revel.Result{product:=loadProduct(id)product.name=nameproduct.price=pricegocache.Set("product_"+id,product,30*time.Minute)returnc.Redirect("/products/%d",id)}func(cApp)DeleteProduct(idstring)revel.Result{product:=loadProduct(id)product.Delete()gocache.Delete("product_"+id)returnc.Redirect("/products")}
Session usage
The Cache has a global key space. To use it as a session store, callers shouldtake advantage of the session’s UUID, as shown below:
cache.Set(c.Session.Id(),products)// and then in subsequent requestserr:=cache.Get(c.Session.Id(),&products)
GoDoc Reference
GitHub Labels
- Issues: topic-cache
- Pull Requests: topic-cache