缓存驱动更换成功,支持3级缓存,memory,以及db

This commit is contained in:
hoteas 2021-05-28 22:52:22 +08:00
parent fc315064e7
commit 1101937028
11 changed files with 369 additions and 126 deletions

View File

@ -32,10 +32,8 @@ type Application struct {
configPath string
Config Map
Db HoTimeDB
*HoTimeCache
*http.Server
CacheIns
sessionLong CacheIns
sessionShort CacheIns
http.Handler
}
@ -51,8 +49,8 @@ func (that *Application) Run(router Router) {
}
//防止手动设置缓存误伤
if that.CacheIns == nil {
that.SetCache(CacheIns(&CacheMemory{}))
if that.HoTimeCache == nil {
that.SetCache()
}
//防止手动设置session误伤
@ -103,10 +101,6 @@ func (that *Application) Run(router Router) {
that.Db.SetConnect(that.connectDbFunc)
}
if that.CacheIns == nil {
that.CacheIns = CacheIns(&CacheMemory{Map: Map{}, Time: that.Config.GetInt64("cacheShortTime")})
}
//异常处理
defer func() {
if err := recover(); err != nil {
@ -170,13 +164,6 @@ func (that *Application) SetConnectDB(connect func(err ...*Error) (master, slave
}
// SetSession 设置配置文件路径全路径或者相对路径
func (that *Application) SetSession(short CacheIns, Long CacheIns) {
that.sessionLong = Long
that.sessionShort = short
}
// SetDefault 默认配置缓存和session实现
func (that *Application) SetDefault(connect func(err ...*Error) (*sql.DB, *sql.DB)) {
that.SetConfig()
@ -189,10 +176,10 @@ func (that *Application) SetDefault(connect func(err ...*Error) (*sql.DB, *sql.D
}
// SetCache 设置配置文件路径全路径或者相对路径
func (that *Application) SetCache(cache CacheIns) {
that.CacheIns = cache
func (that *Application) SetCache() {
cacheIns := HoTimeCache{}
cacheIns.Init(that.Config.GetMap("cache"), HoTimeDBInterface(&that.Db), &that.Error)
that.HoTimeCache = &cacheIns
}
// SetConfig 设置配置文件路径全路径或者相对路径
@ -320,11 +307,9 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) {
}
//访问实例
context := Context{SessionIns: SessionIns{SessionId: sessionId,
LongCache: that.sessionLong,
ShortCache: that.sessionShort,
HoTimeCache: that.HoTimeCache,
},
CacheIns: that.CacheIns,
Resp: w, Req: req, Application: that, RouterString: s, Config: that.Config, Db: &that.Db, HandlerStr: unescapeUrl}
Resp: w, Req: req, Application: that, RouterString: s, Config: that.Config, Db: &that.Db, HandlerStr: unescapeUrl}
//header默认设置
header := w.Header()
header.Set("Content-Type", "text/html; charset=utf-8")
@ -470,7 +455,7 @@ func Init(config string) Application {
appIns.SetConfig(config)
SetDB(&appIns)
//appIns.SetCache()
appIns.SetCache()
return appIns
}

277
cache/cache.go vendored
View File

@ -2,31 +2,276 @@ package cache
import (
. "../common"
"errors"
)
// HoTimeCache 可配置memorydbredis默认启用memory默认优先级为memory>redis>db,memory与数据库缓存设置项一致
//缓存数据填充会自动反方向反哺加入memory缓存过期将自动从redis更新但memory永远不会更新redis如果是集群建议不要开启memory配置即启用
type HoTimeCache struct {
//set(key string, value interface{}, time int64)
//get(key string) interface{}
//delete(key string)
Error
dbCache CacheIns
redisCache CacheIns
memoryCache CacheIns
*Error
dbCache *CacheDb
redisCache *CacheRedis
memoryCache *CacheMemory
Config Map
}
func (this *HoTimeCache) Session(key string, data ...interface{}) *Obj {
return nil
func (that *HoTimeCache) Session(key string, data ...interface{}) *Obj {
var reData *Obj
if len(data) == 0 {
//内存缓存有
if that.memoryCache != nil && that.memoryCache.SessionSet {
reData = that.memoryCache.Cache(key, data)
if reData != nil {
return reData
}
}
//redis缓存有
if that.redisCache != nil && that.redisCache.SessionSet {
reData = that.redisCache.Cache(key, data)
if reData != nil {
if that.memoryCache != nil && that.memoryCache.SessionSet {
that.memoryCache.Cache(key, reData)
}
return reData
}
}
//db缓存有
if that.dbCache != nil && that.dbCache.SessionSet {
reData = that.dbCache.Cache(key, data)
if reData != nil {
if that.memoryCache != nil && that.memoryCache.SessionSet {
that.memoryCache.Cache(key, reData)
}
if that.redisCache != nil && that.redisCache.SessionSet {
that.redisCache.Cache(key, reData)
}
return reData
}
}
return reData
}
//设置缓存
//设置内存缓存
if that.memoryCache != nil && that.memoryCache.SessionSet {
reData = that.memoryCache.Cache(key, data)
}
//redis缓存有
if that.redisCache != nil && that.redisCache.SessionSet {
reData = that.redisCache.Cache(key, data)
}
//redis缓存有
if that.dbCache != nil && that.dbCache.SessionSet {
reData = that.dbCache.Cache(key, data)
}
return reData
}
func (this *HoTimeCache) Db(key string, data ...interface{}) *Obj {
return nil
func (that *HoTimeCache) Db(key string, data ...interface{}) *Obj {
var reData *Obj
if len(data) == 0 {
//内存缓存有
if that.memoryCache != nil && that.memoryCache.DbSet {
reData = that.memoryCache.Cache(key, data)
if reData != nil {
return reData
}
}
//redis缓存有
if that.redisCache != nil && that.redisCache.DbSet {
reData = that.redisCache.Cache(key, data)
if reData != nil {
if that.memoryCache != nil && that.memoryCache.DbSet {
that.memoryCache.Cache(key, reData)
}
return reData
}
}
//redis缓存有
if that.dbCache != nil && that.dbCache.DbSet {
reData = that.dbCache.Cache(key, data)
if reData != nil {
if that.memoryCache != nil && that.memoryCache.DbSet {
that.memoryCache.Cache(key, reData)
}
if that.redisCache != nil && that.redisCache.DbSet {
that.redisCache.Cache(key, reData)
}
return reData
}
}
return reData
}
//设置缓存
//设置内存缓存
if that.memoryCache != nil && that.memoryCache.DbSet {
reData = that.memoryCache.Cache(key, data)
}
//redis缓存有
if that.redisCache != nil && that.redisCache.DbSet {
reData = that.redisCache.Cache(key, data)
}
//redis缓存有
if that.dbCache != nil && that.dbCache.DbSet {
reData = that.dbCache.Cache(key, data)
}
return reData
}
func (this *HoTimeCache) Cache(key string, data ...interface{}) *Obj {
return nil
func (that *HoTimeCache) Cache(key string, data ...interface{}) *Obj {
var reData *Obj
if len(data) == 0 {
//内存缓存有
if that.memoryCache != nil {
reData = that.memoryCache.Cache(key, data)
if reData != nil {
return reData
}
}
//redis缓存有
if that.redisCache != nil {
reData = that.redisCache.Cache(key, data)
if reData != nil {
if that.memoryCache != nil {
that.memoryCache.Cache(key, reData)
}
return reData
}
}
//redis缓存有
if that.dbCache != nil {
reData = that.dbCache.Cache(key, data)
if reData != nil {
if that.memoryCache != nil {
that.memoryCache.Cache(key, reData)
}
if that.redisCache != nil {
that.redisCache.Cache(key, reData)
}
return reData
}
}
return reData
}
//设置缓存
//设置内存缓存
if that.memoryCache != nil {
reData = that.memoryCache.Cache(key, data)
}
//redis缓存有
if that.redisCache != nil {
reData = that.redisCache.Cache(key, data)
}
//redis缓存有
if that.dbCache != nil {
reData = that.dbCache.Cache(key, data)
}
return reData
}
func (this *HoTimeCache) getIndex() []CacheIns {
func (that *HoTimeCache) Init(config Map, hotimeDb HoTimeDBInterface, err ...*Error) {
//防止空数据问题
if config == nil {
config = Map{}
}
if err[0] != nil {
that.Error = err[0]
}
that.Config = config
//memory配置初始化
memory := that.Config.GetMap("memory")
if memory == nil {
memory = Map{
"db": true,
"session": true,
"sort": 0,
"timeout": 60 * 60 * 2,
}
}
if memory.Get("db") == nil {
memory["db"] = true
}
if memory.Get("session") == nil {
memory["session"] = true
}
if memory.Get("timeout") == nil {
memory["timeout"] = 60 * 60 * 2
}
that.Config["memory"] = memory
that.memoryCache = &CacheMemory{TimeOut: memory.GetCeilInt64("timeout"),
DbSet: memory.GetBool("db"), SessionSet: memory.GetBool("session")}
if err[0] != nil {
that.memoryCache.SetError(err[0])
}
//db配置初始化
redis := that.Config.GetMap("redis")
if redis != nil {
if redis.GetString("host") == "" || redis.GetString("port") == "" {
if err[0] != nil {
err[0].SetError(errors.New("请检查redis配置host和port配置"))
}
return
}
if redis.Get("db") == nil {
redis["db"] = true
}
if redis.Get("session") == nil {
redis["session"] = true
}
if redis.Get("timeout") == nil {
redis["timeout"] = 60 * 60 * 24 * 30
}
that.Config["redis"] = redis
that.redisCache = &CacheRedis{TimeOut: redis.GetCeilInt64("timeout"),
DbSet: redis.GetBool("db"), SessionSet: redis.GetBool("session"), Host: redis.GetString("host"),
Pwd: redis.GetString("password"), Port: redis.GetCeilInt64("port")}
if err[0] != nil {
that.redisCache.SetError(err[0])
}
}
//db配置初始化
db := that.Config.GetMap("db")
if db != nil {
if db.Get("db") == nil {
db["db"] = false
}
if db.Get("session") == nil {
db["session"] = true
}
if db.Get("timeout") == nil {
db["timeout"] = 60 * 60 * 24 * 30
}
that.Config["db"] = db
that.dbCache = &CacheDb{TimeOut: db.GetCeilInt64("timeout"),
DbSet: db.GetBool("db"), SessionSet: db.GetBool("session"),
Db: hotimeDb}
if err[0] != nil {
that.dbCache.SetError(err[0])
}
}
return nil
}

21
cache/cache_db.go vendored
View File

@ -20,13 +20,26 @@ type HoTimeDBInterface interface {
}
type CacheDb struct {
Time int64
Db HoTimeDBInterface
Error
TimeOut int64
DbSet bool
SessionSet bool
Time int64
Db HoTimeDBInterface
*Error
ContextBase
isInit bool
}
func (this *CacheDb) GetError() *Error {
return this.Error
}
func (this *CacheDb) SetError(err *Error) {
this.Error = err
}
func (that *CacheDb) initDbTable() {
if that.isInit {
return
@ -146,7 +159,7 @@ func (that *CacheDb) Cache(key string, data ...interface{}) *Obj {
}
if len(data) == 2 {
that.SetError(nil)
tempt := ObjToInt64(data[1], &that.Error)
tempt := ObjToInt64(data[1], that.Error)
if tempt > tim {
tim = tempt

29
cache/cache_memory.go vendored
View File

@ -8,13 +8,25 @@ import (
)
type CacheMemory struct {
Time int64
TimeOut int64
DbSet bool
SessionSet bool
Map
Error
*Error
ContextBase
mutex *sync.RWMutex
}
func (this *CacheMemory) GetError() *Error {
return this.Error
}
func (this *CacheMemory) SetError(err *Error) {
this.Error = err
}
//获取Cache键只能为string类型
func (this *CacheMemory) get(key string) interface{} {
this.Error.SetError(nil)
@ -25,8 +37,8 @@ func (this *CacheMemory) get(key string) interface{} {
if this.Map[key] == nil {
return nil
}
data := this.Map.Get(key, &this.Error).(cacheData)
if this.GetError() != nil {
data := this.Map.Get(key, this.Error).(cacheData)
if this.Error.GetError() != nil {
return nil
}
@ -121,21 +133,18 @@ func (this *CacheMemory) Cache(key string, data ...interface{}) *Obj {
}
if len(data) == 1 {
if this.Time == 0 {
//this.Time = Config.GetInt64("cacheShortTime")
}
tim = tim + this.Time
tim = tim + this.TimeOut
}
if len(data) == 2 {
this.Error.SetError(nil)
tempt := ObjToInt64(data[1], &this.Error)
tempt := ObjToInt64(data[1], this.Error)
if tempt > tim {
tim = tempt
} else if this.GetError() == nil {
} else if this.Error.GetError() == nil {
tim = tim + tempt
}

33
cache/cache_redis.go vendored
View File

@ -8,13 +8,26 @@ import (
)
type CacheRedis struct {
Host string
Pwd string
Time int64
conn redis.Conn
tag int64
TimeOut int64
DbSet bool
SessionSet bool
Host string
Pwd string
Port int64
conn redis.Conn
tag int64
ContextBase
Error
*Error
}
func (this *CacheRedis) GetError() *Error {
return this.Error
}
func (this *CacheRedis) SetError(err *Error) {
this.Error = err
}
//唯一标志
@ -28,7 +41,7 @@ func (this *CacheRedis) GetTag() int64 {
func (this *CacheRedis) reCon() bool {
var err error
this.conn, err = redis.Dial("tcp", this.Host)
this.conn, err = redis.Dial("tcp", this.Host+":"+ObjToStr(this.Port))
if err != nil {
this.conn = nil
this.Error.SetError(err)
@ -129,15 +142,15 @@ func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj {
//添加缓存
if len(data) == 1 {
if this.Time == 0 {
if this.TimeOut == 0 {
//this.Time = Config.GetInt64("cacheShortTime")
}
tim += this.Time
tim += this.TimeOut
}
if len(data) == 2 {
this.Error.SetError(nil)
tempt := ObjToInt64(data[1], &this.Error)
tempt := ObjToInt64(data[1], this.Error)
if tempt > tim {
tim = tempt

4
cache/type.go vendored
View File

@ -8,8 +8,8 @@ type CacheIns interface {
//set(key string, value interface{}, time int64)
//get(key string) interface{}
//delete(key string)
GetError() error
SetError(err error)
GetError() *Error
SetError(err *Error)
Cache(key string, data ...interface{}) *Obj
}

View File

@ -6,7 +6,7 @@ import (
// Error 框架层处理错误
type Error struct {
*logrus.Logger
Logger *logrus.Logger
error
}

View File

@ -16,9 +16,8 @@ import (
type HoTimeDB struct {
*sql.DB
ContextBase
cache.CacheIns
*cache.HoTimeCache
Type string
DBCached bool
LastQuery string
LastData []interface{}
ConnectFunc func(err ...*Error) (*sql.DB, *sql.DB)
@ -42,7 +41,7 @@ func (that *HoTimeDB) GetType() string {
// Action 事务如果action返回true则执行成功false则回滚
func (that *HoTimeDB) Action(action func(db HoTimeDB) bool) bool {
db := HoTimeDB{DB: that.DB, CacheIns: that.CacheIns, DBCached: that.DBCached}
db := HoTimeDB{DB: that.DB, HoTimeCache: that.HoTimeCache}
tx, err := db.Begin()
if err != nil {
that.LastErr.SetError(err)
@ -468,9 +467,9 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map {
qs = append(qs, resWhere...)
md5 := that.md5(query, qs...)
if that.DBCached && that.CacheIns != nil {
if that.HoTimeCache != nil {
//如果缓存有则从缓存取
cacheData := that.Cache(table + ":" + md5)
cacheData := that.HoTimeCache.Db(table + ":" + md5)
if cacheData.Data != nil {
return cacheData.ToMapArray()
@ -485,9 +484,9 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map {
}
//缓存
if that.DBCached && that.CacheIns != nil {
if that.HoTimeCache != nil {
_ = that.Cache(table+":"+md5, res)
_ = that.HoTimeCache.Db(table+":"+md5, res)
}
return res
@ -889,8 +888,8 @@ func (that *HoTimeDB) Update(table string, data Map, where Map) int64 {
//如果更新成功,则删除缓存
if rows != 0 {
if that.DBCached && that.CacheIns != nil {
_ = that.Cache(table+"*", nil)
if that.HoTimeCache != nil {
_ = that.HoTimeCache.Db(table+"*", nil)
}
}
@ -912,8 +911,8 @@ func (that *HoTimeDB) Delete(table string, data map[string]interface{}) int64 {
//如果删除成功,删除对应缓存
if rows != 0 {
if that.DBCached && that.CacheIns != nil {
_ = that.Cache(table+"*", nil)
if that.HoTimeCache != nil {
_ = that.HoTimeCache.Db(table+"*", nil)
}
}
//return 0
@ -954,8 +953,8 @@ func (that *HoTimeDB) Insert(table string, data map[string]interface{}) int64 {
//如果插入成功,删除缓存
if id != 0 {
if that.DBCached && that.CacheIns != nil {
_ = that.Cache(table+"*", nil)
if that.HoTimeCache != nil {
_ = that.HoTimeCache.Db(table+"*", nil)
}
}

View File

@ -2,7 +2,6 @@ package main
import (
"../../hotime"
"../../hotime/cache"
"fmt"
"golang.org/x/net/websocket"
"time"
@ -22,18 +21,18 @@ func main() {
//手动模式,
//appIns.SetConfig("example/config/config.json")
//redis缓存接入
ca := cache.CacheIns(&cache.CacheRedis{Host: appIns.Config.GetString("redisHost"), Pwd: appIns.Config.GetString("redisPwd"), Time: appIns.Config.GetCeilInt64("cacheLongTime")})
ca.Cache("xyzm", "dasdas")
ca.Cache("xyzn", "dasdas")
ca.Cache("xyzo", "dasdas")
ca.Cache("xyz*", nil)
//ca := cache.CacheIns(&cache.CacheRedis{Host: appIns.Config.GetString("redisHost"), Pwd: appIns.Config.GetString("redisPwd"), Time: appIns.Config.GetCeilInt64("cacheLongTime")})
//ca.Cache("xyzm", "dasdas")
//ca.Cache("xyzn", "dasdas")
//ca.Cache("xyzo", "dasdas")
//ca.Cache("xyz*", nil)
//fmt.Println(ca.Cache("xyzm").Data)
//mysql
//mysql.SetDB(&appIns)
//自动选择数据库
dbInterface := cache.HoTimeDBInterface(&appIns.Db)
appIns.SetSession(cache.CacheIns(&cache.CacheMemory{}), cache.CacheIns(&cache.CacheDb{Db: dbInterface, Time: appIns.Config.GetInt64("cacheTime")}))
appIns.SetCache(cache.CacheIns(&cache.CacheMemory{}))
//dbInterface := cache.HoTimeDBInterface(&appIns.Db)
//appIns.SetSession(cache.CacheIns(&cache.CacheMemory{}), cache.CacheIns(&cache.CacheDb{Db: dbInterface, Time: appIns.Config.GetInt64("cacheTime")}))
//appIns.SetCache(cache.CacheIns(&cache.CacheMemory{}))
//快捷模式
//appIns.SetDefault(func(err ...*common.Error) (*sql.DB, *sql.DB) {
@ -50,6 +49,9 @@ func main() {
"app": hotime.Proj{
"index": hotime.Ctr{
"test": func(this *hotime.Context) {
fmt.Println(this.Session("test").Data)
this.Session("test", 145484978484)
fmt.Println(this.Session("test").Data)
//fmt.Println(this.Db.GetTag())
//this.Application.Log.Error("dasdasdas")
//this.Log.Error("dadasdasd")

View File

@ -7,22 +7,14 @@ import (
//session对象
type SessionIns struct {
ShortCache CacheIns
LongCache CacheIns
SessionId string
*HoTimeCache
SessionId string
Map
ContextBase
}
func (that *SessionIns) set() {
if that.ShortCache != nil {
that.ShortCache.Cache(HEAD_SESSION_ADD+that.SessionId, that.Map)
}
if that.LongCache != nil {
that.LongCache.Cache(HEAD_SESSION_ADD+that.SessionId, that.Map)
}
that.HoTimeCache.Session(HEAD_SESSION_ADD+that.SessionId, that.Map)
}
func (that *SessionIns) Session(key string, data ...interface{}) *Obj {
@ -48,29 +40,17 @@ func (that *SessionIns) Session(key string, data ...interface{}) *Obj {
func (that *SessionIns) get() {
if that.ShortCache != nil {
that.Map = that.ShortCache.Cache(HEAD_SESSION_ADD + that.SessionId).ToMap()
if that.Map != nil {
return
}
that.Map = that.HoTimeCache.Session(HEAD_SESSION_ADD + that.SessionId).ToMap()
if that.Map != nil {
return
}
if that.LongCache != nil {
that.Map = that.LongCache.Cache(HEAD_SESSION_ADD + that.SessionId).ToMap()
if that.Map != nil {
if that.ShortCache != nil {
that.ShortCache.Cache(HEAD_SESSION_ADD+that.SessionId, that.Map)
}
return
}
}
that.Map = Map{}
that.ShortCache.Cache(HEAD_SESSION_ADD+that.SessionId, that.Map)
that.HoTimeCache.Session(HEAD_SESSION_ADD+that.SessionId, that.Map)
return
}
func (that *SessionIns) Init(short CacheIns, long CacheIns) {
that.ShortCache = short
that.LongCache = long
func (that *SessionIns) Init(cache *HoTimeCache) {
that.HoTimeCache = cache
}

11
var.go
View File

@ -68,25 +68,22 @@ var ConfigNote = Map{
"cache": Map{
"注释": "可配置memorydbredis默认启用memory默认优先级为memory>redis>db,memory与数据库缓存设置项一致缓存数据填充会自动反方向反哺加入memory缓存过期将自动从redis更新但memory永远不会更新redis如果是集群建议不要开启memory配置即启用",
"memory": Map{
"timeout": "默认60 * 60 * 2必须,过期时间,超时自动删除",
"timeout": "默认60 * 60 * 2必须,过期时间,超时自动删除",
"db": "默认true非必须缓存数据库启用后能减少数据库的读写压力",
"session": "默认true非必须缓存web session同时缓存session保持的用户缓存",
"sort": "默认0非必须优先级数值越大优先级越高默认优先级为memory>redis>db,",
},
"db": Map{
"timeout": "默认60 * 60 * 24 * 30必须,过期时间,超时自动删除",
"db": "默认true非必须缓存数据库启用后能减少数据库的读写压力",
"timeout": "默认60 * 60 * 24 * 30必须,过期时间,超时自动删除",
"db": "默认false非必须缓存数据库启用后能减少数据库的读写压力",
"session": "默认true非必须缓存web session同时缓存session保持的用户缓存",
"sort": "默认0非必须优先级数值越大优先级越高默认优先级为memory>redis>db,",
},
"redis": Map{
"host": "默认服务ip127.0.0.1必须如果需要使用redis服务时配置",
"port": "默认服务端口6379必须如果需要使用redis服务时配置",
"password": "默认密码空必须如果需要使用redis服务时配置默认密码空",
"timeout": "memory默认60 * 60 * 30db默认60 * 60 * 24 * 30必须,过期时间,超时自动删除",
"timeout": "默认60 * 60 * 24 * 15必须,过期时间,超时自动删除",
"db": "默认true非必须缓存数据库启用后能减少数据库的读写压力",
"session": "默认true非必须缓存web session同时缓存session保持的用户缓存",
"sort": "默认0非必须优先级数值越大优先级越高默认优先级为memory>redis>db,",
},
},
"error": Map{