Revel provides a Cache library for server-side, temporary, low-latency storage. It is a good replacement for frequent database access to slowly changing 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

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, to and from any type. 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
    • a comma separated list of hosts to use as backends. If the backend is Redis, then only the first host in this list will be used.

Cache Example

Here’s an example of the common operations. Note that callers may invoke cache operations in a new goroutine if they do not require the result of the invocation to process the request.

import (
	"github.com/revel/revel"
	"github.com/revel/revel/cache"
)

func (c App) ShowProduct(id string) revel.Result {
	var product *Product 
	if err := cache.Get("product_"+id, &product); err != nil {
	    product = loadProduct(id)
	    go cache.Set("product_"+id, product, 30*time.Minute)
	}
	return c.Render(product)
}

func (c App) AddProduct(name string, price int) revel.Result {
	product := NewProduct(name, price)
	product.Save()
	return c.Redirect("/products/%d", product.id)
}

func (c App) EditProduct(id, name string, price int) revel.Result {
	product := loadProduct(id)
	product.name = name
	product.price = price
	go cache.Set("product_"+id, product, 30*time.Minute)
	return c.Redirect("/products/%d", id)
}

func (c App) DeleteProduct(id string) revel.Result {
	product := loadProduct(id)
	product.Delete()
	go cache.Delete("product_"+id)
	return c.Redirect("/products")
}

Session usage

The Cache has a global key space. To use it as a session store, callers should take advantage of the session’s UUID, as shown below:

cache.Set(c.Session.Id(), products)

// and then in subsequent requests
err := cache.Get(c.Session.Id(), &products)
GoDoc Reference
GitHub Labels