diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a1baa2a..ad759b1 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,19 +5,11 @@ - - - - - - + - - - - + - - + + @@ -48,8 +40,28 @@ - - + + + + + + + + + + + + + + + + + + + + + + @@ -114,14 +126,14 @@ @@ -157,7 +169,6 @@ - @@ -188,6 +199,7 @@ + @@ -388,6 +400,9 @@ + + + 1500458878821 @@ -582,7 +597,7 @@ - @@ -595,27 +610,25 @@ - - - - + + - + - + @@ -623,6 +636,7 @@ + @@ -647,355 +661,66 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/cache_memory.go b/cache_memory.go index 619d3de..cad266f 100644 --- a/cache_memory.go +++ b/cache_memory.go @@ -62,18 +62,22 @@ func (this *CacheMemory) Cache(key string, data ...interface{}) *Obj { this.mutex=&sync.RWMutex{} } + reData:= &Obj{} - this.mutex.Lock() - defer this.mutex.Unlock() if len(data) == 0 { - return &Obj{Data: this.get(key)} + 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) - return &Obj{Data: nil} + this.mutex.Unlock() + return reData } if len(data) == 1 { @@ -93,8 +97,9 @@ func (this *CacheMemory) Cache(key string, data ...interface{}) *Obj { } } - + this.mutex.Lock() this.set(key, data[0], tim) - return &Obj{Data: nil} + this.mutex.Unlock() + return reData } diff --git a/cache_redis.go b/cache_redis.go index eef2d13..717c713 100644 --- a/cache_redis.go +++ b/cache_redis.go @@ -2,6 +2,7 @@ package hotime import ( "time" + "sync" ) type CacheRedis struct { @@ -10,52 +11,48 @@ type CacheRedis struct { contextBase } -//获取Cache键只能为string类型 -func (this *CacheRedis) get(key string) interface{} { - this.Error.SetError(nil) - if this.Map[key] == nil { - return nil +func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj { + + + + reData:= &Obj{} + + + if len(data) == 0 { + this.mutex.RLock() + reData.Data=this.get(key) + this.mutex.RUnlock() + return reData } - data := this.Map.Get(key, &this.Error).(cacheData) - if this.GetError() != nil { - return nil + tim := time.Now().Unix() + + if len(data) == 1 && data[0] == nil { + this.mutex.Lock() + this.delete(key) + this.mutex.Unlock() + return reData } - //data:=cacheMap[key]; - if data.time <= time.Now().Unix() { - delete(this.Map, key) - return nil + + if len(data) == 1 { + + if this.Time == 0 { + this.Time = Config.GetInt64("cacheShortTime") + } + + tim += this.Time } - return data.data + 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 + } - -//key value ,时间为时间戳 -func (this *CacheRedis) set(key string, value interface{}, time int64) { - this.Error.SetError(nil) - var data cacheData - dd := this.Map.Get(key, &this.Error) - - if dd == nil { - data = cacheData{} - } else { - data = dd.(cacheData) - } - - data.time = time - data.data = value - - if this.Map == nil { - this.Map = Map{} - } - this.Map.Put(key, data) -} - -func (this *CacheRedis) delete(key string) { - - delete(this.Map, key) -} - -//func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj { -// -// -// -//} diff --git a/example/config/config.json b/example/config/config.json index 14109fc..330512c 100644 --- a/example/config/config.json +++ b/example/config/config.json @@ -6,6 +6,9 @@ "dbPort": "3306", "dbPwd": "root", "dbUser": "root", + "redisPort":"6379", + "redisHost":"192.168.6.254", + "redisPwd":"9rusdfjk482fjdfo2e023", "defFile": [ "index.html", "index.htm" diff --git a/example/main.go b/example/main.go index 358cc1f..cd93c01 100644 --- a/example/main.go +++ b/example/main.go @@ -10,21 +10,6 @@ import ( func main() { - x:=hotime.CacheMemory{} - x.Cache("x",123) - - for i:=0;i<10000000 ;i++ { - go func() { - x.Cache("y",i) - fmt.Println(x.Cache("y")) - fmt.Println(x.Cache("y",nil)) - - }() - - } - - - appIns := hotime.Application{} i := 0