Documentation ¶
Overview ¶
cache 是一个可以使用go-cache或redis作为缓存存储的缓存库。 默认使用基于内存的go-cache(patrickmn/go-cache)作为缓存存储, 缓存过期时间为5分钟,可以通过InitCache方法修改默认缓存存储。 提供了Set,SetNX,Get,GetWithTLL,Delete这些常用操作缓存的方法。
Index ¶
- func Delete(key string) error
- func Get(key string) (interface{}, error)
- func GetStore() store.StoreInterface
- func GetType() string
- func GetWithTTL(key string) (interface{}, time.Duration, error)
- func InitCache(store store.StoreInterface)
- func Set(key string, val interface{}, expiration time.Duration) error
- func SetNX(key string, val interface{}, expiration time.Duration) bool
- type Cache
- func (c *Cache) Clear() error
- func (c *Cache) Delete(key string) error
- func (c *Cache) Get(key string) (interface{}, error)
- func (c *Cache) GetStore() store.StoreInterface
- func (c *Cache) GetType() string
- func (c *Cache) GetWithTTL(key string) (interface{}, time.Duration, error)
- func (c *Cache) Set(key string, value interface{}, expiration time.Duration) error
- func (c *Cache) SetNX(key string, value interface{}, expiration time.Duration) bool
- type CacheInterface
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Delete ¶
删除key对应的缓存。
Example ¶
err := Delete("my-key") if err != nil { log.Fatal(err) }
Output:
func Get ¶
根据key获取value,如果获取不到,返回ErrValueNotFound错误。
Example ¶
err := Set("my-key", "my-value", 0) if err != nil { log.Fatal(err) } val, err := Get("my-key") if err != nil { if err == store.ErrValueNotFound { fmt.Println(err.Error()) } else { log.Fatal(err) } } fmt.Println(val)
Output: my-value
func GetWithTTL ¶
根据key获取value和value在缓存的有限期。
Example ¶
err := Set("my-key", "my-value", 0) if err != nil { log.Fatal(err) } value, ttl, err := GetWithTTL("my-key") if err != nil { if err == store.ErrValueNotFound { fmt.Println(err.Error()) } else { log.Fatal(err) } } fmt.Println(value, ttl)
Output:
func InitCache ¶
func InitCache(store store.StoreInterface)
使用自定义的存储初始化全局cache。
Example ¶
redisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"}) redisStore := store.NewRedis(redisClient, nil) InitCache(redisStore) err := Set("my-key", "my-value", 0) if err != nil { log.Fatal(err) }
Output:
func Set ¶
添加key对应的value到缓存中。 有效期expiration为0,则使用创建缓存存储时的options的expiration属性,默认缓存存储go-cache的过期时间为5分钟。
Example ¶
err := Set("my-key", "my-value", 0) if err != nil { log.Fatal(err) }
Output:
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache
func (*Cache) GetWithTTL ¶
GetWithTTL 如果缓存中存在key对应的缓存对象,则返回该对象和相应的TTL, 否则返回ErrValueNotFound
type CacheInterface ¶
type CacheInterface interface { store.StoreInterface GetStore() store.StoreInterface }
Directories ¶
Path | Synopsis |
---|---|
test
|
|
mocks/store
Package mocks is a generated GoMock package.
|
Package mocks is a generated GoMock package. |
mocks/store/clients
Package mocks is a generated GoMock package.
|
Package mocks is a generated GoMock package. |
Click to show internal directories.
Click to hide internal directories.