Documentation ¶
Index ¶
- Constants
- Variables
- type Cache
- func (cache *Cache) AverageAccessTime() int64
- func (cache *Cache) Clear()
- func (cache *Cache) Del(key []byte) (affected bool)
- func (cache *Cache) DelInt(key int64) (affected bool)
- func (cache *Cache) EntryCount() (entryCount int64)
- func (cache *Cache) EvacuateCount() (count int64)
- func (cache *Cache) ExpiredCount() (count int64)
- func (cache *Cache) Get(key []byte) (value []byte, err error)
- func (cache *Cache) GetFn(key []byte, fn func([]byte) error) (err error)
- func (cache *Cache) GetInt(key int64) (value []byte, err error)
- func (cache *Cache) GetIntWithExpiration(key int64) (value []byte, expireAt uint32, err error)
- func (cache *Cache) GetOrSet(key, value []byte, expireSeconds int) (retValue []byte, err error)
- func (cache *Cache) GetWithBuf(key, buf []byte) (value []byte, err error)
- func (cache *Cache) GetWithExpiration(key []byte) (value []byte, expireAt uint32, err error)
- func (cache *Cache) HitCount() (count int64)
- func (cache *Cache) HitRate() float64
- func (cache *Cache) LookupCount() int64
- func (cache *Cache) MissCount() (count int64)
- func (cache *Cache) NewIterator() *Iterator
- func (cache *Cache) OverwriteCount() (overwriteCount int64)
- func (cache *Cache) Peek(key []byte) (value []byte, err error)
- func (cache *Cache) PeekFn(key []byte, fn func([]byte) error) (err error)
- func (cache *Cache) ResetStatistics()
- func (cache *Cache) Set(key, value []byte, expireSeconds int) (err error)
- func (cache *Cache) SetAndGet(key, value []byte, expireSeconds int) (retValue []byte, found bool, err error)
- func (cache *Cache) SetInt(key int64, value []byte, expireSeconds int) (err error)
- func (cache *Cache) TTL(key []byte) (timeLeft uint32, err error)
- func (cache *Cache) Touch(key []byte, expireSeconds int) (err error)
- func (cache *Cache) TouchedCount() (touchedCount int64)
- type Entry
- type Iterator
- type RingBuf
- func (rb *RingBuf) Begin() int64
- func (rb *RingBuf) Dump() []byte
- func (rb *RingBuf) End() int64
- func (rb *RingBuf) EqualAt(p []byte, off int64) bool
- func (rb *RingBuf) Evacuate(off int64, length int) (newOff int64)
- func (rb *RingBuf) ReadAt(p []byte, off int64) (n int, err error)
- func (rb *RingBuf) Reset(begin int64)
- func (rb *RingBuf) Resize(newSize int)
- func (rb *RingBuf) Size() int64
- func (rb *RingBuf) Skip(length int64)
- func (rb *RingBuf) Slice(off, length int64) ([]byte, error)
- func (rb *RingBuf) String() string
- func (rb *RingBuf) Write(p []byte) (n int, err error)
- func (rb *RingBuf) WriteAt(p []byte, off int64) (n int, err error)
- type StoppableTimer
- type Timer
Constants ¶
const ENTRY_HDR_SIZE = 24
const HASH_ENTRY_SIZE = 16
Variables ¶
var ErrLargeEntry = errors.New("The entry size is larger than 1/1024 of cache size")
var ErrLargeKey = errors.New("The key is larger than 65535")
var ErrNotFound = errors.New("Entry not found")
var ErrOutOfRange = errors.New("out of range")
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a freecache instance.
func NewCache ¶
NewCache returns a newly initialize cache by size. The cache size will be set to 512KB at minimum. If the size is set relatively large, you should call `debug.SetGCPercent()`, set it to a much smaller value to limit the memory consumption and GC pause time.
func NewCacheCustomTimer ¶ added in v1.1.1
NewCacheCustomTimer returns new cache with custom timer.
func (*Cache) AverageAccessTime ¶
AverageAccessTime returns the average unix timestamp when a entry being accessed. Entries have greater access time will be evacuated when it is about to be overwritten by new value.
func (*Cache) Del ¶
Del deletes an item in the cache by key and returns true or false if a delete occurred.
func (*Cache) DelInt ¶
DelInt deletes an item in the cache by int key and returns true or false if a delete occurred.
func (*Cache) EntryCount ¶
EntryCount returns the number of items currently in the cache.
func (*Cache) EvacuateCount ¶
EvacuateCount is a metric indicating the number of times an eviction occurred.
func (*Cache) ExpiredCount ¶
ExpiredCount is a metric indicating the number of times an expire occurred.
func (*Cache) GetFn ¶ added in v1.2.0
GetFn is equivalent to Get or GetWithBuf, but it attempts to be zero-copy, calling the provided function with slice view over the current underlying value of the key in memory. The slice is constrained in length and capacity.
In moth cases, this method will not alloc a byte buffer. The only exception is when the value wraps around the underlying segment ring buffer.
The method will return ErrNotFound is there's a miss, and the function will not be called. Errors returned by the function will be propagated.
func (*Cache) GetInt ¶
GetInt returns the value for an integer within the cache or a not found error.
func (*Cache) GetIntWithExpiration ¶
GetIntWithExpiration returns the value and expiration or a not found error.
func (*Cache) GetOrSet ¶ added in v1.1.1
GetOrSet returns existing value or if record doesn't exist it sets a new key, value and expiration for a cache entry and stores it in the cache, returns nil in that case
func (*Cache) GetWithBuf ¶ added in v1.1.0
GetWithBuf copies the value to the buf or returns not found error. This method doesn't allocate memory when the capacity of buf is greater or equal to value.
func (*Cache) GetWithExpiration ¶
GetWithExpiration returns the value with expiration or not found error.
func (*Cache) HitCount ¶
HitCount is a metric that returns number of times a key was found in the cache.
func (*Cache) LookupCount ¶
LookupCount is a metric that returns the number of times a lookup for a given key occurred.
func (*Cache) MissCount ¶
MissCount is a metric that returns the number of times a miss occurred in the cache.
func (*Cache) NewIterator ¶
NewIterator creates a new iterator for the cache.
func (*Cache) OverwriteCount ¶
OverwriteCount indicates the number of times entries have been overriden.
func (*Cache) Peek ¶ added in v1.1.1
Peek returns the value or not found error, without updating access time or counters.
func (*Cache) PeekFn ¶ added in v1.2.0
PeekFn is equivalent to Peek, but it attempts to be zero-copy, calling the provided function with slice view over the current underlying value of the key in memory. The slice is constrained in length and capacity.
In moth cases, this method will not alloc a byte buffer. The only exception is when the value wraps around the underlying segment ring buffer.
The method will return ErrNotFound is there's a miss, and the function will not be called. Errors returned by the function will be propagated.
func (*Cache) ResetStatistics ¶
func (cache *Cache) ResetStatistics()
ResetStatistics refreshes the current state of the statistics.
func (*Cache) Set ¶
Set sets a key, value and expiration for a cache entry and stores it in the cache. If the key is larger than 65535 or value is larger than 1/1024 of the cache size, the entry will not be written to the cache. expireSeconds <= 0 means no expire, but it can be evicted when cache is full.
func (*Cache) SetAndGet ¶ added in v1.2.0
func (cache *Cache) SetAndGet(key, value []byte, expireSeconds int) (retValue []byte, found bool, err error)
SetAndGet sets a key, value and expiration for a cache entry and stores it in the cache. If the key is larger than 65535 or value is larger than 1/1024 of the cache size, the entry will not be written to the cache. expireSeconds <= 0 means no expire, but it can be evicted when cache is full. Returns existing value if record exists with a bool value to indicate whether an existing record was found
func (*Cache) Touch ¶ added in v1.1.1
Touch updates the expiration time of an existing key. expireSeconds <= 0 means no expire, but it can be evicted when cache is full.
func (*Cache) TouchedCount ¶ added in v1.1.1
TouchedCount indicates the number of times entries have had their expiration time extended.
type Iterator ¶
type Iterator struct {
// contains filtered or unexported fields
}
Iterator iterates the entries for the cache.
type RingBuf ¶
type RingBuf struct {
// contains filtered or unexported fields
}
Ring buffer has a fixed size, when data exceeds the size, old data will be overwritten by new data. It only contains the data in the stream from begin to end
func NewRingBuf ¶
func (*RingBuf) Evacuate ¶
Evacuate read the data at off, then write it to the the data stream, Keep it from being overwritten by new data.
func (*RingBuf) Reset ¶ added in v1.2.0
Reset the ring buffer
Parameters:
begin: beginning offset of the data stream
type StoppableTimer ¶ added in v1.2.0
type StoppableTimer interface { Timer // Release resources of the timer, functionality may or may not be affected // It is not called automatically, so user must call it just once Stop() }
Timer that must be stopped.
func NewCachedTimer ¶ added in v1.2.0
func NewCachedTimer() StoppableTimer
Create cached timer and start runtime timer that updates time every second