Table of Contents


We’ve built a small Gin example here: MemCachier Gin sample app.
Related tutorials:

In Gin you can use the standard mc interface to get and set values as described in our Go documentation to cache results of expensive computations or database queries:

In addition there are two Gin specific ways to use Memcache:

  1. Cache rendered views
  2. Store sessions

Cache rendered views

To cache rendered views you need the gin-contrib/cache library. Now you can use the CachePage middleware like so:

Whenever the view changes, e.g., when the content changes, you need to make sure to invalidate the cached view so it will be re-rendered. This can be done by deleting the cached item (for the root route in this case):

Storing Sessions in Memcache

Memcache works well for sessions that time out, however, since Memcache is a cache and thus not persistent, saving long-lived sessions in Memcache might not be ideal. For long-lived sessions consider a permanent storage option such as your database.

To use sessions in Gin you need gin-contrib/session. You can easily add it to your Gin app like so: