示例#1
0
func IncrBy(key string, incr int) int {
	var err error
	var val int
	defer utils.Recover("redis get falure")
	val, err = redis.IncrBy(key, incr)
	if err != nil {
		log.Redf("decode failure", err)
	}
	return val
}
示例#2
0
func Delete(key string) error {
	var err error
	defer utils.Recover("redis get falure")
	err = redis.Delete(key)

	if err != nil {
		log.Redf("decode failure", err)
	}
	return err
}
示例#3
0
func Get(key string, to interface{}) error {
	var err error
	defer utils.Recover("redis get falure")
	data := redis.Get(key)

	if data == nil {
		to = nil
		return errors.New("key point value is nil ")
	}
	err = utils.Decode(data.([]byte), to)
	if err != nil {
		log.Redf("decode failure", err)
	}
	return err
}
示例#4
0
func Hset(key string, field string, val interface{}, expire int64) error {
	var err error
	data, err := utils.Encode(val)

	if err != nil {
		return err
	}

	if data == nil {
		return errors.New("cc is nil")
	}

	defer utils.Recover("redis set falure")

	err = redis.Hset(key, field, data, expire)
	if err != nil {
		log.Redf("%v", err)
	}

	return err
}
示例#5
0
func InitRedis() {
	cacheConfig := beego.AppConfig.String("cache")

	log.Greenf("cacheConfig:%v \n", cacheConfig)

	var cc cache.Cache
	if "redis" == cacheConfig {
		var err error

		defer utils.Recover("redis init falure")

		cc, err = cache.NewCache("redis", `{"conn":"`+beego.AppConfig.String("redis_host")+`"}`)

		if err != nil {
			log.Redf("%v", err)
		}
		cache, ok := cc.(*caches.MyRedisCache)
		if ok {
			redis = cache
		} else {
			log.Redf("parse cache to MyRedisCache failure !")
		}
	}
}