New caching mechanism

This commit is contained in:
Eduard Urbach 2016-10-23 13:15:47 +09:00
parent 2386b4cb92
commit 44dc4f000e
2 changed files with 70 additions and 15 deletions

42
main.go
View File

@ -1,17 +1,21 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/Joker/jade" "github.com/Joker/jade"
"github.com/OneOfOne/xxhash"
"github.com/aerojs/aero" "github.com/aerojs/aero"
"github.com/blitzprog/arn" "github.com/blitzprog/arn"
"github.com/buaazp/fasthttprouter" "github.com/buaazp/fasthttprouter"
cache "github.com/patrickmn/go-cache"
"github.com/robertkrimen/otto" "github.com/robertkrimen/otto"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"github.com/valyala/fasttemplate" "github.com/valyala/fasttemplate"
@ -26,20 +30,44 @@ const (
hello = "Hello World" hello = "Hello World"
) )
var jsonToResponse *cache.Cache
func worker(script *otto.Script, jobs <-chan map[string]interface{}, results chan<- string) { func worker(script *otto.Script, jobs <-chan map[string]interface{}, results chan<- string) {
vm := otto.New() vm := otto.New()
for properties := range jobs { for properties := range jobs {
h := xxhash.NewS64(0)
for _, value := range properties {
jsonBytes, err := json.Marshal(value)
if err == nil {
h.Write(jsonBytes)
// fmt.Println(string(buffer))
}
}
hash := strconv.FormatUint(h.Sum64(), 10)
cachedResponse, found := jsonToResponse.Get(hash)
if found {
results <- cachedResponse.(string)
} else {
for key, value := range properties { for key, value := range properties {
vm.Set(key, value) vm.Set(key, value)
} }
result, _ := vm.Run(script) result, _ := vm.Run(script)
code, _ := result.ToString() code, _ := result.ToString()
results <- code results <- code
jsonToResponse.Set(hash, code, cache.DefaultExpiration)
}
} }
} }
func main() { func main() {
jsonToResponse = cache.New(5*time.Minute, 1*time.Minute)
app := aero.New() app := aero.New()
jade.PrettyOutput = false jade.PrettyOutput = false
code, _ := jade.ParseFile("pages/anime/anime.pug") code, _ := jade.ParseFile("pages/anime/anime.pug")
@ -103,20 +131,14 @@ func main() {
return return
} }
myMap := make(map[string]interface{}) templateParams := make(map[string]interface{})
myMap["anime"] = anime templateParams["anime"] = anime
jobs <- myMap jobs <- templateParams
aero.Respond(ctx, <-results) aero.Respond(ctx, <-results)
// if runErr != nil {
// panic(runErr)
// }
// aero.Respond(ctx, result.String())
}) })
fmt.Println("Starting server on http://localhost:5000/") fmt.Println("Starting server on http://localhost:4000/")
app.Run() app.Run()
} }

33
main_test.go Normal file
View File

@ -0,0 +1,33 @@
package main
import (
"testing"
"unsafe"
)
var code = "Hello World"
var slice = []byte("Hello World 2")
func BenchmarkStringToBytesSafe(b *testing.B) {
for i := 0; i < b.N; i++ {
slice = []byte(code)
}
}
func BenchmarkStringToBytesUnsafe(b *testing.B) {
for i := 0; i < b.N; i++ {
slice = *(*[]byte)(unsafe.Pointer(&code))
}
}
func BenchmarkBytesToStringSafe(b *testing.B) {
for i := 0; i < b.N; i++ {
code = string(slice)
}
}
func BenchmarkBytesToStringUnsafe(b *testing.B) {
for i := 0; i < b.N; i++ {
code = *(*string)(unsafe.Pointer(&slice))
}
}