1 Star 1 Fork 0

Gousing/cache

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
global.go 13.36 KB
一键复制 编辑 原始数据 按行查看 历史
package cache
import (
"log/slog"
"net"
"sync"
"sync/atomic"
"time"
)
const defaultStoreType StoreType = StoreMemory
var (
globalStoreType = newAtomicStoreType()
globalStoreMemory = newAtomicStoreIO(StoreMemory)
globalStoreRedis = newAtomicStoreIO(StoreRedis)
)
// GetDefaultStoreType 获取默认缓存仓库的驱动类型 StoreType
func GetDefaultStoreType() StoreType {
return globalStoreType.Load()
}
// SetDefaultStoreType 设置默认缓存仓库的驱动类型 StoreType
func SetDefaultStoreType(t StoreType) {
switch t {
case StoreMemory:
globalStoreType.Store(StoreMemory)
case StoreRedis:
globalStoreType.Store(StoreRedis)
default:
logWrapAttr(getEntryPC(2, nil, false), slog.LevelError, "cache: SetStoreType, storeType["+t+"] does not exist")
}
}
// GetDefault 获取默认缓存实例 StoreIO
// - 如果默认实例未配置(未初始化),将自动尝试使用默认参数进行初始化
// - 用户可通过 `SetDefaultStoreType` 设置默认缓存实例的 StoreType 驱动类型(默认为 StoreMemory)
// - 用户可通过 `SetDefaultMemory` 设置自定义默认 StoreMemory 缓存实例
// - 用户可通过 `SetDefaultRedis` 设置自定义默认 StoreRedis 缓存实例
func GetDefault() CacheIO {
switch GetDefaultStoreType() {
case StoreMemory:
return globalStoreMemory.Load()
case StoreRedis:
return globalStoreRedis.Load()
default:
// 返回默认类型的 StoreMemory
logWrapAttr(getEntryPC(2, nil, false), slog.LevelWarn, "cache: GetStore, storeType does not exist, return default: StoreMemory")
return globalStoreMemory.Load()
}
}
// GetDefaultMemory 获取全局默认 StoreMemory 缓存实例
// - 用户可通过 `SetDefaultMemory` 设置自定义默认 StoreMemory 缓存实例
// - 如果默认实例未配置(未初始化),会尝试使用默认参数自动延迟初始化
func GetDefaultMemory() CacheIO {
return globalStoreMemory.Load()
}
// SetDefaultMemory 设置全局默认StoreMemory缓存实例
func SetDefaultMemory(io CacheIO) {
if io == nil {
logWrapAttr(getEntryPC(2, nil, false), slog.LevelError, "cache: SetDefaultMemory, io is nil")
return
}
globalStoreMemory.Store(io)
}
// GetDefaultRedis 全局StoreRedis缓存
// - 用户可通过 `SetDefaultRedis` 设置自定义默认 StoreRedis 缓存实例
// - 如果默认实例未配置(未初始化),会尝试使用默认参数自动延迟初始化
func GetDefaultRedis() CacheIO {
return globalStoreRedis.Load()
}
// SetDefaultRedis 设置全局默认StoreRedis缓存实例
func SetDefaultRedis(io CacheIO) {
if io == nil {
logWrapAttr(getEntryPC(2, nil, false), slog.LevelError, "cache: SetDefaultRedis, io is nil")
return
}
globalStoreRedis.Store(io)
}
// 默认缓存仓库包装代理 TagsIO / CacheIO
// Tags 入口
func Tags(tags ...string) TagsIO {
return GetDefault().Tags(tags...)
}
type atomicStoreType struct {
value atomic.Value
}
type atomicStoreIO struct {
storeType StoreType
value atomic.Value
once sync.Once
}
func newAtomicStoreType() *atomicStoreType {
av := &atomicStoreType{}
av.value.Store(defaultStoreType)
return av
}
func (ast *atomicStoreType) Store(t StoreType) {
ast.value.Store(t)
}
func (ast *atomicStoreType) Load() StoreType {
return ast.value.Load().(StoreType)
}
func newAtomicStoreIO(storeType StoreType) *atomicStoreIO {
av := &atomicStoreIO{
storeType: storeType,
}
return av
}
func (ast *atomicStoreIO) Store(lib CacheIO) {
if lib == nil {
logWrapAttr(getEntryPC(3, nil, false), slog.LevelError, "cache: SetStoreIO, lib is nil, storeType: "+ast.storeType)
return
}
ast.value.Store(lib)
}
func (ast *atomicStoreIO) Load() CacheIO {
if v, ok := ast.value.Load().(CacheIO); ok {
return v
}
// 自动检查一次,如未初始化则根据 storeType 使用默认参数自动延迟初始化
pc := getEntryPC(3, nil, false)
ast.once.Do(func() {
if _, ok := ast.value.Load().(CacheIO); !ok {
switch ast.storeType {
case StoreMemory:
if io, err := NewMemory(MemoryOptions{CacheSize: MemoryCacheSizeDefault, DefaultExpire: 0}); err == nil {
ast.value.Store(io)
} else {
logWrapAttr(pc, slog.LevelError, "cache: globalStoreMemory.Load, automatic initialization failed", slog.String("error", err.Error()))
panic("cache: globalStoreMemory.Load, automatic initialization failed, error: " + err.Error())
}
case StoreRedis:
if io, err := NewRedis(RedisOptions{DefaultExpire: 0}); err == nil {
ast.value.Store(io)
} else {
logWrapAttr(pc, slog.LevelError, "cache: globalStoreRedis.Load, automatic initialization failed", slog.String("error", err.Error()))
panic("cache: globalStoreRedis.Load, automatic initialization failed, error: " + err.Error())
}
default:
logWrapAttr(pc, slog.LevelError, "cache: globalStoreXX.Load, automatic initialization failed, storeType["+ast.storeType+"] does not exist")
panic("cache: globalStoreX.Load, automatic initialization failed, storeType[" + ast.storeType + "] does not exist")
}
}
})
return ast.value.Load().(CacheIO)
}
// Has 判断缓存是否存在
func Has(key string) bool {
return GetDefault().Has(key)
}
// Scan 方式获取缓存对象,适用于 Slice/Map/Struct 缓存对象, 反序列化扫描后放入引用地址变量
// 注意: 不建议直接获取任何非基本类型的缓存进行断言转换,Cache 驱动为Memory时工作正常,如为Redis则无法转换
func Scan(key string, refVal any) error {
return GetDefault().Scan(key, refVal)
}
// Get returns the value for the given key, ie: ([]byte, nil).
// If the value does not exists it returns (nil, error)
func Get(key string) ([]byte, error) {
return GetDefault().Get(key)
}
// GetString 读取 String
func GetString(key string) string {
return GetDefault().GetString(key)
}
// GetStringDefault 读取 String
// If the value does not exists it return defaultVal
func GetStringD(key string, defaultVal string) string {
return GetDefault().GetStringD(key, defaultVal)
}
// GetInt 读取 INT
func GetInt(key string) int {
return GetDefault().GetIntD(key, 0)
}
// GetIntDefault 读取 INT
// If the value does not exists it return defaultVal
func GetIntD(key string, defaultVal int) int {
return GetDefault().GetIntD(key, defaultVal)
}
// GetInt8 读取 INT8
func GetInt8(key string) int8 {
return GetDefault().GetInt8D(key, 0)
}
// GetInt8Default 读取 INT8
// If the value does not exists it return defaultVal
func GetInt8D(key string, defaultVal int8) int8 {
return GetDefault().GetInt8D(key, defaultVal)
}
// GetInt16 读取 INT16
func GetInt16(key string) int16 {
return GetDefault().GetInt16D(key, 0)
}
// GetInt16Default 读取 INT16
// If the value does not exists it return defaultVal
func GetInt16D(key string, defaultVal int16) int16 {
return GetDefault().GetInt16D(key, defaultVal)
}
// GetInt32 读取 INT32
func GetInt32(key string) int32 {
return GetDefault().GetInt32D(key, 0)
}
// GetInt32Default 读取 INT32
// If the value does not exists it return defaultVal
func GetInt32D(key string, defaultVal int32) int32 {
return GetDefault().GetInt32D(key, defaultVal)
}
// GetInt64 读取 INT64
func GetInt64(key string) int64 {
return GetDefault().GetInt64D(key, 0)
}
// GetInt64Default 读取 INT64
// If the value does not exists it return defaultVal
func GetInt64D(key string, defaultVal int64) int64 {
return GetDefault().GetInt64D(key, defaultVal)
}
// GetUint 读取 UINT
func GetUint(key string) uint {
return GetDefault().GetUintD(key, 0)
}
// GetUintDefault 读取 UINT
// If the value does not exists it return defaultVal
func GetUintD(key string, defaultVal uint) uint {
return GetDefault().GetUintD(key, defaultVal)
}
// GetUint8 读取 UINT8
func GetUint8(key string) uint8 {
return GetDefault().GetUint8D(key, 0)
}
// GetUint8Default 读取 UINT8
// If the value does not exists it return defaultVal
func GetUint8D(key string, defaultVal uint8) uint8 {
return GetDefault().GetUint8D(key, defaultVal)
}
// GetUint16 读取 UINT8
func GetUint16(key string) uint16 {
return GetDefault().GetUint16D(key, 0)
}
// GetUint16Default 读取 UINT8
// If the value does not exists it return defaultVal
func GetUint16D(key string, defaultVal uint16) uint16 {
return GetDefault().GetUint16D(key, defaultVal)
}
// GetUint32 读取 UINT32
func GetUint32(key string) uint32 {
return GetDefault().GetUint32D(key, 0)
}
// GetUint32Default 读取 UINT32
// If the value does not exists it return defaultVal
func GetUint32D(key string, defaultVal uint32) uint32 {
return GetDefault().GetUint32D(key, defaultVal)
}
// GetUint64 读取 UINT64
func GetUint64(key string) uint64 {
return GetDefault().GetUint64D(key, 0)
}
// GetUint64Default 读取 UINT64
// If the value does not exists it return defaultVal
func GetUint64D(key string, defaultVal uint64) uint64 {
return GetDefault().GetUint64D(key, defaultVal)
}
// GetFloat32 读取 FLOAT32
func GetFloat32(key string) float32 {
return GetDefault().GetFloat32D(key, 0)
}
// GetFloat32Default 读取 FLOAT32
// If the value does not exists it return defaultVal
func GetFloat32D(key string, defaultVal float32) float32 {
return GetDefault().GetFloat32D(key, defaultVal)
}
// GetFloat64 读取 FLOAT64
func GetFloat64(key string) float64 {
return GetDefault().GetFloat64D(key, 0)
}
// GetFloat64Default 读取 FLOAT64
// If the value does not exists it return defaultVal
func GetFloat64D(key string, defaultVal float64) float64 {
return GetDefault().GetFloat64D(key, defaultVal)
}
// GetBool 读取 BOOL
func GetBool(key string) bool {
return GetDefault().GetBoolD(key, false)
}
// GetBoolDefault 读取 BOOL
// If the value does not exists it return defaultVal
func GetBoolD(key string, defaultVal bool) bool {
return GetDefault().GetBoolD(key, defaultVal)
}
// GetTime
func GetTime(key string) time.Time {
return GetDefault().GetTimeD(key, time.Time{})
}
func GetTimeD(key string, defaultVal time.Time) time.Time {
return GetDefault().GetTimeD(key, defaultVal)
}
// GetDuration time.Duration => int64
func GetDuration(key string) time.Duration {
return GetDefault().GetDurationD(key, time.Duration(0))
}
func GetDurationD(key string, defaultVal time.Duration) time.Duration {
return GetDefault().GetDurationD(key, defaultVal)
}
func GetTimeMonth(key string) time.Month {
return GetDefault().GetTimeMonthD(key, time.Month(0))
}
func GetTimeMonthD(key string, defaultVal time.Month) time.Month {
return GetDefault().GetTimeMonthD(key, defaultVal)
}
func GetTimeWeekday(key string) time.Weekday {
return GetDefault().GetTimeWeekdayD(key, time.Weekday(0))
}
func GetTimeWeekdayD(key string, defaultVal time.Weekday) time.Weekday {
return GetDefault().GetTimeWeekdayD(key, defaultVal)
}
// GetIP
func GetIP(key string) net.IP {
return GetDefault().GetIPD(key, net.IP(nil))
}
// GetIPDefault net.IP(nil)
func GetIPD(key string, defaultVal net.IP) net.IP {
return GetDefault().GetIPD(key, defaultVal)
}
// Set 设置缓存(实例默认过期时间)
func Set(key string, val any) error {
return GetDefault().Set(key, val)
}
// SetExpire 设置缓存(标记过期时间)
func SetExpire(key string, val any, expire time.Duration) error {
return GetDefault().SetExpire(key, val, expire)
}
// SetNotExist 设置缓存(标记过期时间,为0表示永不过期),不存在则设置
func SetNotExist(key string, val any, expire time.Duration) error {
return GetDefault().SetNotExist(key, val, expire)
}
// ChangeInt 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)
// - expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间
func ChangeInt(key string, step int, expire time.Duration) (int, error) {
return GetDefault().ChangeInt(key, step, expire)
}
// ChangeInt32 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)
// - expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间
func ChangeInt32(key string, step int32, expire time.Duration) (int32, error) {
return GetDefault().ChangeInt32(key, step, expire)
}
// ChangeInt64 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)
// - expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间
func ChangeInt64(key string, step int64, expire time.Duration) (int64, error) {
return GetDefault().ChangeInt64(key, step, expire)
}
// ChangeFloat32 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)
// - expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间
func ChangeFloat32(key string, step float32, expire time.Duration) (float32, error) {
return GetDefault().ChangeFloat32(key, step, expire)
}
// ChangeFloat64 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)
// - expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间
func ChangeFloat64(key string, step float64, expire time.Duration) (float64, error) {
return GetDefault().ChangeFloat64(key, step, expire)
}
// Delete 删除缓存
func Del(keys ...string) error {
return GetDefault().Del(keys...)
}
// Clear 清空全部缓存
func Clear() error {
return GetDefault().Clear()
}
// Size 获取已缓存数据条目的数量
func Size() int64 {
return GetDefault().Size()
}
// Stats 获取已缓存数据条目的数量
func Stats() StoreStats {
return GetDefault().Stats()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/gousing/cache.git
git@gitee.com:gousing/cache.git
gousing
cache
cache
v1.1.1

搜索帮助