How to do it…

  1. Install the github.com/astaxie/beego/cache package using the go get command, as follows:
$ go get github.com/astaxie/beego/cache
  1. Move to $GOPATH/src/my-first-beego-project/controllers and create cachecontroller.go, where we will define the GetFromCache handler, which will get the value for a key from a cache and write it to an HTTP response, as follows:
package controllers
import
(
"fmt"
"time"
"github.com/astaxie/beego"
"github.com/astaxie/beego/cache"
)
type CacheController struct
{
beego.Controller
}
var beegoCache cache.Cache
var err error
func init()
{
beegoCache, err = cache.NewCache("memory",
`{"interval":60}`)
beegoCache.Put("foo", "bar", 100000*time.Second)
}
func (this *CacheController) GetFromCache()
{
foo := beegoCache.Get("foo")
this.Ctx.WriteString("Hello " + fmt.Sprintf("%v", foo))
}
  1. Move to $GOPATH/src/my-first-beego-project/routers and edit router.go to add the GET mapping /getFromCache, which will be handled by the GetFromCache handler defined in a CacheController, as follows:
package routers
import
(
"my-first-beego-project/controllers"
"my-first-beego-project/filters"
"github.com/astaxie/beego"
)
func init()
{
beego.Router("/", &controllers.MainController{})
...
beego.Router("/getFromCache", &controllers.
CacheController{}, "get:GetFromCache")
}
  1. Run the program using the following command:
$ bee run
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.181.57