1212// RedisCache is redis cache adapter.
1313// it contains serverIp for redis conn.
1414type 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}
0 commit comments