| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- // Author: simon (ynwdlxm@163.com)
- // Date: 2025/7/21 10:10
- // Desc: 缓存包
- package cache
- import (
- "encoding/json"
- "sync"
- "time"
- "github.com/spf13/cast"
- "github.com/runningwater/gohub/pkg/logger"
- )
- type Service struct {
- Store Store
- }
- var once sync.Once
- var Cache *Service
- func InitWithCacheStore(store Store) {
- once.Do(func() {
- Cache = &Service{
- Store: store,
- }
- })
- }
- func Set(key string, value any, expire time.Duration) {
- bytes, err := json.Marshal(value)
- logger.LogIf(err)
- Cache.Store.Set(key, string(bytes), expire)
- }
- func Get(key string) any {
- stringValue := Cache.Store.Get(key)
- var wanted any
- err := json.Unmarshal([]byte(stringValue), &wanted)
- logger.LogIf(err)
- return wanted
- }
- func Has(key string) bool {
- return Cache.Store.Has(key)
- }
- // GetObject 获取对象,用法如下:
- //
- // model := user.User{}
- // cache.GetObject("user:1", &model)
- func GetObject(key string, wanted any) {
- val := Cache.Store.Get(key)
- if len(val) > 0 {
- err := json.Unmarshal([]byte(val), &wanted)
- logger.LogIf(err)
- } else {
- logger.WarnString("cache", "GetObject", "cache key ["+key+"] not found")
- }
- }
- func GetString(key string) string {
- return cast.ToString(Get(key))
- }
- func GetBool(key string) bool {
- return cast.ToBool(Get(key))
- }
- func GetInt(key string) int {
- return cast.ToInt(Get(key))
- }
- func GetInt32(key string) int32 {
- return cast.ToInt32(Get(key))
- }
- func GetInt64(key string) int64 {
- return cast.ToInt64(Get(key))
- }
- func GetUint(key string) uint {
- return cast.ToUint(Get(key))
- }
- func GetUint32(key string) uint32 {
- return cast.ToUint32(Get(key))
- }
- func GetUint64(key string) uint64 {
- return cast.ToUint64(Get(key))
- }
- func GetFloat32(key string) float32 {
- return cast.ToFloat32(Get(key))
- }
- func GetFloat64(key string) float64 {
- return cast.ToFloat64(Get(key))
- }
- func GetTime(key string) time.Time {
- return cast.ToTime(Get(key))
- }
- func GetDuration(key string) time.Duration {
- return cast.ToDuration(Get(key))
- }
- func GetIntSlice(key string) []int {
- return cast.ToIntSlice(Get(key))
- }
- func GetStringSlice(key string) []string {
- return cast.ToStringSlice(Get(key))
- }
- func GetStringMap(key string) map[string]string {
- return cast.ToStringMapString(Get(key))
- }
- func GetStringMapString(key string) map[string]string {
- return cast.ToStringMapString(Get(key))
- }
- func GetStringMapStringSlice(key string) map[string][]string {
- return cast.ToStringMapStringSlice(Get(key))
- }
- func GetStringMapInt(key string) map[string]int {
- return cast.ToStringMapInt(Get(key))
- }
- func Forget(key string) {
- Cache.Store.Forget(key)
- }
- func Forever(key string, value string) {
- Cache.Store.Set(key, value, 0)
- }
- func Flush() {
- Cache.Store.Flush()
- }
- func Increment(params ...any) {
- Cache.Store.Increment(params...)
- }
- func Decrement(params ...any) {
- Cache.Store.Decrement(params...)
- }
- func IsAlive() error {
- return Cache.Store.IsAlive()
- }
|