diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 3a6594a..76d0dd9 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -6,9 +6,9 @@
-
-
-
+
+
+
@@ -27,26 +27,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -60,8 +40,62 @@
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -77,51 +111,21 @@
-
-
+
+
-
-
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -142,9 +146,11 @@
limit
Ping
SESSION_TYPE
+ GetString
MUTEX_SESSION_TYPE
+ GetTag
@@ -152,7 +158,6 @@
-
@@ -175,11 +180,12 @@
-
+
+
@@ -223,8 +229,8 @@
-
-
+
+
@@ -236,6 +242,10 @@
+
+
+
+
@@ -441,7 +451,7 @@
-
+
1500458878821
@@ -583,11 +593,18 @@
1504168369028
-
+
+ 1504575745842
+
+
+
+ 1504575745842
+
+
-
+
@@ -607,7 +624,7 @@
-
+
@@ -644,13 +661,6 @@
-
-
-
-
-
-
-
@@ -771,13 +781,6 @@
-
-
-
-
-
-
-
@@ -792,20 +795,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -820,13 +809,6 @@
-
-
-
-
-
-
-
@@ -841,13 +823,6 @@
-
-
-
-
-
-
-
@@ -862,13 +837,6 @@
-
-
-
-
-
-
-
@@ -943,14 +911,6 @@
-
-
-
-
-
-
-
-
@@ -959,22 +919,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -986,10 +930,96 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/config/config.json b/config/config.json
index 3d4ee02..aebc893 100644
--- a/config/config.json
+++ b/config/config.json
@@ -1,4 +1,5 @@
{
+ "HOTIME-MAP": 1504577466932974800,
"cacheLongTime": 2592000,
"cacheShortTime": 7200,
"dbHost": "127.0.0.1",
diff --git a/example/main.go b/example/main.go
index 90b0591..0c45dd8 100644
--- a/example/main.go
+++ b/example/main.go
@@ -1,41 +1,47 @@
package main
import (
- "database/sql"
- "fmt"
+
_ "github.com/go-sql-driver/mysql"
"go.hoteas.com/hotime"
- "golang.org/x/net/websocket"
+ "time"
+ "fmt"
)
func main() {
- appIns := hotime.Application{}
- i := 0
+ a:=hotime.Map{}
+ a.Get("y")
+ for i:=0;i<300 ;i++ {
+ go func() {
+ for j:=0;j<30 ;j++ {
+ if j/2==0{
+ a.Put("x","1")
+ }else {
+ fmt.Println(a.GetInt64("x"))
+ }
- appIns.SetConnectListener(func(context *hotime.Context) bool {
- fmt.Println(i)
- i++
- //this.HandlerStr = "/test.html"
- return true
- })
+ }
+ }()
- //手动模式,
- appIns.SetConfig()
- 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"
- DB, e := sql.Open("mysql", query)
- if e != nil && len(err) != 0 {
- err[0].SetError(e)
- }
- return DB
- })
- appIns.SetSession(hotime.CacheIns(&hotime.CacheMemory{}), hotime.CacheIns(&hotime.CacheDb{Db: &appIns.Db, Time: appIns.Config.GetInt64("cacheTime")}))
- appIns.SetCache(hotime.CacheIns(&hotime.CacheMemory{}))
- //快捷模式
- //appIns.SetDefault(func(err ...*hotime.Error) *sql.DB {
+ }
+
+ time.Sleep(time.Second*5)
+
+ //appIns := hotime.Application{}
+ //i := 0
+ //
+ //appIns.SetConnectListener(func(context *hotime.Context) bool {
+ // fmt.Println(i)
+ // i++
+ // //this.HandlerStr = "/test.html"
+ // return true
+ //})
+ //
+ ////手动模式,
+ //appIns.SetConfig()
+ //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"
// DB, e := sql.Open("mysql", query)
@@ -44,42 +50,55 @@ func main() {
// }
// return DB
//})
-
- appIns.Run(hotime.Router{
- "app": hotime.Proj{
- "index": hotime.Ctr{
- "test": func(this *hotime.Context) {
- fmt.Println(this.Db.GetTag())
- x:=this.Db.Action(func(db hotime.HoTimeDB) bool {
-
- db.Insert("user",hotime.Map{"unickname":"dasdas"})
-
- return true
- })
- this.Display(5, x)
- },
- "websocket": func(this *hotime.Context) {
- hdler := websocket.Handler(func(ws *websocket.Conn) {
- for true {
- msg := make([]byte, 5120)
- n, err := ws.Read(msg)
-
- if err != nil {
- return
- }
- fmt.Printf("Receive: %s\n", msg[:n])
-
- send_msg := "[" + string(msg[:n]) + "]"
- m, err := ws.Write([]byte(send_msg))
- if err != nil {
- return
- }
- fmt.Printf("Send: %s\n", msg[:m])
- }
- })
- hdler.ServeHTTP(this.Resp, this.Req)
- },
- },
- },
- })
+ //appIns.SetSession(hotime.CacheIns(&hotime.CacheMemory{}), hotime.CacheIns(&hotime.CacheDb{Db: &appIns.Db, Time: appIns.Config.GetInt64("cacheTime")}))
+ //appIns.SetCache(hotime.CacheIns(&hotime.CacheMemory{}))
+ //
+ ////快捷模式
+ ////appIns.SetDefault(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"
+ //// DB, e := sql.Open("mysql", query)
+ //// if e != nil && len(err) != 0 {
+ //// err[0].SetError(e)
+ //// }
+ //// return DB
+ ////})
+ //
+ //appIns.Run(hotime.Router{
+ // "app": hotime.Proj{
+ // "index": hotime.Ctr{
+ // "test": func(this *hotime.Context) {
+ // fmt.Println(this.Db.GetTag())
+ // x:=this.Db.Action(func(db hotime.HoTimeDB) bool {
+ //
+ // db.Insert("user",hotime.Map{"unickname":"dasdas"})
+ //
+ // return true
+ // })
+ // this.Display(5, x)
+ // },
+ // "websocket": func(this *hotime.Context) {
+ // hdler := websocket.Handler(func(ws *websocket.Conn) {
+ // for true {
+ // msg := make([]byte, 5120)
+ // n, err := ws.Read(msg)
+ //
+ // if err != nil {
+ // return
+ // }
+ // fmt.Printf("Receive: %s\n", msg[:n])
+ //
+ // send_msg := "[" + string(msg[:n]) + "]"
+ // m, err := ws.Write([]byte(send_msg))
+ // if err != nil {
+ // return
+ // }
+ // fmt.Printf("Send: %s\n", msg[:m])
+ // }
+ // })
+ // hdler.ServeHTTP(this.Resp, this.Req)
+ // },
+ // },
+ // },
+ //})
}
diff --git a/map.go b/map.go
index 9a298f5..515279b 100644
--- a/map.go
+++ b/map.go
@@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"reflect"
+ //"time"
)
//hotime的常用map
@@ -11,86 +12,114 @@ type Map map[string]interface{}
//获取string
func (this Map) GetString(key string, err ...*Error) string {
-
+ return SafeMutex(this.GetTag(), func() interface{} {
if len(err) != 0 {
err[0].SetError(nil)
}
return ObjToStr((this)[key])
+ }).(string)
}
-func (this *Map) Pointer() *Map{
+func (this *Map) Pointer() *Map {
return this
}
//增加接口
func (this Map) Put(key string, value interface{}) {
- //if this==nil{
- // this=Map{}
- //}
+ SafeMutex(this.GetTag(), func() interface{} {
this[key] = value
+ return nil
+ })
+
}
//删除接口
func (this Map) Delete(key string) {
+ SafeMutex(this.GetTag(), func() interface{} {
delete(this, key)
-
-
-
+ return nil
+ })
}
+//唯一标志
+func (this Map) GetTag() string {
+
+ //return SafeMutex(MUTEX_MAP, func() interface{} {
+ // if this[MUTEX_MAP] == nil {
+ // this[MUTEX_MAP] = ObjToStr(time.Now().UnixNano())
+ //
+ // }
+ // return ObjToStr(this[MUTEX_MAP])
+ //}).(string)
+ return MUTEX_MAP
+ }
+
//获取Int
func (this Map) GetInt(key string, err ...*Error) int {
+
+ return SafeMutex(this.GetTag(), func() interface{} {
v := ObjToInt((this)[key], err...)
-
return v
-
+ }).(int)
}
//获取Int
func (this Map) GetInt64(key string, err ...*Error) int64 {
+
+ return SafeMutex(this.GetTag(), func() interface{} {
v := ObjToInt64((this)[key], err...)
return v
-
+ }).(int64)
}
//获取Float64
func (this Map) GetFloat64(key string, err ...*Error) float64 {
+ return SafeMutex(this.GetTag(), func() interface{} {
v := ObjToFloat64((this)[key], err...)
-
return v
+ }).(float64)
}
func (this Map) GetSlice(key string, err ...*Error) Slice {
- //var v Slice
+ res := SafeMutex(this.GetTag(), func() interface{} {
v := ObjToSlice((this)[key], err...)
-
return v
+ })
+ //var v Slice
+ if res != nil {
+ return res.(Slice)
+ }
-
+ return nil
}
func (this Map) GetMap(key string, err ...*Error) Map {
- //var data Slice
+ res := SafeMutex(this.GetTag(), func() interface{} {
v := ObjToMap((this)[key], err...)
-
return v
+ })
+ //var v Slice
+ if res != nil {
+ return res.(Map)
+ }
-
+ return nil
}
func (this Map) Get(key string, err ...*Error) interface{} {
+ res := SafeMutex(this.GetTag(), func() interface{} {
if v, ok := (this)[key]; ok {
return v
}
@@ -100,12 +129,17 @@ func (this Map) Get(key string, err ...*Error) interface{} {
err[0].SetError(e)
}
- return nil
+ return nil
+ })
+
+ return res
+
}
//请传递指针过来
func (this Map) ToStruct(stct interface{}) {
+ SafeMutex(this.GetTag(), func() interface{} {
data := reflect.ValueOf(stct).Elem()
for k, v := range this {
ks := StrFirstToUpper(k)
@@ -126,20 +160,27 @@ func (this Map) ToStruct(stct interface{}) {
dkey.Set(reflect.ValueOf(v))
}
}
-
-
+ return nil
+ })
}
func (this Map) ToJsonString() string {
+ res := SafeMutex(this.GetTag(), func() interface{} {
return ObjToStr(this)
+ })
+ return res.(string)
}
-func (this Map) JsonToMap(jsonStr string,err... *Error) {
- e:=json.Unmarshal([]byte(jsonStr), &this)
- if e!=nil&&len(err)!=0{
+func (this Map) JsonToMap(jsonStr string, err ...*Error) {
+
+ SafeMutex(this.GetTag(), func() interface{} {
+ e := json.Unmarshal([]byte(jsonStr), &this)
+ if e != nil && len(err) != 0 {
err[0].SetError(e)
}
+ return nil
+ })
}