hotime/cache/cache_db.go

174 lines
3.9 KiB
Go
Raw Normal View History

2021-05-23 23:27:41 +00:00
package cache
2017-08-04 08:20:59 +00:00
import (
2022-03-12 17:12:29 +00:00
. "code.hoteas.com/golang/hotime/common"
2021-05-23 23:27:41 +00:00
"database/sql"
2017-08-04 08:20:59 +00:00
"encoding/json"
2018-04-09 17:16:24 +00:00
"strings"
2019-11-10 10:00:45 +00:00
"time"
2017-08-04 08:20:59 +00:00
)
2021-05-23 23:27:41 +00:00
type HoTimeDBInterface interface {
2021-05-28 16:37:20 +00:00
GetPrefix() string
2021-05-23 23:27:41 +00:00
Query(query string, args ...interface{}) []Map
2021-05-25 11:53:34 +00:00
Exec(query string, args ...interface{}) (sql.Result, *Error)
2021-05-23 23:27:41 +00:00
Get(table string, qu ...interface{}) Map
Select(table string, qu ...interface{}) []Map
Delete(table string, data map[string]interface{}) int64
Update(table string, data Map, where Map) int64
Insert(table string, data map[string]interface{}) int64
GetType() string
}
2017-08-04 08:20:59 +00:00
type CacheDb struct {
TimeOut int64
DbSet bool
SessionSet bool
Db HoTimeDBInterface
*Error
2021-05-23 23:27:41 +00:00
ContextBase
2017-08-04 08:20:59 +00:00
isInit bool
}
2022-03-12 17:48:54 +00:00
func (that *CacheDb) GetError() *Error {
2022-03-12 17:48:54 +00:00
return that.Error
}
2022-03-12 17:48:54 +00:00
func (that *CacheDb) SetError(err *Error) {
that.Error = err
}
2021-05-25 12:27:24 +00:00
func (that *CacheDb) initDbTable() {
if that.isInit {
2017-08-04 08:20:59 +00:00
return
}
2021-05-25 12:27:24 +00:00
if that.Db.GetType() == "mysql" {
2017-08-04 08:20:59 +00:00
2021-05-25 12:27:24 +00:00
dbNames := that.Db.Query("SELECT DATABASE()")
2019-11-10 10:00:45 +00:00
if len(dbNames) == 0 {
return
}
dbName := dbNames[0].GetString("DATABASE()")
2021-05-28 16:37:20 +00:00
res := that.Db.Query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='" + dbName + "' AND TABLE_NAME='" + that.Db.GetPrefix() + "cached'")
2019-11-10 10:00:45 +00:00
if len(res) != 0 {
2021-05-25 12:27:24 +00:00
that.isInit = true
2019-11-10 10:00:45 +00:00
return
}
2022-05-02 07:42:54 +00:00
_, e := that.Db.Exec("CREATE TABLE `" + that.Db.GetPrefix() + "cached` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `key` varchar(60) DEFAULT NULL, `value` varchar(2000) DEFAULT NULL, `time` bigint(20) DEFAULT NULL, `endtime` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=198740 DEFAULT CHARSET=utf8")
2019-11-10 10:00:45 +00:00
if e.GetError() == nil {
2021-05-25 12:27:24 +00:00
that.isInit = true
2019-11-10 10:00:45 +00:00
}
2017-08-04 08:20:59 +00:00
}
2019-11-10 10:00:45 +00:00
2021-05-25 12:27:24 +00:00
if that.Db.GetType() == "sqlite" {
2021-05-28 16:37:20 +00:00
res := that.Db.Query(`select * from sqlite_master where type = 'table' and name = '` + that.Db.GetPrefix() + `cached'`)
2019-11-10 10:00:45 +00:00
if len(res) != 0 {
2021-05-25 12:27:24 +00:00
that.isInit = true
2019-11-10 10:00:45 +00:00
return
}
2021-05-28 16:37:20 +00:00
_, e := that.Db.Exec(`CREATE TABLE "` + that.Db.GetPrefix() + `cached" (
2019-11-10 10:00:45 +00:00
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
2022-05-02 07:42:54 +00:00
"key" TEXT(60),
"value" TEXT(2000),
2019-11-10 10:00:45 +00:00
"time" integer,
"endtime" integer
);`)
if e.GetError() == nil {
2021-05-25 12:27:24 +00:00
that.isInit = true
2019-11-10 10:00:45 +00:00
}
2017-08-04 08:20:59 +00:00
}
}
//获取Cache键只能为string类型
2021-05-25 12:27:24 +00:00
func (that *CacheDb) get(key string) interface{} {
2017-08-04 08:20:59 +00:00
2022-05-02 07:42:54 +00:00
cached := that.Db.Get("cached", "*", Map{"key": key})
2017-08-04 08:20:59 +00:00
if cached == nil {
return nil
}
//data:=cacheMap[key];
if cached.GetInt64("endtime") <= time.Now().Unix() {
2021-05-25 12:27:24 +00:00
that.Db.Delete("cached", Map{"id": cached.GetString("id")})
2017-08-04 08:20:59 +00:00
return nil
}
data := Map{}
2022-05-02 07:42:54 +00:00
data.JsonToMap(cached.GetString("value"))
2017-08-04 08:20:59 +00:00
return data.Get("data")
}
//key value ,时间为时间戳
2021-05-25 12:27:24 +00:00
func (that *CacheDb) set(key string, value interface{}, tim int64) {
2017-08-04 08:20:59 +00:00
bte, _ := json.Marshal(Map{"data": value})
2022-05-02 07:42:54 +00:00
num := that.Db.Update("cached", Map{"value": string(bte), "time": time.Now().UnixNano(), "endtime": tim}, Map{"key": key})
2017-08-04 08:20:59 +00:00
if num == int64(0) {
2022-05-02 07:42:54 +00:00
that.Db.Insert("cached", Map{"value": string(bte), "time": time.Now().UnixNano(), "endtime": tim, "key": key})
2017-08-04 08:20:59 +00:00
}
//随机执行删除命令
if Rand(1000) > 950 {
2021-05-25 12:27:24 +00:00
that.Db.Delete("cached", Map{"endtime[<]": time.Now().Unix()})
2017-08-04 08:20:59 +00:00
}
}
2021-05-25 12:27:24 +00:00
func (that *CacheDb) delete(key string) {
2018-04-09 17:16:24 +00:00
2019-11-10 10:00:45 +00:00
del := strings.Index(key, "*")
2018-04-09 17:16:24 +00:00
//如果通配删除
2019-11-10 10:00:45 +00:00
if del != -1 {
key = Substr(key, 0, del)
2022-05-02 07:42:54 +00:00
that.Db.Delete("cached", Map{"key": key + "%"})
2018-04-09 17:16:24 +00:00
2019-11-10 10:00:45 +00:00
} else {
2022-05-02 07:42:54 +00:00
that.Db.Delete("cached", Map{"key": key})
2018-04-09 17:16:24 +00:00
}
2017-08-04 08:20:59 +00:00
}
2021-05-25 12:27:24 +00:00
func (that *CacheDb) Cache(key string, data ...interface{}) *Obj {
2017-08-04 08:20:59 +00:00
2021-05-25 12:27:24 +00:00
that.initDbTable()
2017-08-04 08:20:59 +00:00
if len(data) == 0 {
2021-05-25 12:27:24 +00:00
return &Obj{Data: that.get(key)}
2017-08-04 08:20:59 +00:00
}
tim := time.Now().Unix()
if len(data) == 1 && data[0] == nil {
2021-05-25 12:27:24 +00:00
that.delete(key)
2017-08-04 08:20:59 +00:00
return &Obj{Data: nil}
}
if len(data) == 1 {
2021-05-28 15:48:33 +00:00
if that.TimeOut == 0 {
2021-05-25 12:27:24 +00:00
//that.Time = Config.GetInt64("cacheLongTime")
2017-08-04 08:20:59 +00:00
}
2021-05-28 15:48:33 +00:00
tim += that.TimeOut
2017-08-04 08:20:59 +00:00
}
if len(data) == 2 {
2021-05-25 12:27:24 +00:00
that.SetError(nil)
tempt := ObjToInt64(data[1], that.Error)
2017-08-04 08:20:59 +00:00
2018-06-13 11:10:13 +00:00
if tempt > tim {
tim = tempt
2021-05-25 12:27:24 +00:00
} else if that.GetError() == nil {
2017-08-04 08:20:59 +00:00
tim = tim + tempt
}
}
2021-05-25 12:27:24 +00:00
that.set(key, data[0], tim)
2017-08-04 08:20:59 +00:00
return &Obj{Data: nil}
}