Documentation ¶
Index ¶
- Variables
- func BufferMarshal(result interface{}) ([]byte, error)
- func BufferUnmarshal(data []byte, result interface{}) error
- func CPUTicks() int64
- func FastRand() uint32
- func MemHash(data []byte) uint64
- func MemHashString(str string) uint64
- func Memclr(b []byte)
- func NanoTime() int64
- func NewRing(params RingCacheParams, opts ...CacheOption) *ringCache
- type Cache
- func (c *Cache) Add(key Key, value interface{}, ttl ...time.Duration)
- func (c *Cache) Get(key Key) (interface{}, bool)
- func (c *Cache) GetBytes(key Key) ([]byte, bool)
- func (c *Cache) Keys() []Key
- func (c *Cache) Len() int
- func (c *Cache) Peek(key Key) (interface{}, bool)
- func (c *Cache) PeekBytes(key Key) ([]byte, bool)
- func (c *Cache) Remove(key Key)
- func (c *Cache) TTL(key Key) time.Duration
- type CacheOption
- type Key
- type L2Cache
- func (l2 *L2Cache) Del(ctx context.Context, key string) (int64, error)
- func (l2 *L2Cache) Get(ctx context.Context, key string, result interface{}) error
- func (l2 *L2Cache) GetBytes(ctx context.Context, key string) ([]byte, error)
- func (l2 *L2Cache) GetIgnoreNilErr(ctx context.Context, key string, result interface{}) error
- func (l2 *L2Cache) Set(ctx context.Context, key string, value interface{}, ttl ...time.Duration) error
- func (l2 *L2Cache) SetBytes(ctx context.Context, key string, value []byte, ttl ...time.Duration) error
- func (l2 *L2Cache) TTL(ctx context.Context, key string) (time.Duration, error)
- type L2CacheMarshal
- type L2CacheOption
- type L2CacheUnmarshal
- type RingCacheParams
- type SlowCache
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidType = errors.New("invalid type")
ErrInvalidType is the error of invalid type
var ErrIsNil = errors.New("cache is nil")
ErrIsNil is the error of nil cache
var ErrKeyIsNil = errors.New("key is nil")
ErrKeyIsNil is the error of nil key
Functions ¶
func BufferMarshal ¶ added in v0.3.2
BufferMarshal converts *bytes.Buffer to bytes, it returns a ErrInvalidType if restult is not *bytes.Buffer
func BufferUnmarshal ¶ added in v0.3.2
BufferUnmarshal writes the data to buffer, it returns a ErrInvalidType if restult is not *bytes.Buffer
func CPUTicks ¶ added in v0.5.0
func CPUTicks() int64
CPUTicks is a faster alternative to NanoTime to measure time duration.
func FastRand ¶ added in v0.5.0
func FastRand() uint32
FastRand is a fast thread local random function.
func MemHash ¶ added in v0.5.0
MemHash is the hash function used by go map, it utilizes available hardware instructions(behaves as aeshash if aes instruction is available). NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
func MemHashString ¶ added in v0.5.0
MemHashString is the hash function used by go map, it utilizes available hardware instructions (behaves as aeshash if aes instruction is available). NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
func NanoTime ¶ added in v0.5.0
func NanoTime() int64
NanoTime returns the current time in nanoseconds from a monotonic clock.
func NewRing ¶ added in v0.5.0
func NewRing(params RingCacheParams, opts ...CacheOption) *ringCache
NewRing returns a new ring cache
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
func New ¶
func New(maxEntries int, defaultTTL time.Duration, opts ...CacheOption) *Cache
New returns a new lru cache with ttl
func (*Cache) Get ¶
Get returns value and exists from the cache by key, if value is expired then remove it. If the value is expired, value is not nil but exists is false.
func (*Cache) Peek ¶ added in v0.4.0
Peek get a key's value from the cache, but not move to front. The performance is better than get. It will not be removed if the cache is expired.
func (*Cache) PeekBytes ¶ added in v1.4.0
PeekBytes is the same as Peek function, but returns []byte
type CacheOption ¶ added in v0.4.1
type CacheOption func(c *Cache)
CacheOption cache option
func CacheEvictedOption ¶ added in v0.4.1
func CacheEvictedOption(fn func(key Key, value interface{})) CacheOption
CacheEvictedOption sets evicted function to cache
type L2Cache ¶ added in v0.3.0
type L2Cache struct {
// contains filtered or unexported fields
}
A l2cache for frequently visited data
func NewL2Cache ¶ added in v0.3.0
func NewL2Cache(slowCache SlowCache, maxEntries int, defaultTTL time.Duration, opts ...L2CacheOption) *L2Cache
NewL2Cache return a new L2Cache, it returns panic if maxEntries or defaultTTL is nil
func (*L2Cache) Get ¶ added in v0.3.0
Get gets data from lru cache first, if not exists, then gets the data from slow cache. Use unmarshal function coverts the data to result
func (*L2Cache) GetBytes ¶ added in v1.3.0
GetBytes gets data from lur cache first, if not exists, then gets the data from slow cache.
func (*L2Cache) GetIgnoreNilErr ¶ added in v1.5.0
Get gets data from lru cache first, if not exists, then gets the data from slow cache. Use unmarshal function coverts the data to result. It will not return nil error.
func (*L2Cache) Set ¶ added in v0.3.0
func (l2 *L2Cache) Set(ctx context.Context, key string, value interface{}, ttl ...time.Duration) error
Set converts the value to bytes, then sets it to lru cache and slow cache
type L2CacheMarshal ¶ added in v1.1.0
type L2CacheOption ¶ added in v0.4.1
type L2CacheOption func(c *L2Cache)
L2CacheOption l2cache option
func L2CacheMarshalOption ¶ added in v0.4.1
func L2CacheMarshalOption(fn L2CacheMarshal) L2CacheOption
L2CacheMarshalOption sets custom marshal function for l2cache
func L2CacheNilErrOption ¶ added in v1.5.0
func L2CacheNilErrOption(nilErr error) L2CacheOption
L2CacheNilErrOption set nil error for l2cache
func L2CachePrefixOption ¶ added in v0.4.1
func L2CachePrefixOption(prefix string) L2CacheOption
L2CachePrefixOption sets prefix for l2cache
func L2CacheUnmarshalOption ¶ added in v0.4.1
func L2CacheUnmarshalOption(fn L2CacheUnmarshal) L2CacheOption
L2CacheUnmarshalOption sets custom unmarshal function for l2cache