44 "fmt"
55 "github.com/devfeel/dotweb/cache"
66 "github.com/devfeel/dotweb/config"
7+ "github.com/devfeel/dotweb/core"
78 "github.com/devfeel/dotweb/framework/json"
89 "github.com/devfeel/dotweb/framework/log"
910 "github.com/devfeel/dotweb/servers"
@@ -15,7 +16,6 @@ import (
1516 "runtime/pprof"
1617 "strconv"
1718 "strings"
18- "sync"
1919)
2020
2121type (
@@ -27,12 +27,7 @@ type (
2727 Modules []* HttpModule
2828 logpath string
2929 ExceptionHandler ExceptionHandle
30- AppContext * ItemContext
31- }
32-
33- ItemContext struct {
34- contextMap map [string ]interface {}
35- contextMutex * sync.RWMutex
30+ AppContext * core.ItemContext
3631 }
3732
3833 ExceptionHandle func (* HttpContext , interface {})
@@ -50,92 +45,12 @@ func New() *DotWeb {
5045 HttpServer : NewHttpServer (),
5146 OfflineServer : servers .NewOfflineServer (),
5247 Modules : make ([]* HttpModule , 0 , 10 ),
53- AppContext : NewItemContext (),
48+ AppContext : core . NewItemContext (),
5449 }
5550 app .HttpServer .setDotApp (app )
5651 return app
5752}
5853
59- func NewItemContext () * ItemContext {
60- return & ItemContext {
61- contextMap : make (map [string ]interface {}),
62- contextMutex : new (sync.RWMutex ),
63- }
64- }
65-
66- /*
67- * 以key、value置入AppContext
68- */
69- func (ctx * ItemContext ) Set (key string , value interface {}) error {
70- ctx .contextMutex .Lock ()
71- ctx .contextMap [key ] = value
72- ctx .contextMutex .Unlock ()
73- return nil
74- }
75-
76- /*
77- * 读取指定key在AppContext中的内容
78- */
79- func (ctx * ItemContext ) Get (key string ) (value interface {}, exists bool ) {
80- ctx .contextMutex .RLock ()
81- value , exists = ctx .contextMap [key ]
82- ctx .contextMutex .RUnlock ()
83- return value , exists
84- }
85-
86- //remove item by gived key
87- //if not exists key, do nothing...
88- func (ctx * ItemContext ) Remove (key string ) {
89- ctx .contextMutex .Lock ()
90- delete (ctx .contextMap , key )
91- ctx .contextMutex .Unlock ()
92- }
93-
94- //get item by gived key, and remove it
95- //only can be read once, it will be locked
96- func (ctx * ItemContext ) Once (key string ) (value interface {}, exists bool ) {
97- ctx .contextMutex .Lock ()
98- defer ctx .contextMutex .Unlock ()
99- value , exists = ctx .contextMap [key ]
100- if exists {
101- delete (ctx .contextMap , key )
102- }
103- return value , exists
104- }
105-
106- /*
107- * 读取指定key在AppContext中的内容,以string格式输出
108- */
109- func (ctx * ItemContext ) GetString (key string ) string {
110- value , exists := ctx .Get (key )
111- if ! exists {
112- return ""
113- }
114- return fmt .Sprint (value )
115- }
116-
117- /*
118- * 读取指定key在AppContext中的内容,以int格式输出
119- */
120- func (ctx * ItemContext ) GetInt (key string ) int {
121- value , exists := ctx .Get (key )
122- if ! exists {
123- return 0
124- }
125- return value .(int )
126- }
127-
128- //check exists key
129- func (ctx * ItemContext ) Exists (key string ) bool {
130- _ , exists := ctx .contextMap [key ]
131- return exists
132- }
133-
134- //get context length
135- func (ctx * ItemContext ) Len () int {
136- return len (ctx .contextMap )
137- }
138-
13954/*
14055* return cache interface
14156 */
0 commit comments