Documentation
¶
Overview ¶
*
- 基于key/val内存缓存设计
- 1. 支持过期时间
- 2. 缓存读写是安全并发的
- 3. 当前机器重启后,就会丢失缓存
- 4. 缓存默认不过期,除非Set设置了过期时间为非0
- 5. janitor对key的缓存时间,定期处理
Index ¶
- Constants
- type Cache
- func (c Cache) Add(k string, x interface{}, d time.Duration) error
- func (c Cache) Delete(k string)
- func (c Cache) DeleteExpired()
- func (c Cache) Flush()
- func (c Cache) Get(k string) (interface{}, bool)
- func (c Cache) Increment(k string, n int64) error
- func (c Cache) Item() map[string]Item
- func (c Cache) ItemCount() int
- func (c Cache) OnEvicted(f func(string, interface{}))
- func (c Cache) Replace(k string, x interface{}, d time.Duration) error
- func (c Cache) Set(k string, x interface{}, d time.Duration)
- type Item
Constants ¶
const ( // For use with functions that take an expiration time. NoExpiration time.Duration = -1 DefaultExpiration time.Duration = 0 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
use for outer struct
func NewFactory ¶
Return a new cache with a given default expiration duration and cleanup interval.
func (Cache) Add ¶
Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns an error otherwise.
func (Cache) Delete ¶
func (c Cache) Delete(k string)
Delete an item from the cache. Does nothing if the key is not in the cache.
func (Cache) DeleteExpired ¶
func (c Cache) DeleteExpired()
Delete all expired items from the cache.
func (Cache) Get ¶
Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.
func (Cache) Increment ¶
Increment an item of number (int, TODO other type).Returns an error if the item's value is not an integer, if it was not found, or if it is not possible to increment it by n. To retrieve the incremented value, use one of the specialized methods, e.g. IncrementInt64.
func (Cache) ItemCount ¶
func (c Cache) ItemCount() int
Return the number of items in the cache. Equivalent to len(c.Items()).
func (Cache) OnEvicted ¶
func (c Cache) OnEvicted(f func(string, interface{}))
Sets an (optional) function that is called with the key and value when an item is evicted from the cache. (Including when it is deleted manually, but not when it is overwritten.) Set to nil to disable.