diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 88e09a6..651b310 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,13 +4,11 @@
-
-
-
-
-
+
+
+
@@ -31,8 +29,52 @@
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -41,8 +83,8 @@
-
-
+
+
@@ -51,18 +93,38 @@
-
-
+
+
-
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -124,7 +186,6 @@
-
@@ -132,8 +193,10 @@
-
+
+
+
@@ -178,6 +241,22 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -198,13 +277,14 @@
-
-
-
-
+
+
+
+
+
+
+ 1522782970484
+
+
+
+ 1522782970484
+
+
-
+
@@ -604,7 +691,7 @@
-
+
@@ -650,7 +737,9 @@
-
+
+
+
@@ -666,7 +755,9 @@
-
+
+
+
@@ -678,22 +769,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -702,11 +777,87 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
diff --git a/cache/cache_redis.go b/cache/cache_redis.go
new file mode 100644
index 0000000..2668dfb
--- /dev/null
+++ b/cache/cache_redis.go
@@ -0,0 +1,124 @@
+package cache
+
+import (
+ "time"
+ "github.com/garyburd/redigo/redis"
+ . "go.hoteas.com/hotime"
+ "strings"
+)
+
+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) Cache(key string, data ...interface{}) *Obj {
+ reData:= &Obj{}
+ var err error
+ if this.conn==nil{
+ re:=this.reCon()
+ if !re{
+ return reData
+ }
+ }
+ //查询缓存
+ if len(data) == 0 {
+
+ 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
+
+ }
+ tim := int64(0)
+ //删除缓存
+ if len(data) == 1 && data[0] == nil {
+
+ _,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)
+ }
+ }
+ }
+ 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 this.GetError() == nil {
+
+ tim = tim + tempt
+
+ }
+ }
+
+ _,err=this.conn.Do("SET",key,ObjToStr(data[0]),"EX",ObjToStr(tim))
+ if err!=nil{
+
+ this.Error.SetError(err)
+ _,err=this.conn.Do("PING")
+ if err!=nil{
+ if this.reCon(){
+ _,err=this.conn.Do("SET",key,ObjToStr(data[0]),"EX",ObjToStr(tim))
+ }
+ }
+ }
+ return reData
+
+}
diff --git a/cache_memory.go b/cache_memory.go
index cad266f..e4fe48a 100644
--- a/cache_memory.go
+++ b/cache_memory.go
@@ -30,6 +30,24 @@ func (this *CacheMemory) get(key string) interface{} {
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)
@@ -58,6 +76,11 @@ func (this *CacheMemory) delete(key string) {
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{}
}
diff --git a/cache_redis.go b/cache_redis.go
deleted file mode 100644
index e35265a..0000000
--- a/cache_redis.go
+++ /dev/null
@@ -1,59 +0,0 @@
-package hotime
-
-import (
- "time"
- "sync"
- "github.com/garyburd/redigo/redis"
-)
-
-type CacheRedis struct {
- Time int64
- redis.Conn
- contextBase
-}
-
-func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj {
-
- redis.Dial()
-
- reData:= &Obj{}
-
-
- 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 += this.Time
- }
- if len(data) == 2 {
- this.Error.SetError(nil)
- tempt := ObjToInt64(data[1], &this.Error)
- if this.GetError() == nil {
-
- tim = tim + tempt
-
- }
- }
- this.mutex.Lock()
- this.set(key, data[0], tim)
- this.mutex.Unlock()
- return reData
-
-}
diff --git a/example/config/config.json b/example/config/config.json
index 330512c..ed25874 100644
--- a/example/config/config.json
+++ b/example/config/config.json
@@ -6,9 +6,6 @@
"dbPort": "3306",
"dbPwd": "root",
"dbUser": "root",
- "redisPort":"6379",
- "redisHost":"192.168.6.254",
- "redisPwd":"9rusdfjk482fjdfo2e023",
"defFile": [
"index.html",
"index.htm"
@@ -16,6 +13,9 @@
"error": {},
"logLevel": 0,
"port": "80",
+ "redisHost": "192.168.6.254:6379",
+ "redisPort": "6379",
+ "redisPwd": "9rusdfjk482fjdfo2e023",
"sessionName": "HOTIME",
"tpt": "example/tpt"
}
\ No newline at end of file
diff --git a/example/main.go b/example/main.go
index 24818f9..92ce947 100644
--- a/example/main.go
+++ b/example/main.go
@@ -5,13 +5,20 @@ import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"go.hoteas.com/hotime"
+ "go.hoteas.com/hotime/cache"
"golang.org/x/net/websocket"
- "github.com/garyburd/redigo/redis"
)
func main() {
- hotime.CacheRedis{nil,redis.Dial()}
+
+
+
+
appIns := hotime.Application{}
+
+
+
+
i := 0
appIns.SetConnectListener(func(context *hotime.Context) bool {
@@ -23,6 +30,16 @@ func main() {
//手动模式,
appIns.SetConfig("example/config/config.json")
+
+
+ ca:=hotime.CacheIns(&cache.CacheRedis{Host:appIns.Config.GetString("redisHost"),Pwd:appIns.Config.GetString("redisPwd"),Time:appIns.Config.GetCeilInt64("cacheLongTime")})
+
+ ca.Cache("x",hotime.Map{"1":"2132"})
+ fmt.Println(ca.Cache("x").ToMap())
+ fmt.Println(ca.Cache("x",nil).Data)
+ fmt.Println(ca.Cache("x",nil).Data)
+ fmt.Println(ca.Cache("x").Data)
+ fmt.Println(ca.Cache("x").Data)
appIns.SetConnectDB(func(err ...*hotime.Error) *sql.DB {
query := appIns.Config.GetString("dbUser") + ":" + appIns.Config.GetString("dbPwd") +
"@tcp(" + appIns.Config.GetString("dbHost") + ":" + appIns.Config.GetString("dbPort") + ")/" + appIns.Config.GetString("dbName") + "?charset=utf8"
diff --git a/type.go b/type.go
index 4e1f8e4..9fab6fe 100644
--- a/type.go
+++ b/type.go
@@ -6,9 +6,9 @@ type Proj map[string]Ctr
type Router map[string]Proj
type CacheIns interface {
- set(key string, value interface{}, time int64)
- get(key string) interface{}
- delete(key string)
+ //set(key string, value interface{}, time int64)
+ //get(key string) interface{}
+ //delete(key string)
Cache(key string, data ...interface{}) *Obj
}