Table of Contents

Gin

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:

package main

import (
  "os"
  "fmt"
  "github.com/memcachier/mc"
)

func main() {
  username := os.Getenv("MEMCACHIER_USERNAME")
  password := os.Getenv("MEMCACHIER_PASSWORD")
  servers := os.Getenv("MEMCACHIER_SERVERS")

  mcClient := mc.NewMC(servers, username, password)
  defer mcClient.Quit()

  _, err := mcClient.set("foo", "bar", 0, 0, 0)
  if err != nil {
    fmt.Printf("Failed to set value: %s\n", err)
  }

  val, _, _, err := c.Get("foo")
  if err != nil {
    fmt.Printf("Failed to fetch value: %s\n", err)
  }
  fmt.Printf("Got value: %s\n", val)
}

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:

package main

import (
  "os"
  "github.com/gin-gonic/gin"
  "github.com/gin-contrib/cache"
  "github.com/gin-contrib/cache/persistence"
)

func main() {
	username := os.Getenv("MEMCACHIER_USERNAME")
  password := os.Getenv("MEMCACHIER_PASSWORD")
  servers := os.Getenv("MEMCACHIER_SERVERS")

  mcStore := persistence.NewMemcachedBinaryStore(servers, username, password, persistence.FOREVER)

	router := gin.New()
  router.GET("/", cache.CachePage(mcStore, persistence.DEFAULT, func(c *gin.Context) {
    // ...
  }))
}

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):

mcStore.Delete(cache.CreateKey("/"))

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:

package main

import (
  "os"
  "github.com/memcachier/mc"
  "github.com/gin-contrib/sessions"
  "github.com/gin-contrib/sessions/memcached"
)

func main() {
	username := os.Getenv("MEMCACHIER_USERNAME")
  password := os.Getenv("MEMCACHIER_PASSWORD")
  servers := os.Getenv("MEMCACHIER_SERVERS")

  mcClient := mc.NewMC(servers, username, password)
  defer mcClient.Quit()

	router := gin.New()
  sessionStore := memcached.NewMemcacheStore(mcClient, "", []byte("secret"))
  router.Use(sessions.Sessions("mysession", sessionStore))
}