Skip to content

Commit de838dc

Browse files
author
pzrr@qq.com
committed
Version 0.3.9.3
* 调整Cache接口,Exists\Get\GetString\GetInt\GetInt64增加error返回值,影响Cache模块、Session模块 * Redis无法连接信息将从接口error返回,取消原来的panic设计 * 2017-04-04 16:00
1 parent 8fd40dd commit de838dc

10 files changed

Lines changed: 89 additions & 75 deletions

File tree

cache/cache.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import (
77

88
type Cache interface {
99
// Exist return true if value cached by given key
10-
Exists(key string) bool
10+
Exists(key string) (bool, error)
1111
// Get returns value by given key
12-
Get(key string) interface{}
12+
Get(key string) (interface{}, error)
1313
// GetString returns value string format by given key
14-
GetString(key string) string
14+
GetString(key string) (string, error)
1515
// GetInt returns value int format by given key
16-
GetInt(key string) int
16+
GetInt(key string) (int, error)
1717
// GetInt64 returns value int64 format by given key
18-
GetInt64(key string) int64
18+
GetInt64(key string) (int64, error)
1919
// Set cache value by given key
2020
Set(key string, v interface{}, ttl int64) error
2121
// Incr increases int64-type value by given key as a counter

cache/redis/cache_redis.go

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var (
1212
// RedisCache is redis cache adapter.
1313
// it contains serverIp for redis conn.
1414
type RedisCache struct {
15-
serverIp string //connection string, like "redis://:password@10.0.1.11:6379/0"
15+
serverIp string //connection string, like "10.0.1.11:6379"
1616
}
1717

1818
// NewRedisCache returns a new *RedisCache.
@@ -22,13 +22,10 @@ func NewRedisCache(serverIp string) *RedisCache {
2222
}
2323

2424
// Exists check item exist in redis cache.
25-
func (ca *RedisCache) Exists(key string) bool {
25+
func (ca *RedisCache) Exists(key string) (bool, error) {
2626
redisClient := redisutil.GetRedisClient(ca.serverIp)
2727
exists, err := redisClient.Exists(key)
28-
if err != nil {
29-
return false
30-
}
31-
return exists
28+
return exists, err
3229
}
3330

3431
// Incr increase int64 counter in redis cache.
@@ -53,56 +50,48 @@ func (ca *RedisCache) Decr(key string) (int64, error) {
5350

5451
// Get cache from redis cache.
5552
// if non-existed or expired, return nil.
56-
func (ca *RedisCache) Get(key string) interface{} {
53+
func (ca *RedisCache) Get(key string) (interface{}, error) {
5754
redisClient := redisutil.GetRedisClient(ca.serverIp)
5855
reply, err := redisClient.GetObj(key)
59-
if err != nil {
60-
return nil
61-
} else {
62-
return reply
63-
}
56+
return reply, err
6457
}
6558

6659
// returns value string format by given key
6760
// if non-existed or expired, return "".
68-
func (ca *RedisCache) GetString(key string) string {
61+
func (ca *RedisCache) GetString(key string) (string, error) {
6962
redisClient := redisutil.GetRedisClient(ca.serverIp)
7063
reply, err := redisClient.Get(key)
71-
if err != nil {
72-
return ""
73-
} else {
74-
return reply
75-
}
64+
return reply, err
7665
}
7766

7867
// returns value int format by given key
7968
// if non-existed or expired, return nil.
80-
func (ca *RedisCache) GetInt(key string) int {
81-
v := ca.GetString(key)
82-
if v == "" {
83-
return 0
69+
func (ca *RedisCache) GetInt(key string) (int, error) {
70+
v, err := ca.GetString(key)
71+
if err != nil || v == "" {
72+
return 0, err
8473
} else {
8574
i, e := strconv.Atoi(v)
8675
if e != nil {
87-
return 0
76+
return 0, err
8877
} else {
89-
return i
78+
return i, nil
9079
}
9180
}
9281
}
9382

9483
// returns value int64 format by given key
9584
// if non-existed or expired, return nil.
96-
func (ca *RedisCache) GetInt64(key string) int64 {
97-
v := ca.GetString(key)
98-
if v == "" {
99-
return ZeroInt64
85+
func (ca *RedisCache) GetInt64(key string) (int64, error) {
86+
v, err := ca.GetString(key)
87+
if err != nil || v == "" {
88+
return ZeroInt64, err
10089
} else {
10190
i, e := strconv.ParseInt(v, 10, 64)
10291
if e != nil {
103-
return ZeroInt64
92+
return ZeroInt64, err
10493
} else {
105-
return i
94+
return i, nil
10695
}
10796
}
10897
}

cache/runtime/cache_runtime.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,57 +47,57 @@ func NewRuntimeCache() *RuntimeCache {
4747

4848
// Get cache from runtime cache.
4949
// if non-existed or expired, return nil.
50-
func (ca *RuntimeCache) Get(key string) interface{} {
50+
func (ca *RuntimeCache) Get(key string) (interface{}, error) {
5151
ca.RLock()
5252
defer ca.RUnlock()
5353
if item, ok := ca.items[key]; ok {
5454
if item.isExpire() {
55-
return nil
55+
return nil, nil
5656
}
57-
return item.value
57+
return item.value, nil
5858
}
59-
return nil
59+
return nil, nil
6060
}
6161

6262
// returns value string format by given key
6363
// if non-existed or expired, return "".
64-
func (ca *RuntimeCache) GetString(key string) string {
65-
v := ca.Get(key)
66-
if v == nil {
67-
return ""
64+
func (ca *RuntimeCache) GetString(key string) (string, error) {
65+
v, err := ca.Get(key)
66+
if err != nil || v == nil {
67+
return "", nil
6868
} else {
69-
return fmt.Sprint(v)
69+
return fmt.Sprint(v), nil
7070
}
7171
}
7272

7373
// returns value int format by given key
7474
// if non-existed or expired, return 0.
75-
func (ca *RuntimeCache) GetInt(key string) int {
76-
v := ca.GetString(key)
77-
if v == "" {
78-
return 0
75+
func (ca *RuntimeCache) GetInt(key string) (int, error) {
76+
v, err := ca.GetString(key)
77+
if err != nil || v == "" {
78+
return 0, nil
7979
} else {
8080
i, e := strconv.Atoi(v)
8181
if e != nil {
82-
return 0
82+
return 0, nil
8383
} else {
84-
return i
84+
return i, nil
8585
}
8686
}
8787
}
8888

8989
// returns value int64 format by given key
9090
// if non-existed or expired, return 0.
91-
func (ca *RuntimeCache) GetInt64(key string) int64 {
92-
v := ca.GetString(key)
93-
if v == "" {
94-
return ZeroInt64
91+
func (ca *RuntimeCache) GetInt64(key string) (int64, error) {
92+
v, err := ca.GetString(key)
93+
if err != nil || v == "" {
94+
return ZeroInt64, nil
9595
} else {
9696
i, e := strconv.ParseInt(v, 10, 64)
9797
if e != nil {
98-
return ZeroInt64
98+
return ZeroInt64, nil
9999
} else {
100-
return i
100+
return i, nil
101101
}
102102
}
103103
}
@@ -196,13 +196,13 @@ func (ca *RuntimeCache) Decr(key string) (int64, error) {
196196
}
197197

198198
// Exist check item exist in runtime cache.
199-
func (ca *RuntimeCache) Exists(key string) bool {
199+
func (ca *RuntimeCache) Exists(key string) (bool, error) {
200200
ca.RLock()
201201
defer ca.RUnlock()
202202
if v, ok := ca.items[key]; ok {
203-
return !v.isExpire()
203+
return !v.isExpire(), nil
204204
}
205-
return false
205+
return false, nil
206206
}
207207

208208
// Delete item in runtime cacha.

example/cache/main.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@ func main() {
2828
//pprofport := 8081
2929
//go app.StartPProfServer(pprofport)
3030

31-
app.SetCache(cache.NewRuntimeCache())
31+
//app.SetCache(cache.NewRuntimeCache())
32+
app.SetCache(cache.NewRedisCache("127.0.0.1:6379"))
3233

33-
app.Cache().Set("g", "gv", 20)
34+
err := app.Cache().Set("g", "gv", 20)
35+
if err != nil {
36+
fmt.Println("Cache Set ", err)
37+
}
3438

3539
// 开始服务
3640
port := 8080
3741
fmt.Println("dotweb.StartServer => " + strconv.Itoa(port))
38-
err := app.StartServer(port)
42+
err = app.StartServer(port)
3943
fmt.Println("dotweb.StartServer error => ", err)
4044
}
4145

@@ -45,15 +49,22 @@ type UserInfo struct {
4549
}
4650

4751
func One(ctx *dotweb.HttpContext) {
48-
g := ctx.Cache().GetString("g")
49-
ctx.Cache().Incr("count")
50-
ctx.WriteString("One [" + g + "] ")
52+
g, err := ctx.Cache().GetString("g")
53+
if err != nil {
54+
g = err.Error()
55+
}
56+
_, err = ctx.Cache().Incr("count")
57+
ctx.WriteString("One [" + g + "] " + fmt.Sprint(err))
5158
}
5259

5360
func Two(ctx *dotweb.HttpContext) {
54-
g := ctx.Cache().GetString("g")
55-
ctx.Cache().Incr("count")
56-
ctx.WriteString("Two [" + g + "] [" + ctx.Cache().GetString("count") + "]")
61+
g, err := ctx.Cache().GetString("g")
62+
if err != nil {
63+
g = err.Error()
64+
}
65+
_, err = ctx.Cache().Incr("count")
66+
c, _ := ctx.Cache().GetString("count")
67+
ctx.WriteString("Two [" + g + "] [" + c + "] " + fmt.Sprint(err))
5768
}
5869

5970
func InitRoute(server *dotweb.HttpServer) {

framework/redis/redisutil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func newPool(redisIP string) *redis.Pool {
3434
Dial: func() (redis.Conn, error) {
3535
c, err := redis.Dial("tcp", redisIP)
3636
if err != nil {
37-
panic(err)
37+
//panic(err)
3838
}
3939
return c, err
4040
},

server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,9 @@ func (server *HttpServer) wrapWebSocketHandle(handle HttpHandle) websocket.Handl
338338
errmsg = exception.CatchError("httpserver::WebsocketHandle", LogTarget_HttpServer, err)
339339

340340
//记录访问日志
341-
headinfo := fmt.Sprintln(httpCtx.WebSocket.Conn.Request().Header)
341+
headinfo := fmt.Sprintln(httpCtx.WebSocket.Request().Header)
342342
logJson := LogJson{
343-
RequestUrl: httpCtx.WebSocket.Conn.Request().RequestURI,
343+
RequestUrl: httpCtx.WebSocket.Request().RequestURI,
344344
HttpHeader: headinfo,
345345
HttpBody: errmsg,
346346
}

session/store_redis.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ func (store *RedisStore) SessionRead(sessionId string) (*SessionState, error) {
3535
redisClient := redisutil.GetRedisClient(store.serverIp)
3636
key := getRedisKey(sessionId)
3737
kvs, err := redisClient.Get(key)
38+
if err != nil {
39+
return nil, err
40+
}
3841
var kv map[interface{}]interface{}
3942
if len(kvs) == 0 {
4043
kv = make(map[interface{}]interface{})

uploadfile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func NewUploadFile(file multipart.File, header *multipart.FileHeader) *UploadFil
2626
}
2727

2828
// 获取文件大小的接口
29-
type Sizer interface {
29+
type sizer interface {
3030
Size() int64
3131
}
3232

@@ -38,7 +38,7 @@ func (f *UploadFile) FileName() string {
3838
//get upload file size
3939
func (f *UploadFile) Size() int64 {
4040
if f.fileSize <= 0 {
41-
if sizer, ok := f.File.(Sizer); ok {
41+
if sizer, ok := f.File.(sizer); ok {
4242
f.fileSize = sizer.Size()
4343
}
4444
}

version.MD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
##dotweb版本记录:
22

3+
#### Version 0.3.9.3
4+
* 调整Cache接口,Exists\Get\GetString\GetInt\GetInt64增加error返回值,影响Cache模块、Session模块
5+
* Redis无法连接信息将从接口error返回,取消原来的panic设计
6+
* 2017-04-04 16:00
7+
38
#### Version 0.3.9.2
49
* Render增加SetTemplatePath接口,用于设置模板默认目录, 默认添加base、base/templates、base/views
510
* 模板查找顺序从最后一个插入的元素开始往前找

websocket.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ package dotweb
22

33
import (
44
"golang.org/x/net/websocket"
5+
"net/http"
56
)
67

78
type WebSocket struct {
89
Conn *websocket.Conn
910
}
1011

12+
//get http request
13+
func (ws *WebSocket) Request() *http.Request {
14+
return ws.Conn.Request()
15+
}
16+
1117
//send message from websocket.conn
12-
func (ws *WebSocket) SendMessage(msg string) {
13-
websocket.Message.Send(ws.Conn, msg)
18+
func (ws *WebSocket) SendMessage(msg string) error {
19+
return websocket.Message.Send(ws.Conn, msg)
1420
}
1521

1622
//read message from websocket.conn

0 commit comments

Comments
 (0)