cache

package
v1.0.67 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2022 License: Apache-2.0 Imports: 8 Imported by: 8

Documentation

Index

Constants

View Source
const (
	// For use with functions that take an expiration time.
	NoExpiration time.Duration = -1
	// For use with functions that take an expiration time. Equivalent to
	// passing in the same expiration duration as was given to New() or
	// NewFrom() when the cache was created (e.g. 5 minutes.)
	DefaultExpiration time.Duration = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache added in v1.0.11

type Cache struct {
	// contains filtered or unexported fields
}

func New added in v1.0.11

func New(defaultExpiration, cleanupInterval time.Duration) *Cache

Return a new cache with a given default expiration duration and cleanup interval. If the expiration duration is less than one (or NoExpiration), the items in the cache never expire (by default), and must be deleted manually. If the cleanup interval is less than one, expired items are not deleted from the cache before calling c.DeleteExpired().

func NewFrom added in v1.0.11

func NewFrom(defaultExpiration, cleanupInterval time.Duration, items map[string]Item) *Cache

Return a new cache with a given default expiration duration and cleanup interval. If the expiration duration is less than one (or NoExpiration), the items in the cache never expire (by default), and must be deleted manually. If the cleanup interval is less than one, expired items are not deleted from the cache before calling c.DeleteExpired().

NewFrom() also accepts an items map which will serve as the underlying map for the cache. This is useful for starting from a deserialized cache (serialized using e.g. gob.Encode() on c.Items()), or passing in e.g. make(map[string]Item, 500) to improve startup performance when the cache is expected to reach a certain minimum size.

Only the cache's methods synchronize access to this map, so it is not recommended to keep any references to the map around after creating a cache. If need be, the map can be accessed at a later point using c.Items() (subject to the same caveat.)

Note regarding serialization: When using e.g. gob, make sure to gob.Register() the individual types stored in the cache before encoding a map retrieved with c.Items(), and to register those same types before decoding a blob containing an items map.

func (Cache) Add added in v1.0.11

func (c Cache) Add(k string, x interface{}, d time.Duration) error

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) Decrement added in v1.0.11

func (c Cache) Decrement(k string, n int64) error

Decrement an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found, or if it is not possible to decrement it by n. To retrieve the decremented value, use one of the specialized methods, e.g. DecrementInt64.

func (Cache) DecrementFloat added in v1.0.11

func (c Cache) DecrementFloat(k string, n float64) error

Decrement an item of type float32 or float64 by n. Returns an error if the item's value is not floating point, if it was not found, or if it is not possible to decrement it by n. Pass a negative number to decrement the value. To retrieve the decremented value, use one of the specialized methods, e.g. DecrementFloat64.

func (Cache) DecrementFloat32 added in v1.0.11

func (c Cache) DecrementFloat32(k string, n float32) (float32, error)

Decrement an item of type float32 by n. Returns an error if the item's value is not an float32, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementFloat64 added in v1.0.11

func (c Cache) DecrementFloat64(k string, n float64) (float64, error)

Decrement an item of type float64 by n. Returns an error if the item's value is not an float64, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementInt added in v1.0.11

func (c Cache) DecrementInt(k string, n int) (int, error)

Decrement an item of type int by n. Returns an error if the item's value is not an int, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementInt16 added in v1.0.11

func (c Cache) DecrementInt16(k string, n int16) (int16, error)

Decrement an item of type int16 by n. Returns an error if the item's value is not an int16, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementInt32 added in v1.0.11

func (c Cache) DecrementInt32(k string, n int32) (int32, error)

Decrement an item of type int32 by n. Returns an error if the item's value is not an int32, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementInt64 added in v1.0.11

func (c Cache) DecrementInt64(k string, n int64) (int64, error)

Decrement an item of type int64 by n. Returns an error if the item's value is not an int64, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementInt8 added in v1.0.11

func (c Cache) DecrementInt8(k string, n int8) (int8, error)

Decrement an item of type int8 by n. Returns an error if the item's value is not an int8, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUint added in v1.0.11

func (c Cache) DecrementUint(k string, n uint) (uint, error)

Decrement an item of type uint by n. Returns an error if the item's value is not an uint, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUint16 added in v1.0.11

func (c Cache) DecrementUint16(k string, n uint16) (uint16, error)

Decrement an item of type uint16 by n. Returns an error if the item's value is not an uint16, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUint32 added in v1.0.11

func (c Cache) DecrementUint32(k string, n uint32) (uint32, error)

Decrement an item of type uint32 by n. Returns an error if the item's value is not an uint32, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUint64 added in v1.0.11

func (c Cache) DecrementUint64(k string, n uint64) (uint64, error)

Decrement an item of type uint64 by n. Returns an error if the item's value is not an uint64, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUint8 added in v1.0.11

func (c Cache) DecrementUint8(k string, n uint8) (uint8, error)

Decrement an item of type uint8 by n. Returns an error if the item's value is not an uint8, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUintptr added in v1.0.11

func (c Cache) DecrementUintptr(k string, n uintptr) (uintptr, error)

Decrement an item of type uintptr by n. Returns an error if the item's value is not an uintptr, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) Delete added in v1.0.11

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 added in v1.0.11

func (c Cache) DeleteExpired()

Delete all expired items from the cache.

func (Cache) Flush added in v1.0.11

func (c Cache) Flush()

Delete all items from the cache.

func (Cache) Get added in v1.0.11

func (c Cache) Get(k string) (interface{}, bool)

Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.

func (Cache) GetWithExpiration added in v1.0.11

func (c Cache) GetWithExpiration(k string) (interface{}, time.Time, bool)

GetWithExpiration returns an item and its expiration time from the cache. It returns the item or nil, the expiration time if one is set (if the item never expires a zero value for time.Time is returned), and a bool indicating whether the key was found.

func (Cache) Increment added in v1.0.11

func (c Cache) Increment(k string, n int64) error

Increment an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64, float32 or float64 by n. 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) IncrementFloat added in v1.0.11

func (c Cache) IncrementFloat(k string, n float64) error

Increment an item of type float32 or float64 by n. Returns an error if the item's value is not floating point, if it was not found, or if it is not possible to increment it by n. Pass a negative number to decrement the value. To retrieve the incremented value, use one of the specialized methods, e.g. IncrementFloat64.

func (Cache) IncrementFloat32 added in v1.0.11

func (c Cache) IncrementFloat32(k string, n float32) (float32, error)

Increment an item of type float32 by n. Returns an error if the item's value is not an float32, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementFloat64 added in v1.0.11

func (c Cache) IncrementFloat64(k string, n float64) (float64, error)

Increment an item of type float64 by n. Returns an error if the item's value is not an float64, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementInt added in v1.0.11

func (c Cache) IncrementInt(k string, n int) (int, error)

Increment an item of type int by n. Returns an error if the item's value is not an int, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementInt16 added in v1.0.11

func (c Cache) IncrementInt16(k string, n int16) (int16, error)

Increment an item of type int16 by n. Returns an error if the item's value is not an int16, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementInt32 added in v1.0.11

func (c Cache) IncrementInt32(k string, n int32) (int32, error)

Increment an item of type int32 by n. Returns an error if the item's value is not an int32, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementInt64 added in v1.0.11

func (c Cache) IncrementInt64(k string, n int64) (int64, error)

Increment an item of type int64 by n. Returns an error if the item's value is not an int64, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementInt8 added in v1.0.11

func (c Cache) IncrementInt8(k string, n int8) (int8, error)

Increment an item of type int8 by n. Returns an error if the item's value is not an int8, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUint added in v1.0.11

func (c Cache) IncrementUint(k string, n uint) (uint, error)

Increment an item of type uint by n. Returns an error if the item's value is not an uint, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUint16 added in v1.0.11

func (c Cache) IncrementUint16(k string, n uint16) (uint16, error)

Increment an item of type uint16 by n. Returns an error if the item's value is not an uint16, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUint32 added in v1.0.11

func (c Cache) IncrementUint32(k string, n uint32) (uint32, error)

Increment an item of type uint32 by n. Returns an error if the item's value is not an uint32, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUint64 added in v1.0.11

func (c Cache) IncrementUint64(k string, n uint64) (uint64, error)

Increment an item of type uint64 by n. Returns an error if the item's value is not an uint64, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUint8 added in v1.0.11

func (c Cache) IncrementUint8(k string, n uint8) (uint8, error)

Increment an item of type uint8 by n. Returns an error if the item's value is not an uint8, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUintptr added in v1.0.11

func (c Cache) IncrementUintptr(k string, n uintptr) (uintptr, error)

Increment an item of type uintptr by n. Returns an error if the item's value is not an uintptr, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) ItemCount added in v1.0.11

func (c Cache) ItemCount() int

Returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up.

func (Cache) Items added in v1.0.11

func (c Cache) Items() map[string]Item

Copies all unexpired items in the cache into a new map and returns it.

func (Cache) Load added in v1.0.11

func (c Cache) Load(r io.Reader) error

Add (Gob-serialized) cache items from an io.Reader, excluding any items with keys that already exist (and haven't expired) in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) LoadFile added in v1.0.11

func (c Cache) LoadFile(fname string) error

Load and add cache items from the given filename, excluding any items with keys that already exist in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) OnEvicted added in v1.0.11

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.

func (Cache) Replace added in v1.0.11

func (c Cache) Replace(k string, x interface{}, d time.Duration) error

Set a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns an error otherwise.

func (Cache) Save added in v1.0.11

func (c Cache) Save(w io.Writer) (err error)

Write the cache's items (using Gob) to an io.Writer.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) SaveFile added in v1.0.11

func (c Cache) SaveFile(fname string) error

Save the cache's items to the given filename, creating the file if it doesn't exist, and overwriting it if it does.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) Set added in v1.0.11

func (c Cache) Set(k string, x interface{}, d time.Duration)

Add an item to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

func (Cache) SetDefault added in v1.0.11

func (c Cache) SetDefault(k string, x interface{})

Add an item to the cache, replacing any existing item, using the default expiration.

type CacheManager

type CacheManager struct {
}

缓存管理器

func (*CacheManager) Brpop added in v1.0.51

func (self *CacheManager) Brpop(key string, expire int64, result interface{}) error

func (*CacheManager) Del

func (self *CacheManager) Del(key ...string) error

func (*CacheManager) Flush

func (self *CacheManager) Flush() error

func (*CacheManager) Get

func (self *CacheManager) Get(key string, input interface{}) (bool, error)

func (*CacheManager) Keys

func (self *CacheManager) Keys(pattern ...string) ([]string, error)

func (*CacheManager) Put

func (self *CacheManager) Put(key string, input interface{}, expire ...int) error

func (*CacheManager) Rpush added in v1.0.51

func (self *CacheManager) Rpush(key string, val interface{}) error

func (*CacheManager) Size

func (self *CacheManager) Size(pattern ...string) (int, error)

func (*CacheManager) Values

func (self *CacheManager) Values(pattern ...string) ([]interface{}, error)

type ICache

type ICache interface {
	// 查询
	Get(key string, input interface{}) (bool, error)
	// 保存/过期时间(秒)
	Put(key string, input interface{}, expire ...int) error
	// 删除
	Del(input ...string) error
	// 查询全部key数量
	Size(pattern ...string) (int, error)
	// 查询全部key
	Keys(pattern ...string) ([]string, error)
	// 查询全部key
	Values(pattern ...string) ([]interface{}, error)
	// 清空全部key-value
	Flush() error
	// 查询队列数据
	Brpop(key string, expire int64, result interface{}) error
	// 发送队列数据
	Rpush(key string, val interface{}) error
}

orm数据库接口

type Item added in v1.0.11

type Item struct {
	Object     interface{}
	Expiration int64
}

func (Item) Expired added in v1.0.11

func (item Item) Expired() bool

Returns true if the item has expired.

type LocalMapManager added in v1.0.11

type LocalMapManager struct {
	CacheManager
	// contains filtered or unexported fields
}

本地缓存管理器

func NewLocalCache added in v1.0.11

func NewLocalCache(a, b int) *LocalMapManager

a默认缓存时间/分钟 b默认校验数据间隔时间/分钟

func (*LocalMapManager) Del added in v1.0.11

func (self *LocalMapManager) Del(key ...string) error

func (*LocalMapManager) Flush added in v1.0.11

func (self *LocalMapManager) Flush() error

func (*LocalMapManager) Get added in v1.0.11

func (self *LocalMapManager) Get(key string, input interface{}) (bool, error)

func (*LocalMapManager) GetBy added in v1.0.11

func (self *LocalMapManager) GetBy(key string, input interface{}) (interface{}, bool, error)

func (*LocalMapManager) Put added in v1.0.11

func (self *LocalMapManager) Put(key string, input interface{}, expire ...int) error

func (*LocalMapManager) Size added in v1.0.11

func (self *LocalMapManager) Size(pattern ...string) (int, error)

数据量大时请慎用

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL