| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package cache
- import (
- "context"
- "time"
- )
- type Redis struct {
- DB int `mapstructure:"db" json:"db" yaml:"db"`
- Addr string `mapstructure:"addr" json:"addr" yaml:"addr"`
- Password string `mapstructure:"password" json:"password" yaml:"password"`
- PoolSize int `mapstructure:"pool-size" json:"pool-size" yaml:"pool-size"`
- }
- func SetCache(key string, value interface{}, expiration time.Duration) error {
- err := Instance().Set(context.Background(), key, value, expiration).Err()
- if err != nil {
- return err
- }
- return nil
- }
- func GetCache(key string) (interface{}, error) {
- return Instance().Get(context.Background(), key).Result()
- }
- func DeleteCache(key string) (int64, error) {
- return Instance().Del(context.Background(), key).Result()
- }
- func GetCacheString(key string) (string, error) {
- value, err := GetCacheBytes(key)
- if err != nil {
- return "", err
- }
- return string(value), nil
- }
- func GetCacheBytes(key string) ([]byte, error) {
- return Instance().Get(context.Background(), key).Bytes()
- }
- func GetCacheUint(key string) (uint64, error) {
- return Instance().Get(context.Background(), key).Uint64()
- }
|