mcache

package
v0.0.0-...-81f576f Latest Latest
Warning

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

Go to latest
Published: May 4, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package mcache provides a global memory cache which can be used to store necessary data in local memory. It uses freecache as underlying engine. For more details about freecache, please refer to https://github.com/coocood/freecache

Index

Constants

View Source
const (
	// SizeKB means 1 kilobyte.
	SizeKB = 1024
	// SizeMB means 1 megabyte.
	SizeMB = 1024 * SizeKB
	// SizeGB means 1 gigabyte.
	SizeGB = 1024 * SizeMB
)

Variables

This section is empty.

Functions

func AverageAccessTime

func AverageAccessTime() int64

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 Clear

func Clear()

Clear clears the cache.

func Del

func Del(key string) (affected bool)

Del deletes an item in the cache by key and returns true or false if a delete occurred.

func EntryCount

func EntryCount() (entryCount int64)

EntryCount returns the number of items currently in the cache.

func EvacuateCount

func EvacuateCount() (count int64)

EvacuateCount is a metric indicating the number of times an eviction occurred.

func ExpiredCount

func ExpiredCount() (count int64)

ExpiredCount is a metric indicating the number of times an expire occurred.

func Get

func Get(key string) (value []byte, err error)

Get returns the value or not found error.

func GetInt

func GetInt(key string) (int, error)

GetInt returns the value for an integer within the cache or a not found error.

func GetIntWithExpiration

func GetIntWithExpiration(key string) (value int, expireAt uint32, err error)

GetIntWithExpiration returns the value and expiration or a not found error.

func GetString

func GetString(key string) (string, error)

GetString will get the cached value and try to convert it to string.

func GetWithBuf

func GetWithBuf(key string, buf []byte) (value []byte, err error)

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 GetWithExpiration

func GetWithExpiration(key string) (value []byte, expireAt uint32, err error)

GetWithExpiration returns the value with expiration or not found error.

func HitCount

func HitCount() (count int64)

HitCount is a metric that returns number of times a key was found in the cache.

func HitRate

func HitRate() float64

HitRate is the ratio of hits over lookups.

func Init

func Init(memSize int, setGCPercent bool, gcPercent int)

Init is the first function that needs to be invoked when this package is used. It will initialized freecache according to the given parameters. memSize: the initial size of the cache in Byte. The memory will be preallocated. Example: 100 * SizeMB setGCPercent: whether to set GC percentage. gcPercent: the GC percentage to set. Memory is preallocated.If you allocate large amount of memory, you may need to set debug.SetGCPercent() to a much lower percentage to get a normal GC frequency.

func IsUsable

func IsUsable() bool

IsUsable can be used to check if c has been initialized.

func LookupCount

func LookupCount() int64

LookupCount is a metric that returns the number of times a lookup for a given key occurred.

func MissCount

func MissCount() (count int64)

MissCount is a metric that returns the number of times a miss occurred in the cache.

func OverwriteCount

func OverwriteCount() (overwriteCount int64)

OverwriteCount indicates the number of times entries have been overriden.

func ResetStatistics

func ResetStatistics()

ResetStatistics refreshes the current state of the statistics.

func Set

func Set(key string, value []byte, expireSeconds int) (err error)

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 SetInt

func SetInt(key string, value int, expireSeconds int) error

SetInt stores in integer value in the cahe.

func SetString

func SetString(key, value string, expireSeconds int) error

SetString stores in string value in the cache.

func TTL

func TTL(key string) (timeLeft uint32, err error)

TTL returns the TTL time left for a given key or a not found error.

Types

This section is empty.

Jump to

Keyboard shortcuts

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