1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package utils
- import (
- "gd_auth_check/common.in/cache"
- "encoding/json"
- "errors"
- "fmt"
- )
- func RedisSet(tabname string, tail string, value interface{}) error {
- bytes, err := json.Marshal(value)
- if err != nil {
- return err
- }
- key := fmt.Sprintf("%s-%s", tabname, tail)
- _, err = cache.Redis.Set(key, string(bytes))
- return err
- }
- func RedisSetEx(key string, seconds int64, value interface{}) error {
- bytes, err := json.Marshal(value)
- if err != nil {
- return err
- }
- _, err = cache.Redis.SetEx(key, seconds, string(bytes))
- return err
- }
- func RedisSetNxEx(key string, seconds int64, value interface{}) (bool,error) {
- bytes, err := json.Marshal(value)
- if err != nil {
- return false ,err
- }
- isSucess, err := cache.Redis.SetNxEx(key, string(bytes), seconds)
- return isSucess,err
- }
- func RedisGetKey(option string) (string, error) {
- array, err := cache.Redis.Keys(option)
- if err != nil {
- return "", err
- }
- if len(array) == 0 {
- return "", errors.New("no record")
- }
- return fmt.Sprintf("%s", array[0]), nil
- }
- func RedisGet(key string, value interface{}) error {
- ret, err := cache.Redis.Get(key)
- if err != nil {
- return err
- }
- if ret == "" {
- return errors.New("empty")
- }
- err = json.Unmarshal([]byte(ret), value)
- return err
- }
- func RedisDelKey(key string) error {
- _, err := cache.Redis.Del(key)
- return err
- }
|