This commit is contained in:
Sun
2023-11-08 21:53:07 +08:00
commit 211c3071dc
245 changed files with 39293 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
package global
import (
"sun-panel/lib/cache"
"sun-panel/structs"
"time"
)
// 缓存驱动
const (
CACHE_DRIVE_REDIS = "redis"
CACHE_DRIVE_MEMORY = "memory"
)
// 创建一个缓存区
// | defaultExpiration:默认过期时长
// | cleanupInterval:清理过期的key间隔 0.不清理
// | name:缓存名称
func NewCache[T any](defaultExpiration time.Duration, cleanupInterval time.Duration, name string) cache.Cacher[T] {
drive := Config.GetValueString("base", "cache_drive")
if drive == "" {
drive = CACHE_DRIVE_MEMORY
}
var cacher cache.Cacher[T]
Logger.Debugln("缓存驱动:", drive)
switch drive {
case CACHE_DRIVE_MEMORY:
cacher = cache.NewGoCache[T](defaultExpiration, cleanupInterval)
case CACHE_DRIVE_REDIS:
redisConfig := structs.IniConfigRedis{}
if err := Config.GetSection("redis", &redisConfig); err != nil {
redisConfig.Prefix = ""
}
cacher = cache.NewRedisCache[T](RedisDb, redisConfig.Prefix+name, defaultExpiration, cleanupInterval)
}
return cacher
}
+39
View File
@@ -0,0 +1,39 @@
package global
import (
"sun-panel/initialize/database"
"sun-panel/lib/cache"
"sun-panel/lib/cmn/systemSetting"
"sun-panel/lib/iniConfig"
"sun-panel/lib/language"
"sun-panel/models"
redis "github.com/redis/go-redis/v9"
"go.uber.org/zap"
"gorm.io/gorm"
)
var (
ISDOCKER = "" // 是否为docker模式运行
RUNCODE = "debug" // 运行模式:debug | release
// DB_MYSQL = "mysql"
// DB_SQLITE = "sqlite"
DB_DRIVER = database.SQLITE
)
// var Log *cmn.LogStruct
var (
Lang *language.LangStructObj
UserToken cache.Cacher[models.User]
CUserToken cache.Cacher[string] // 用户token
Logger *zap.SugaredLogger
LoggerLevel = zap.NewAtomicLevel() // 支持通过http以及配置文件动态修改日志级别
VerifyCodeCachePool cache.Cacher[string]
Config *iniConfig.IniConfig
Db *gorm.DB
RedisDb *redis.Client
SystemSetting *systemSetting.SystemSettingCache
RateLimit *RateLimiter
)
+37
View File
@@ -0,0 +1,37 @@
package global
import (
"sun-panel/lib/queue"
"sun-panel/lib/queue/queueMemory"
"sun-panel/lib/queue/queueRedis"
"sun-panel/structs"
)
// 缓存驱动
const (
QUEUE_DRIVE_REDIS = "redis"
QUEUE_DRIVE_MEMORY = "memory"
)
// 创建一个队列
// name:缓存名称
func NewQueuer(name string) queue.Queuer {
drive := Config.GetValueString("base", "queue_drive")
if drive == "" {
drive = CACHE_DRIVE_MEMORY
}
var queuer queue.Queuer
Logger.Debugln("队列驱动:", drive)
switch drive {
case CACHE_DRIVE_MEMORY:
queuer = queueMemory.New()
case CACHE_DRIVE_REDIS:
redisConfig := structs.IniConfigRedis{}
if err := Config.GetSection("redis", &redisConfig); err != nil {
redisConfig.Prefix = ""
}
queuer = queueRedis.New(RedisDb, redisConfig.Prefix+name)
}
return queuer
}
+43
View File
@@ -0,0 +1,43 @@
package global
import (
"strconv"
"sun-panel/lib/cache"
)
type RateLimiter struct {
Minute cache.Cacher[int]
Hour cache.Cacher[int]
}
func (r *RateLimiter) MinuteAddOnce(userId uint) {
key := "user_" + strconv.Itoa(int(userId))
times := r.MinuteGet(userId) + 1
// fmt.Println("fen册数", times)
r.Minute.SetKeepExpiration(key, times)
// r.Minute.SetDefault(key, times)
}
func (r *RateLimiter) MinuteGet(userId uint) int {
if v, ok := r.Minute.Get("user_" + strconv.Itoa(int(userId))); !ok {
return 0
} else {
return v
}
}
func (r *RateLimiter) HourAddOnce(userId uint) {
key := "user_" + strconv.Itoa(int(userId))
times := r.HourGet(userId) + 1
// fmt.Println("hour册数", times)
r.Hour.SetKeepExpiration(key, times)
// r.Hour.SetDefault(key, times)
}
func (r *RateLimiter) HourGet(userId uint) int {
if v, ok := r.Hour.Get("user_" + strconv.Itoa(int(userId))); !ok {
return 0
} else {
return v
}
}