| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 | 
							- package sso
 
- import (
 
- 	"encoding/json"
 
- 	"fmt"
 
- 	"kpt-pasture/config"
 
- 	"time"
 
- 	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
 
- 	"gitee.com/xuyiping_admin/pkg/tool"
 
- 	"gitee.com/xuyiping_admin/pkg/xerr"
 
- 	"github.com/go-redis/redis"
 
- 	redisv7 "github.com/go-redis/redis/v7"
 
- )
 
- type Cache struct {
 
- 	Client *redisv7.Client
 
- 	Expiry time.Duration
 
- }
 
- func NewCache(cfg *config.AppConfig) *Cache {
 
- 	return &Cache{
 
- 		Client: NewClientLatest(cfg),
 
- 		Expiry: time.Duration(cfg.RedisSetting.CacheRedis.Expiry) * time.Minute,
 
- 	}
 
- }
 
- func (c *Cache) Auth(userAuth *operationPb.UserAuthData) (string, error) {
 
- 	return c.get(fmt.Sprintf("sso:auth:%s", tool.Md5String(fmt.Sprintf("%s-%s", userAuth.UserName, userAuth.Password))))
 
- }
 
- func (c *Cache) CacheAuth(token string, res interface{}) error {
 
- 	return c.set(fmt.Sprintf("sso:auth:%s", token), res)
 
- }
 
- func (c *Cache) GetAccount(token string) (interface{}, error) {
 
- 	return c.get(fmt.Sprintf("sso:get_account:%s", token))
 
- }
 
- func (c *Cache) CacheSetAccount(token string, res interface{}) error {
 
- 	return c.set(fmt.Sprintf("sso:get_account:%s", token), res)
 
- }
 
- func (c *Cache) get(key string) (string, error) {
 
- 	bs, err := c.Client.Get(key).Bytes()
 
- 	if err != nil {
 
- 		return "", xerr.WithMessage(err, key)
 
- 	}
 
- 	if len(bs) == 0 {
 
- 		return "", xerr.WithStack(redis.Nil)
 
- 	}
 
- 	return string(bs), nil
 
- }
 
- func (c *Cache) set(key string, res interface{}) error {
 
- 	if res == nil {
 
- 		return nil
 
- 	}
 
- 	b, _ := json.Marshal(res)
 
- 	if err := c.Client.Set(key, string(b), c.Expiry).Err(); err != nil {
 
- 		return xerr.WithMessage(err, key, string(b))
 
- 	}
 
- 	return nil
 
- }
 
 
  |