2017-08-04 08:20:59 +00:00
|
|
|
package hotime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
2018-04-09 17:16:24 +00:00
|
|
|
"strings"
|
2017-08-04 08:20:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CacheDb struct {
|
|
|
|
Time int64
|
|
|
|
Db *HoTimeDB
|
2017-08-23 07:35:49 +00:00
|
|
|
contextBase
|
2017-08-04 08:20:59 +00:00
|
|
|
isInit bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *CacheDb) initDbTable() {
|
|
|
|
if this.isInit {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dbNames := this.Db.Query("SELECT DATABASE()")
|
|
|
|
|
|
|
|
if len(dbNames) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dbName := dbNames[0].GetString("DATABASE()")
|
|
|
|
res := this.Db.Query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='" + dbName + "' AND TABLE_NAME='cached'")
|
|
|
|
if len(res) == 0 {
|
|
|
|
this.Db.Exec("CREATE TABLE `cached` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `ckey` varchar(60) DEFAULT NULL, `cvalue` varchar(2000) DEFAULT NULL, `time` bigint(20) DEFAULT NULL, `endtime` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=198740 DEFAULT CHARSET=utf8")
|
|
|
|
}
|
|
|
|
this.isInit = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//获取Cache键只能为string类型
|
|
|
|
func (this *CacheDb) get(key string) interface{} {
|
|
|
|
|
|
|
|
cached := this.Db.Get("cached", "*", Map{"ckey": key})
|
|
|
|
|
|
|
|
if cached == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
//data:=cacheMap[key];
|
|
|
|
if cached.GetInt64("endtime") <= time.Now().Unix() {
|
|
|
|
|
|
|
|
this.Db.Delete("cached", Map{"id": cached.GetString("id")})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
data := Map{}
|
|
|
|
data.JsonToMap(cached.GetString("cvalue"))
|
|
|
|
|
|
|
|
return data.Get("data")
|
|
|
|
}
|
|
|
|
|
|
|
|
//key value ,时间为时间戳
|
|
|
|
func (this *CacheDb) set(key string, value interface{}, tim int64) {
|
|
|
|
|
|
|
|
bte, _ := json.Marshal(Map{"data": value})
|
|
|
|
|
|
|
|
num := this.Db.Update("cached", Map{"cvalue": string(bte), "time": time.Now().UnixNano(), "endtime": tim}, Map{"ckey": key})
|
|
|
|
if num == int64(0) {
|
|
|
|
this.Db.Insert("cached", Map{"cvalue": string(bte), "time": time.Now().UnixNano(), "endtime": tim, "ckey": key})
|
|
|
|
}
|
|
|
|
|
|
|
|
//随机执行删除命令
|
|
|
|
if Rand(1000) > 950 {
|
|
|
|
this.Db.Delete("cached", Map{"endtime[<]": time.Now().Unix()})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *CacheDb) delete(key string) {
|
2018-04-09 17:16:24 +00:00
|
|
|
|
|
|
|
del:=strings.Index(key,"*")
|
|
|
|
//如果通配删除
|
|
|
|
if del!=-1{
|
|
|
|
key=Substr(key,0,del)
|
|
|
|
this.Db.Delete("cached", Map{"ckey": key+"%"})
|
|
|
|
|
|
|
|
}else{
|
|
|
|
this.Db.Delete("cached", Map{"ckey": key})
|
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *CacheDb) Cache(key string, data ...interface{}) *Obj {
|
|
|
|
|
|
|
|
this.initDbTable()
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
return &Obj{Data: this.get(key)}
|
|
|
|
}
|
|
|
|
tim := time.Now().Unix()
|
|
|
|
|
|
|
|
if len(data) == 1 && data[0] == nil {
|
|
|
|
this.delete(key)
|
|
|
|
return &Obj{Data: nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) == 1 {
|
|
|
|
if this.Time == 0 {
|
|
|
|
this.Time = Config.GetInt64("cacheLongTime")
|
|
|
|
}
|
|
|
|
tim += this.Time
|
|
|
|
}
|
|
|
|
if len(data) == 2 {
|
|
|
|
this.SetError(nil)
|
|
|
|
tempt := ObjToInt64(data[1], &this.Error)
|
|
|
|
|
|
|
|
if this.GetError() == nil {
|
|
|
|
|
|
|
|
tim = tim + tempt
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.set(key, data[0], tim)
|
|
|
|
return &Obj{Data: nil}
|
|
|
|
}
|