backend/tmp/reborn/domain/repository/cache.go

64 lines
1.7 KiB
Go

package repository
import (
"context"
"time"
)
// CacheRepository 快取 Repository 介面
type CacheRepository interface {
// Get 取得快取
Get(ctx context.Context, key string) (string, error)
// Set 設定快取
Set(ctx context.Context, key, value string, ttl time.Duration) error
// Delete 刪除快取
Delete(ctx context.Context, keys ...string) error
// Exists 檢查快取是否存在
Exists(ctx context.Context, key string) (bool, error)
// GetObject 取得物件快取
GetObject(ctx context.Context, key string, dest interface{}) error
// SetObject 設定物件快取
SetObject(ctx context.Context, key string, value interface{}, ttl time.Duration) error
// DeletePattern 根據模式刪除快取
DeletePattern(ctx context.Context, pattern string) error
}
// Cache Keys 定義
const (
// 權限樹快取 key
CacheKeyPermissionTree = "permission:tree"
// 使用者權限快取 key: user:permission:{uid}
CacheKeyUserPermissionPrefix = "user:permission:"
// 角色權限快取 key: role:permission:{role_uid}
CacheKeyRolePermissionPrefix = "role:permission:"
// 角色策略快取 key: role:policy:{role_id}
CacheKeyRolePolicyPrefix = "role:policy:"
// 權限列表快取 key
CacheKeyPermissionList = "permission:list:active"
)
// CacheKeyUserPermission 使用者權限快取 key
func CacheKeyUserPermission(uid string) string {
return CacheKeyUserPermissionPrefix + uid
}
// CacheKeyRolePermission 角色權限快取 key
func CacheKeyRolePermission(roleUID string) string {
return CacheKeyRolePermissionPrefix + roleUID
}
// CacheKeyRolePolicy 角色策略快取 key
func CacheKeyRolePolicy(roleID int64) string {
return CacheKeyRolePolicyPrefix + string(rune(roleID))
}