整个包结构重构
This commit is contained in:
Vendored
+31
@@ -0,0 +1,31 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
. "../common"
|
||||
)
|
||||
|
||||
type HoTimeCache struct {
|
||||
|
||||
//set(key string, value interface{}, time int64)
|
||||
//get(key string) interface{}
|
||||
//delete(key string)
|
||||
dbCache CacheIns
|
||||
redisCache CacheIns
|
||||
memoryCache CacheIns
|
||||
}
|
||||
|
||||
func (this *HoTimeCache) Session(key string, data ...interface{}) *Obj {
|
||||
return nil
|
||||
}
|
||||
func (this *HoTimeCache) Db(key string, data ...interface{}) *Obj {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *HoTimeCache) Cache(key string, data ...interface{}) *Obj {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *HoTimeCache) getIndex() []CacheIns {
|
||||
|
||||
return nil
|
||||
}
|
||||
Vendored
+159
@@ -0,0 +1,159 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
. "../common"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type HoTimeDBInterface interface {
|
||||
Query(query string, args ...interface{}) []Map
|
||||
Exec(query string, args ...interface{}) (sql.Result, Error)
|
||||
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
|
||||
}
|
||||
|
||||
type CacheDb struct {
|
||||
Time int64
|
||||
Db HoTimeDBInterface
|
||||
ContextBase
|
||||
isInit bool
|
||||
}
|
||||
|
||||
func (this *CacheDb) initDbTable() {
|
||||
if this.isInit {
|
||||
return
|
||||
}
|
||||
if this.Db.GetType() == "mysql" {
|
||||
|
||||
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.isInit = true
|
||||
return
|
||||
}
|
||||
|
||||
_, e := 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` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=198740 DEFAULT CHARSET=utf8")
|
||||
if e.GetError() == nil {
|
||||
this.isInit = true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if this.Db.GetType() == "sqlite" {
|
||||
res := this.Db.Query(`select * from sqlite_master where type = 'table' and name = 'cached'`)
|
||||
|
||||
if len(res) != 0 {
|
||||
this.isInit = true
|
||||
return
|
||||
}
|
||||
_, e := this.Db.Exec(`CREATE TABLE "cached" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"ckey" TEXT(60),
|
||||
"cvalue" TEXT(2000),
|
||||
"time" integer,
|
||||
"endtime" integer
|
||||
);`)
|
||||
if e.GetError() == nil {
|
||||
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) {
|
||||
|
||||
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})
|
||||
}
|
||||
}
|
||||
|
||||
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 tempt > tim {
|
||||
tim = tempt
|
||||
} else if this.GetError() == nil {
|
||||
|
||||
tim = tim + tempt
|
||||
}
|
||||
}
|
||||
this.set(key, data[0], tim)
|
||||
return &Obj{Data: nil}
|
||||
}
|
||||
Vendored
+147
@@ -0,0 +1,147 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
. "../common"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CacheMemory struct {
|
||||
Time int64
|
||||
Map
|
||||
ContextBase
|
||||
mutex *sync.RWMutex
|
||||
}
|
||||
|
||||
//获取Cache键只能为string类型
|
||||
func (this *CacheMemory) get(key string) interface{} {
|
||||
this.Error.SetError(nil)
|
||||
if this.Map == nil {
|
||||
this.Map = Map{}
|
||||
}
|
||||
|
||||
if this.Map[key] == nil {
|
||||
return nil
|
||||
}
|
||||
data := this.Map.Get(key, &this.Error).(cacheData)
|
||||
if this.GetError() != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if data.time < time.Now().Unix() {
|
||||
delete(this.Map, key)
|
||||
return nil
|
||||
}
|
||||
return data.data
|
||||
}
|
||||
|
||||
func (this *CacheMemory) refreshMap() {
|
||||
|
||||
go func() {
|
||||
this.mutex.Lock()
|
||||
defer this.mutex.Unlock()
|
||||
for key, v := range this.Map {
|
||||
data := v.(cacheData)
|
||||
if data.time <= time.Now().Unix() {
|
||||
delete(this.Map, key)
|
||||
}
|
||||
}
|
||||
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
//key value ,时间为时间戳
|
||||
func (this *CacheMemory) set(key string, value interface{}, time int64) {
|
||||
this.Error.SetError(nil)
|
||||
var data cacheData
|
||||
|
||||
if this.Map == nil {
|
||||
this.Map = Map{}
|
||||
}
|
||||
|
||||
dd := this.Map[key]
|
||||
|
||||
if dd == nil {
|
||||
data = cacheData{}
|
||||
} else {
|
||||
data = dd.(cacheData)
|
||||
}
|
||||
|
||||
data.time = time
|
||||
data.data = value
|
||||
|
||||
this.Map.Put(key, data)
|
||||
}
|
||||
|
||||
func (this *CacheMemory) delete(key string) {
|
||||
del := strings.Index(key, "*")
|
||||
//如果通配删除
|
||||
if del != -1 {
|
||||
key = Substr(key, 0, del)
|
||||
for k, _ := range this.Map {
|
||||
if strings.Index(k, key) != -1 {
|
||||
delete(this.Map, key)
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
delete(this.Map, key)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (this *CacheMemory) Cache(key string, data ...interface{}) *Obj {
|
||||
|
||||
x := RandX(1, 100000)
|
||||
if x > 99950 {
|
||||
this.refreshMap()
|
||||
}
|
||||
if this.mutex == nil {
|
||||
this.mutex = &sync.RWMutex{}
|
||||
}
|
||||
|
||||
reData := &Obj{Data: nil}
|
||||
|
||||
if len(data) == 0 {
|
||||
this.mutex.RLock()
|
||||
reData.Data = this.get(key)
|
||||
this.mutex.RUnlock()
|
||||
return reData
|
||||
}
|
||||
tim := time.Now().Unix()
|
||||
|
||||
if len(data) == 1 && data[0] == nil {
|
||||
this.mutex.Lock()
|
||||
this.delete(key)
|
||||
this.mutex.Unlock()
|
||||
return reData
|
||||
}
|
||||
|
||||
if len(data) == 1 {
|
||||
if this.Time == 0 {
|
||||
//this.Time = Config.GetInt64("cacheShortTime")
|
||||
}
|
||||
|
||||
tim = tim + this.Time
|
||||
|
||||
}
|
||||
if len(data) == 2 {
|
||||
this.Error.SetError(nil)
|
||||
tempt := ObjToInt64(data[1], &this.Error)
|
||||
|
||||
if tempt > tim {
|
||||
|
||||
tim = tempt
|
||||
} else if this.GetError() == nil {
|
||||
|
||||
tim = tim + tempt
|
||||
}
|
||||
}
|
||||
this.mutex.Lock()
|
||||
this.set(key, data[0], tim)
|
||||
this.mutex.Unlock()
|
||||
return reData
|
||||
|
||||
}
|
||||
Vendored
+153
@@ -0,0 +1,153 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
. "../common"
|
||||
"github.com/garyburd/redigo/redis"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CacheRedis struct {
|
||||
Host string
|
||||
Pwd string
|
||||
Time int64
|
||||
conn redis.Conn
|
||||
tag int64
|
||||
Error
|
||||
}
|
||||
|
||||
//唯一标志
|
||||
func (this *CacheRedis) GetTag() int64 {
|
||||
|
||||
if this.tag == int64(0) {
|
||||
this.tag = time.Now().UnixNano()
|
||||
}
|
||||
return this.tag
|
||||
}
|
||||
|
||||
func (this *CacheRedis) reCon() bool {
|
||||
var err error
|
||||
this.conn, err = redis.Dial("tcp", this.Host)
|
||||
if err != nil {
|
||||
this.conn = nil
|
||||
this.Error.SetError(err)
|
||||
return false
|
||||
}
|
||||
|
||||
_, err = this.conn.Do("AUTH", this.Pwd)
|
||||
if err != nil {
|
||||
this.conn = nil
|
||||
this.Error.SetError(err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (this *CacheRedis) del(key string) {
|
||||
del := strings.Index(key, "*")
|
||||
if del != -1 {
|
||||
val, err := redis.Strings(this.conn.Do("KEYS", key))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
this.conn.Send("MULTI")
|
||||
for i, _ := range val {
|
||||
this.conn.Send("DEL", val[i])
|
||||
}
|
||||
this.conn.Do("EXEC")
|
||||
} else {
|
||||
_, err := this.conn.Do("DEL", key)
|
||||
if err != nil {
|
||||
this.Error.SetError(err)
|
||||
_, err = this.conn.Do("PING")
|
||||
if err != nil {
|
||||
if this.reCon() {
|
||||
_, err = this.conn.Do("DEL", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//key value ,时间为时间戳
|
||||
func (this *CacheRedis) set(key string, value string, time int64) {
|
||||
_, err := this.conn.Do("SET", key, value, "EX", ObjToStr(time))
|
||||
if err != nil {
|
||||
|
||||
this.Error.SetError(err)
|
||||
_, err = this.conn.Do("PING")
|
||||
if err != nil {
|
||||
if this.reCon() {
|
||||
_, err = this.conn.Do("SET", key, value, "EX", ObjToStr(time))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *CacheRedis) get(key string) *Obj {
|
||||
reData := &Obj{}
|
||||
var err error
|
||||
reData.Data, err = redis.String(this.conn.Do("GET", key))
|
||||
if err != nil {
|
||||
reData.Data = nil
|
||||
if !strings.Contains(err.Error(), "nil returned") {
|
||||
this.Error.SetError(err)
|
||||
_, err = this.conn.Do("PING")
|
||||
if err != nil {
|
||||
if this.reCon() {
|
||||
reData.Data, err = redis.String(this.conn.Do("GET", key))
|
||||
}
|
||||
}
|
||||
|
||||
return reData
|
||||
}
|
||||
}
|
||||
return reData
|
||||
}
|
||||
|
||||
func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj {
|
||||
reData := &Obj{}
|
||||
if this.conn == nil {
|
||||
re := this.reCon()
|
||||
if !re {
|
||||
return reData
|
||||
}
|
||||
}
|
||||
//查询缓存
|
||||
if len(data) == 0 {
|
||||
|
||||
reData = this.get(key)
|
||||
return reData
|
||||
|
||||
}
|
||||
tim := int64(0)
|
||||
//删除缓存
|
||||
if len(data) == 1 && data[0] == nil {
|
||||
this.del(key)
|
||||
return reData
|
||||
}
|
||||
//添加缓存
|
||||
if len(data) == 1 {
|
||||
|
||||
if this.Time == 0 {
|
||||
//this.Time = Config.GetInt64("cacheShortTime")
|
||||
}
|
||||
|
||||
tim += this.Time
|
||||
}
|
||||
if len(data) == 2 {
|
||||
this.Error.SetError(nil)
|
||||
tempt := ObjToInt64(data[1], &this.Error)
|
||||
if tempt > tim {
|
||||
|
||||
tim = tempt
|
||||
} else if this.GetError() == nil {
|
||||
|
||||
tim = tim + tempt
|
||||
}
|
||||
}
|
||||
|
||||
this.set(key, ObjToStr(data[0]), tim)
|
||||
|
||||
return reData
|
||||
|
||||
}
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
. "../common"
|
||||
)
|
||||
|
||||
type CacheIns interface {
|
||||
//set(key string, value interface{}, time int64)
|
||||
//get(key string) interface{}
|
||||
//delete(key string)
|
||||
Cache(key string, data ...interface{}) *Obj
|
||||
}
|
||||
|
||||
//单条缓存数据
|
||||
type cacheData struct {
|
||||
time int64
|
||||
data interface{}
|
||||
}
|
||||
Reference in New Issue
Block a user