gorush/storage/redis/redis.go

117 lines
2.6 KiB
Go

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
}