1 Star 0 Fork 0

carlmax_my/console-core-go

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
store.go 2.64 KB
Copy Edit Raw Blame History
carlmax_my authored 2024-12-02 21:32 . init project
package store
import (
"encoding/json"
"time"
"gitee.com/carlmax_my/console-core-go/pkg/cache"
"gitee.com/carlmax_my/console-core-go/pkg/trace"
)
type BaseStore struct {
cache cache.Cache
ttl time.Duration
}
func New(cache cache.Cache) (*BaseStore, error) {
return &BaseStore{
cache: cache,
// ttl: configs.GetLoginSessionTTL(),
}, nil
}
func NewWithTTL(cache cache.Cache, ttl time.Duration) (*BaseStore, error) {
return &BaseStore{
cache: cache,
ttl: ttl,
}, nil
}
func (s *BaseStore) Set(key string, value interface{}, options ...cache.Option) error {
return s.cache.Set(key, value, s.ttl, options...)
}
func (s *BaseStore) SetT(key string, value interface{}, trace trace.T) error {
return s.Set(key, value, cache.WithTrace(trace))
}
func (s *BaseStore) SetModel(key string, value interface{}, options ...cache.Option) error {
if bytes, err := json.Marshal(value); err != nil {
return err
} else {
return s.cache.Set(key, bytes, s.ttl, options...)
}
}
func (s *BaseStore) SetModelT(key string, value interface{}, trace trace.T) error {
return s.SetModel(key, value, cache.WithTrace(trace))
}
func (s *BaseStore) Get(key string, options ...cache.Option) (string, error) {
return s.cache.Get(key, options...)
}
func (s *BaseStore) GetT(key string, trace trace.T) (string, error) {
return s.Get(key, cache.WithTrace(trace))
}
func (s *BaseStore) GetModel(key string, value interface{}, options ...cache.Option) error {
if str, err := s.cache.Get(key, options...); err != nil {
return err
} else {
return json.Unmarshal([]byte(str), value)
}
}
func (s *BaseStore) GetModelT(key string, value interface{}, trace trace.T) error {
return s.GetModel(key, value, cache.WithTrace(trace))
}
func (s *BaseStore) Del(key string, options ...cache.Option) (int64, error) {
return s.cache.Del(key, options...)
}
func (s *BaseStore) DelT(key string, trace trace.T) (int64, error) {
return s.Del(key, cache.WithTrace(trace))
}
func (s *BaseStore) CheckGet(key string, options ...cache.Option) (string, error) {
return s.cache.CheckGet(key, options...)
}
func (s *BaseStore) CheckGetT(key string, trace trace.T) (string, error) {
return s.CheckGet(key, cache.WithTrace(trace))
}
func (s *BaseStore) CheckGetModel(key string, value interface{}, options ...cache.Option) error {
if str, err := s.cache.CheckGet(key, options...); err != nil {
return err
} else {
return json.Unmarshal([]byte(str), value)
}
}
func (s *BaseStore) CheckGetModelT(key string, value interface{}, trace trace.T) error {
return s.CheckGetModel(key, value, cache.WithTrace(trace))
}
func (s *BaseStore) Exists(key string) (bool, error) {
return s.cache.Exists(key)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/carlmax_my/console-core-go.git
git@gitee.com:carlmax_my/console-core-go.git
carlmax_my
console-core-go
console-core-go
v0.0.27

Search