Skip to content

Commit

Permalink
Add cache module with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Raggaer committed Aug 1, 2018
1 parent be9b56d commit dc58643
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/controllers/controller.go
Expand Up @@ -77,6 +77,7 @@ func (h *Handler) MainRoute(ctx *fasthttp.RequestCtx) {
lua.NewConfigModule(h.Config.Custom),
lua.NewTemplateModule(h.Tpl, ctx),
lua.NewURLModule(),
lua.NewCacheModule(h.Cache),
})
defer state.Close()

Expand Down
58 changes: 58 additions & 0 deletions app/lua/cache.go
@@ -0,0 +1,58 @@
package lua

import (
"time"

"github.com/patrickmn/go-cache"
glua "github.com/tul/gopher-lua"
)

// CacheModule provices access to the memory cache object
type CacheModule struct {
Cache *cache.Cache
}

// NewCacheModule returns a new cache module
func NewCacheModule(c *cache.Cache) *Module {
module := &CacheModule{
Cache: c,
}
return &Module{
Name: "cache",
Data: module,
Funcs: map[string]glua.LGFunction{
"get": module.Get,
"set": module.Set,
},
}
}

// Get retrieves a cache value
func (c *CacheModule) Get(state *glua.LState) int {
key := state.ToString(1)
v, ok := c.Cache.Get(key)
if !ok {
state.Push(glua.LNil)
return 1
}
state.Push(GoValueToLua(v))
return 1
}

// Set sets a cache value
func (c *CacheModule) Set(state *glua.LState) int {
key := state.ToString(1)
val := LuaValueToGo(state.Get(2))
dur := state.Get(3)
if dur.Type() == glua.LTString {
d, err := time.ParseDuration(state.ToString(3))
if err != nil {
state.RaiseError("Unable to parse cache duration - %v", err)
return 0
}
c.Cache.Set(key, val, d)
return 0
}
c.Cache.Set(key, val, time.Minute*5)
return 0
}
55 changes: 55 additions & 0 deletions test/cache_test.go
@@ -0,0 +1,55 @@
package test

import (
"fmt"
"io/ioutil"
"net/http"
"testing"
)

// TestCacheSet test the cache module set function

func TestCacheSet(t *testing.T) {
port := make(chan int, 1)
defer createTestServer(port, t).Close()
addr := <-port
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/cache/set", addr))
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("Wrong cache.set status code. Expected '200' but got '%d'", resp.StatusCode)
}
}

// TestCacheGet test the cache module get function
func TestCacheGet(t *testing.T) {
port := make(chan int, 1)
defer createTestServer(port, t).Close()

// First set a cache value
addr := <-port
resps, err := http.Get(fmt.Sprintf("http://localhost:%d/cache/set", addr))
if err != nil {
t.Fatal(err)
}
defer resps.Body.Close()
if resps.StatusCode != 200 {
t.Fatalf("Wrong cache.set status code. Expected '200' but got '%d'", resps.StatusCode)
}

// Get a cache value
respg, err := http.Get(fmt.Sprintf("http://localhost:%d/cache/get", addr))
if err != nil {
t.Fatal(err)
}
defer respg.Body.Close()
bodyContent, err := ioutil.ReadAll(respg.Body)
if err != nil {
t.Fatal(err)
}
if string(bodyContent) != "Testing cache.set function" {
t.Fatalf("Wrong cache.get value. Expected 'Testing cache.set function' but got '%s'", string(bodyContent))
}
}
3 changes: 3 additions & 0 deletions test/controllers/cache/get.lua
@@ -0,0 +1,3 @@
local cache = require('cache')
local req = require('http')
req.write(cache.get("bison-test"))
2 changes: 2 additions & 0 deletions test/controllers/cache/set.lua
@@ -0,0 +1,2 @@
local cache = require('cache')
cache.set("bison-test", "Testing cache.set function", "5m")
6 changes: 6 additions & 0 deletions test/router/router.lua
Expand Up @@ -43,6 +43,12 @@ local router = {
},
['/url/path_unescape'] = {
get = 'url/path_unescape.lua'
},
['/cache/set'] = {
get = 'cache/set.lua'
},
['/cache/get'] = {
get = 'cache/get.lua'
}
}

Expand Down
3 changes: 3 additions & 0 deletions test/suite.go
Expand Up @@ -7,8 +7,10 @@ import (
"net/http"
"path/filepath"
"testing"
"time"

"github.com/buaazp/fasthttprouter"
"github.com/patrickmn/go-cache"
"github.com/raggaer/bison/app/config"
"github.com/raggaer/bison/app/controllers"
"github.com/raggaer/bison/app/lua"
Expand Down Expand Up @@ -54,6 +56,7 @@ func createTestServer(p chan<- int, t *testing.T) io.Closer {
Routes: routes,
Files: files,
Tpl: tpl,
Cache: cache.New(time.Minute*5, time.Minute*10),
}

for _, rx := range routes {
Expand Down

0 comments on commit dc58643

Please sign in to comment.