1
0
mirror of https://gitea.com/xorm/cachestore synced 2025-10-06 00:02:51 +02:00
Files
xorm-cachestore/example/main.go
Wenhui Shen f618b52b9c update
2015-10-14 21:13:46 +08:00

53 lines
1.1 KiB
Go

package main
import (
"log"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/cachestore"
"github.com/go-xorm/xorm"
)
var (
cacher *xorm.LRUCacher
CacheDir string = "."
cfg []string = []string{"leveldb"}
)
func main() {
//cfg := strings.SplitN(cacherName, ":", 2)
engine, err := xorm.NewEngine("mysql", "root:root@/coscms?charset=utf8")
if err != nil {
log.Fatalf("The database connection failed: %v\n", err)
}
switch strings.ToLower(cfg[0]) {
case "memory":
ccStore := xorm.NewMemoryStore()
cacher = xorm.NewLRUCacher(ccStore, 1000)
case "leveldb":
storagePath := CacheDir + "/leveldb/dbcache"
if len(cfg) == 2 {
storagePath = cfg[1]
}
ccStore := cachestore.NewLevelDBStore(storagePath)
cacher = xorm.NewLRUCacher(ccStore, 999999999)
case "memcache":
conn := "127.0.0.1:11211"
if len(cfg) == 2 {
conn = cfg[1]
}
ccStore := cachestore.NewMemCache(strings.Split(conn, ";"))
cacher = xorm.NewLRUCacher(ccStore, 999999999)
}
if cacher != nil {
cacher.Expired = 86400 * time.Second
engine.SetDefaultCacher(cacher)
}
//engine.Where(querystring, ...)
}