Fixed #69 Support redis interface.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-05-02 12:06:08 +08:00
parent 0fe9313453
commit a8851981c2
2 changed files with 162 additions and 0 deletions

116
storage/redis/redis.go Normal file
View File

@ -0,0 +1,116 @@
package redis
import (
"github.com/appleboy/gorush/gorush"
"gopkg.in/redis.v3"
"log"
"strconv"
)
const (
gorushTotalCount = "gorush-total-count"
gorushIosSuccess = "gorush-ios-success-count"
gorushIosError = "gorush-ios-error-count"
gorushAndroidSuccess = "gorush-android-success-count"
gorushAndroidError = "gorush-android-error-count"
)
var RedisClient *redis.Client
// Storage implements the storage interface for gorush (https://github.com/appleboy/gorush)
func New(config gorush.ConfYaml, stat gorush.StatusApp) *Storage {
return &Storage{
stat: stat,
config: config,
}
}
func getRedisInt64Result(key string, count *int64) {
val, _ := RedisClient.Get(key).Result()
*count, _ = strconv.ParseInt(val, 10, 64)
}
type Storage struct {
config gorush.ConfYaml
stat gorush.StatusApp
}
func (s *Storage) initRedis() error {
RedisClient = redis.NewClient(&redis.Options{
Addr: s.config.Stat.Redis.Addr,
Password: s.config.Stat.Redis.Password,
DB: s.config.Stat.Redis.DB,
})
_, err := RedisClient.Ping().Result()
if err != nil {
// redis server error
log.Println("Can't connect redis server: " + err.Error())
return err
}
s.stat.TotalCount = s.getTotalCount()
s.stat.Ios.PushSuccess = s.getIosSuccess()
s.stat.Ios.PushError = s.getIosError()
s.stat.Android.PushSuccess = s.getAndroidSuccess()
s.stat.Android.PushError = s.getAndroidError()
return nil
}
func (s *Storage) addTotalCount(count int64) {
RedisClient.Set(gorushTotalCount, strconv.Itoa(int(count)), 0)
}
func (s *Storage) addIosSuccess(count int64) {
RedisClient.Set(gorushIosSuccess, strconv.Itoa(int(count)), 0)
}
func (s *Storage) addIosError(count int64) {
RedisClient.Set(gorushIosError, strconv.Itoa(int(count)), 0)
}
func (s *Storage) addAndroidSuccess(count int64) {
RedisClient.Set(gorushAndroidSuccess, strconv.Itoa(int(count)), 0)
}
func (s *Storage) addAndroidError(count int64) {
RedisClient.Set(gorushAndroidError, strconv.Itoa(int(count)), 0)
}
func (s *Storage) getTotalCount() int64 {
var count int64
getRedisInt64Result(gorushTotalCount, &count)
return count
}
func (s *Storage) getIosSuccess() int64 {
var count int64
getRedisInt64Result(gorushIosSuccess, &count)
return count
}
func (s *Storage) getIosError() int64 {
var count int64
getRedisInt64Result(gorushIosError, &count)
return count
}
func (s *Storage) getAndroidSuccess() int64 {
var count int64
getRedisInt64Result(gorushAndroidSuccess, &count)
return count
}
func (s *Storage) getAndroidError() int64 {
var count int64
getRedisInt64Result(gorushAndroidError, &count)
return count
}

View File

@ -0,0 +1,46 @@
package redis
import (
"github.com/appleboy/gorush/gorush"
"github.com/stretchr/testify/assert"
"testing"
)
func TestRedisServerError(t *testing.T) {
config := gorush.BuildDefaultPushConf()
config.Stat.Redis.Addr = "localhost:6370"
redis := New(config, gorush.StatusApp{})
err := redis.initRedis()
assert.Error(t, err)
}
func TestRedisEngine(t *testing.T) {
var val int64
config := gorush.BuildDefaultPushConf()
redis := New(config, gorush.StatusApp{})
redis.initRedis()
redis.addTotalCount(1)
val = redis.getTotalCount()
assert.Equal(t, int64(1), val)
redis.addIosSuccess(2)
val = redis.getIosSuccess()
assert.Equal(t, int64(2), val)
redis.addIosError(3)
val = redis.getIosError()
assert.Equal(t, int64(3), val)
redis.addAndroidSuccess(4)
val = redis.getAndroidSuccess()
assert.Equal(t, int64(4), val)
redis.addAndroidError(5)
val = redis.getAndroidError()
assert.Equal(t, int64(5), val)
}