login_access.go 1.9 KB
package cache

import (
	"time"

	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
)

//登录凭证缓存
type LoginTokenCache struct {
}

func (ca LoginTokenCache) keyAccessToken(account string, platform string) string {
	str := KEY_PREFIX + "accesstoken:" + account + ":" + platform
	return str
}

func (ca LoginTokenCache) keyRefreshToken(account string, platform string) string {
	str := KEY_PREFIX + "refreshtoken" + account + ":" + platform
	return str
}

func (ca LoginTokenCache) SaveAccessToken(access *domain.LoginAccess) error {
	nowTime := time.Now().Unix()
	exp := access.AccessExpired - nowTime
	if exp <= 0 {
		exp = 60 * 60 * 2
	}
	key := ca.keyAccessToken(access.Account, access.Platform)
	result := clientRedis.Set(key, access.AccessToken, time.Duration(exp))
	return result.Err()
}

func (ca LoginTokenCache) RemoveAccessToken(account string, platform string) error {
	key := ca.keyAccessToken(account, platform)
	result := clientRedis.Del(key)
	return result.Err()
}

func (ca LoginTokenCache) GetAccessToken(account string, platform string) (string, error) {
	key := ca.keyAccessToken(account, platform)
	result := clientRedis.Get(key)
	return result.Result()
}

func (ca LoginTokenCache) SaveRefreshToken(access *domain.LoginAccess) error {
	nowTime := time.Now().Unix()
	exp := access.RefreshExpired - nowTime
	if exp <= 0 {
		exp = 60 * 60 * 2
	}
	key := ca.keyAccessToken(access.Account, access.Platform)
	result := clientRedis.Set(key, access.RefreshToken, time.Duration(exp))
	return result.Err()
}

func (ca LoginTokenCache) RemoveRefreshToken(account string, platform string) error {
	key := ca.keyRefreshToken(account, platform)
	result := clientRedis.Del(key)
	return result.Err()
}

func (ca LoginTokenCache) GetRefreshToken(account string, platform string) (string, error) {
	key := ca.keyRefreshToken(account, platform)
	result := clientRedis.Get(key)
	return result.Result()
}