cache

package module
v0.0.0-...-52b973c Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2017 License: Apache-2.0 Imports: 14 Imported by: 0

README

GO-cache

A nice and fast set of caches by LAZADA! All cache types are thread safe and supports prometheus metrics.

Have 3 cache interfaces:

  1. IAutoCache
  2. IByteCache
  3. IStructCache

IAutoCache:

Implementations:

  • StorageAutoCacheFake
  • StorageAutoCache
StorageAutoCacheFake:

Can be used in tests

StorageAutoCache:

Executes callback when record`s ttl expired.

Example:
package main

import (
	"fmt"
	"math/rand"
	"time"
	
	"go-cache"
)

func init() {
	rand.Seed(42)
}

func getValue() (interface{}, error) {
	return rand.Intn(100), nil
}

func main() {
	autoCache := cache.NewStorageAutoCacheObject(true, nil)

	const key = "unique-key"

	err := autoCache.Put(getValue, key, 100*time.Second)
	if err != nil {
		panic(err)
	}

	value, err := autoCache.Get(key)
	if err != nil {
		panic(err)
	}

	fmt.Print(value)
	// Output:
	// 5
}

IByteCache:

Implementations:

  • BlackholeCache
  • AerospikeCache
BlackholeCache:

Can be used in tests

AerospikeCache:

Use aerospike to store slices of bytes. Can be limited. Can store data into different sets. Also supports tags.

Example:
package main

import (
	"fmt"
	"time"

	"go-cache"
	"go-cache/metric/dummy"
)

func main() {
	config := &cache.AerospikeConfig{
		NameSpace:  "test",
		Hosts:      []string{"localhost:3000"},
		MaxRetries: 5,
	}
	logger := &cache.AerospikeDummyLogger{}

	client, err := cache.CreateAerospikeClient(config, logger)
	if err != nil {
		panic(err)
	}

	aerospikeCache := cache.NewAerospikeCache(config, client, logger, dummy.NewMetric())

	const wirth = "But quality of work can be expected only through personal satisfaction, dedication and enjoyment. In our profession, precision and perfection are not a dispensible luxury, but a simple necessity."
	data := []byte(wirth)
	key := &cache.Key{
		Set: "testset",
		Pk:  "testExpired",
	}

	aerospikeCache.Put(data, key, time.Second)

	cachedData, ok := aerospikeCache.Get(key)
	if !ok {
		panic("Something went wrong")
	}

	fmt.Println(string(cachedData))
	// Output:
	// But quality of work can be expected only through personal satisfaction, dedication and enjoyment. In our profession, precision and perfection are not a dispensible luxury, but a simple necessity.
}

StructCache:

Can store type into local cache. Fast and tread safe.

Supports:

  • Limit
  • LRU
  • Cache sets
Example:
package main

import (
	"fmt"
	"time"

	"go-cache"
	"go-cache/metric/dummy"
)

func main() {
	structCache := cache.NewStructCacheObject(8000, nil, dummy.NewMetric())

	k := &cache.Key{
		Set: "set",
		Pk:  "1",
	}
	data := "The essential is invisible to the eyes, we can not truly see but with the eyes of the heart."

	ttl := 5*time.Minute

	structCache.Put(data, k, ttl)

	infResult, find := structCache.Get(k)
	if !find {
		panic("Key is not found")
	}

	result, ok := infResult.(string)
	if !ok {
		panic("Data have wrong type")
	}

	fmt.Println(string(result))
	// Output:
	// The essential is invisible to the eyes, we can not truly see but with the eyes of the heart.
}

Documentation

Index

Examples

Constants

View Source
const (
	// SmallCacheTTL define a short cache lifetime
	SmallCacheTTL = 5 * time.Minute

	// DefaultCacheTTL default cache lifetime
	DefaultCacheTTL = 30 * time.Minute

	// TwoHCacheTTL two hours cache lifetime
	TwoHCacheTTL = 2 * time.Hour

	// LongCacheTTL large cache lifetime (one day)
	LongCacheTTL = 24 * time.Hour

	// VeryLongCacheTTL very large cache lifetime (one mounth)
	VeryLongCacheTTL = 30 * 24 * time.Hour

	// EternalLongCacheTTL eternal large cache lifetime (one 10 years)
	EternalLongCacheTTL = 10 * 365 * 24 * time.Hour
)
View Source
const (
	RetryTimeout = time.Second * 10
)

Variables

View Source
var ErrSetAlreadyExists = errors.New("Set already exists")

Functions

func CreateAerospikeClient

func CreateAerospikeClient(config *AerospikeConfig, logger IAerospikeCacheLogger) (*aerospike.Client, error)

CreateAerospikeClient creates AerospikeClient for AerospikeCache

Types

type AerospikeCache

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

AerospikeCache implements cache that uses Aerospike as storage

func NewAerospikeCache

func NewAerospikeCache(config *AerospikeConfig, client *aerospike.Client, logger IAerospikeCacheLogger, metric metric.Metric) *AerospikeCache

NewAerospikeCache initializes instance of Aerospike-based cache

func (*AerospikeCache) ClearSet

func (a *AerospikeCache) ClearSet(set string) error

ClearSet removes all values in set

func (*AerospikeCache) Client

func (a *AerospikeCache) Client() *aerospike.Client

Client returns aerospike client.

func (*AerospikeCache) Close

func (a *AerospikeCache) Close()

Close cache storage Aerospike

func (*AerospikeCache) Count

func (a *AerospikeCache) Count() int

FIXME: implement me Count returns count of data in cache

func (*AerospikeCache) CreateTagsIndex

func (a *AerospikeCache) CreateTagsIndex(aerospikeIndex AerospikeIndex) error

CreateTagsIndex creates tags index (indexName) by setName

func (*AerospikeCache) Flush

func (a *AerospikeCache) Flush() int

FIXME: This function should be implemented Flush cleans all data.

func (*AerospikeCache) Get

func (a *AerospikeCache) Get(key *Key) ([]byte, bool)

Get returns data by given key

Example
config := &AerospikeConfig{
	NameSpace:  "test",
	Hosts:      []string{"localhost:3000"},
	MaxRetries: 5,
}
logger := &AerospikeDummyLogger{}

client, err := CreateAerospikeClient(config, logger)
if err != nil {
	panic(err)
}

aerospikeCache := NewAerospikeCache(config, client, logger, dummy.NewMetric())

const wirth = "But quality of work can be expected only through personal satisfaction, dedication and enjoyment. In our profession, precision and perfection are not a dispensible luxury, but a simple necessity."
data := []byte(wirth)
key := &Key{
	Set: "testset",
	Pk:  "testExpired",
}

aerospikeCache.Put(data, key, time.Second)

cachedData, ok := aerospikeCache.Get(key)
if !ok {
	panic("Something went wrong")
}

fmt.Println(string(cachedData))
Output:

But quality of work can be expected only through personal satisfaction, dedication and enjoyment. In our profession, precision and perfection are not a dispensible luxury, but a simple necessity.

func (*AerospikeCache) Put

func (a *AerospikeCache) Put(data []byte, key *Key, ttl time.Duration)

Put will delayed put cache in Aerospike

func (*AerospikeCache) Remove

func (a *AerospikeCache) Remove(key *Key) (err error)

Remove removes data by given cache key If tags provided, all records having at least one of them will be removed Otherwise only an item with given Primary key will be removed

func (*AerospikeCache) ScanKeys

func (a *AerospikeCache) ScanKeys(set string) ([]Key, error)

ScanKeys return all keys for set

func (*AerospikeCache) SetCachePrefix

func (a *AerospikeCache) SetCachePrefix(prefix string)

SetCachePrefix defines prefix for user key

type AerospikeConfig

type AerospikeConfig struct {
	Prefix    string   `config:"aerospike_prefix" default:"" description:"aerospike prefix"`
	NameSpace string   `config:"aerospike_namespace" description:"aerospike namespace"`
	Hosts     []string `config:"aerospike_hosts" description:"aerospike comma-separated host:port list"`

	MaxRetries          int           `config:"aerospike_max_retries" default:"3" description:"aerospike max retries for get and put operations"`
	SleepBetweenRetries time.Duration `config:"aerospike_sleep_between_retries" default:"500ms" description:"aerospike sleep between connection/read/write retries"`

	// tcp connection to aerospike server timeout
	ConnectionTimeout time.Duration `config:"aerospike_connection_timeout" default:"1s" description:"aerospike connection timeout"`

	// max unused connection lifetime
	IdleTimeout time.Duration `config:"aerospike_idle_timeout" default:"1m" description:"aerospike idle connection lifetime"`

	ReadTimeout   time.Duration `config:"aerospike_read_timeout" default:"100ms" description:"aerospike read timeout"`
	RemoveTimeout time.Duration `config:"aerospike_remove_timeout" default:"800ms" description:"aerospike remove timeout"`
	PutTimeout    time.Duration `config:"aerospike_put_timeout" default:"500ms" description:"aerospike put timeout"`

	// max connection pool (queue) size
	ConnectionQueueSize int `config:"aerospike_connection_queue_size" default:"256" description:"aerospike connection queue size"`

	// if true - wait for used connection to be released (up to 1ms)
	LimitConnectionsToQueueSize bool `config:"aerospike_limit_connections" default:"false" description:"aerospike limit connections count to queue size"`

	LogLevel int `` /* 128-byte string literal not displayed */

	FailIfNotConnected bool `config:"aerospike_fail_if_not_connected" default:"false" description:"aerospike fail if not connected"`

	// connection count metric update time interval
	UpdateConnectionCountMetricInterval time.Duration `` /* 135-byte string literal not displayed */
}

AerospikeConfig contains configuration for aerospike

type AerospikeIndex

type AerospikeIndex struct {
	SetName   string
	IndexName string
	IndexType aerospike.IndexType
}

AerospikeIndex contains info about aerospike index

type BlackholeCache

type BlackholeCache struct {
}

BlackholeCache implements cache that accept any data but always return null. Used for unit tests

func NewBlackholeCache

func NewBlackholeCache() *BlackholeCache

NewBlackholeCache initializes instance of BlackholeCache

func (*BlackholeCache) ClearSet

func (cache *BlackholeCache) ClearSet(set string) error

ClearSet returns nil, does nothing

func (*BlackholeCache) Close

func (cache *BlackholeCache) Close()

Close do nothing

func (*BlackholeCache) Count

func (cache *BlackholeCache) Count() int

Count returns count of data in cache FIXME: implement me

func (*BlackholeCache) Flush

func (cache *BlackholeCache) Flush() int

Flush removes all entries from cache and returns number of flushed entries

func (*BlackholeCache) Get

func (cache *BlackholeCache) Get(key *Key) (data []byte, ok bool)

Get returns nil, do nothing

func (*BlackholeCache) Put

func (cache *BlackholeCache) Put(data []byte, key *Key, ttl time.Duration)

Put returns nil, do nothing

func (*BlackholeCache) Remove

func (cache *BlackholeCache) Remove(key *Key) (err error)

Remove returns nil, do nothing

func (*BlackholeCache) ScanKeys

func (cache *BlackholeCache) ScanKeys(set string) ([]Key, error)

ScanKeys returns nil, do nothing

type Entry

type Entry struct {
	Key        *Key
	CreateDate time.Time // In UTC
	EndDate    int64
	Data       interface{}
}

Entry contains data

func CreateEntry

func CreateEntry(key *Key, endDate int64, data interface{}) *Entry

CreateEntry returns new instance of Entry

func (*Entry) IsValid

func (entry *Entry) IsValid() bool

IsValid returns Entry is valid

type EntryAutoCache

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

EntryAutoCache contains data

func CreateEntryAutoCache

func CreateEntryAutoCache(updater func() (interface{}, error), interval time.Duration, name string, logger IAutoCacheLogger) *EntryAutoCache

CreateEntryAutoCache returns new instance of EntryAutoCache

func (*EntryAutoCache) GetValue

func (entry *EntryAutoCache) GetValue() (interface{}, error)

GetValue returns the result of processing the updater

func (*EntryAutoCache) Start

func (entry *EntryAutoCache) Start() error

Start starts updater process in goroutine

func (*EntryAutoCache) Stop

func (entry *EntryAutoCache) Stop()

Stop stops updater process

type IAerospikeCacheLogger

type IAerospikeCacheLogger interface {
	Printf(format string, v ...interface{})
	Debugf(message string, args ...interface{})
	Errorf(message string, args ...interface{})
	Warningf(message string, args ...interface{})
	Warning(...interface{})
	Criticalf(message string, args ...interface{})
	Critical(...interface{})
}

type IAutoCache

type IAutoCache interface {
	Get(key string) (data interface{}, err error)
	Put(updater func() (interface{}, error), key string, ttl time.Duration) error
	Remove(key string)
}

IAutoCache defines required interface for caching module (need for auto cache)

type IAutoCacheLogger

type IAutoCacheLogger interface {
	Errorf(message string, args ...interface{})
	Criticalf(message string, args ...interface{})
}

type IByteCache

type IByteCache interface {
	IFlushable
	Count() int
	Get(key *Key) (data []byte, ok bool)
	Put(data []byte, key *Key, ttl time.Duration)
	Remove(key *Key) error
	Close()
	ClearSet(set string) error
	ScanKeys(set string) ([]Key, error)
}

IByteCache defines required interface for caching module

func NewEntryCacheWrapper

func NewEntryCacheWrapper(fn fnCreate, logger IAerospikeCacheLogger) IByteCache

NewEntryCacheWrapper initializes instance of IEntryCache

type IFlushable

type IFlushable interface {
	Flush() int
}

IFlushable determines cache that can be flushed

type ILimitSetter

type ILimitSetter interface {
	SetLimit(int)
}

type IMemoryCacheLogger

type IMemoryCacheLogger interface {
	Errorf(message string, args ...interface{})
	Warning(message ...interface{})
}

type IStructCache

type IStructCache interface {
	IFlushable
	RegisterCacheSet(setName string, limit int, ticker *time.Ticker) error
	Get(key *Key) (data interface{}, ok bool)
	GetWithTime(key *Key) (data interface{}, dt time.Time, ok bool)
	Put(data interface{}, key *Key, ttl time.Duration) error
	Remove(key *Key)
	Count() int
	Close()
}

IStructCache defines required interface for caching module (to be able to store any kind of data, mostly in memory)

type IStructCacheDebug

type IStructCacheDebug interface {
	IFlushable
	Count() int
	Find(maskedKey string, limit int) []string
}

IStructCacheDebug defines interface for console debug tools

type IStructCacheLogger

type IStructCacheLogger interface {
	IsDebugEnabled() bool
	Debugf(message string, args ...interface{})
	Debug(...interface{})
	Warningf(message string, args ...interface{})
	Warning(...interface{})
}

type Key

type Key struct {
	Set  string
	Pk   string
	Tags []string
}

Key defines cache key for some record

func (Key) ID

func (key Key) ID() string

ID returns string that identifies given key

func (Key) String

func (key Key) String() string

String implements fmt.Stringer interface for readable string representation

type NilLogger

type NilLogger struct {
}

func NewNilLogger

func NewNilLogger() *NilLogger

func (*NilLogger) Critical

func (this *NilLogger) Critical(...interface{})

func (*NilLogger) Criticalf

func (this *NilLogger) Criticalf(message string, args ...interface{})

func (*NilLogger) Debug

func (this *NilLogger) Debug(...interface{})

func (*NilLogger) Debugf

func (this *NilLogger) Debugf(message string, args ...interface{})

func (*NilLogger) Errorf

func (this *NilLogger) Errorf(message string, args ...interface{})

func (*NilLogger) IsDebugEnabled

func (this *NilLogger) IsDebugEnabled() bool

func (*NilLogger) Printf

func (this *NilLogger) Printf(format string, v ...interface{})

func (*NilLogger) Warning

func (this *NilLogger) Warning(...interface{})

func (*NilLogger) Warningf

func (this *NilLogger) Warningf(message string, args ...interface{})

type StorageAutoCache

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

StorageAutoCache implements cache that uses auto cache as storage

func NewStorageAutoCacheObject

func NewStorageAutoCacheObject(active bool, logger IAutoCacheLogger) *StorageAutoCache

NewStorageAutoCacheObject create new instance of StorageAutoCache

func (*StorageAutoCache) Get

func (storage *StorageAutoCache) Get(key string) (interface{}, error)

Get returns data by given key

Example
autoCache := NewStorageAutoCacheObject(true, nil)

const key = "unique-key"
getValue := func() (interface{}, error) {
	rand.Seed(42)
	return rand.Intn(100), nil
}

err := autoCache.Put(getValue, key, 100*time.Second)
if err != nil {
	panic(err)
}

value, err := autoCache.Get(key)
if err != nil {
	panic(err)
}

fmt.Print(value)
Output:

5

func (*StorageAutoCache) Put

func (storage *StorageAutoCache) Put(updater func() (interface{}, error), key string, ttl time.Duration) error

Put puts data into storage

func (*StorageAutoCache) Remove

func (storage *StorageAutoCache) Remove(key string)

Remove removes value by key

Example
autoCache := NewStorageAutoCacheObject(true, nil)

const key = "unique-key"
getValue := func() (interface{}, error) {
	rand.Seed(42)
	return rand.Intn(100), nil
}

err := autoCache.Put(getValue, key, 100*time.Second)
if err != nil {
	panic(err)
}

autoCache.Remove(key)

_, err = autoCache.Get(key)

fmt.Print(err)
Output:

Auto cache key unique-key nof found

type StorageAutoCacheFake

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

StorageAutoCacheFake implements cache that uses auto cache as storage This type of autocache have no TTL

func NewStorageAutoCacheFake

func NewStorageAutoCacheFake() *StorageAutoCacheFake

NewStorageAutoCache create new instance of StorageAutoCache

func (*StorageAutoCacheFake) Get

func (storage *StorageAutoCacheFake) Get(key string) (interface{}, error)

Get returns data by given key

func (*StorageAutoCacheFake) Put

func (storage *StorageAutoCacheFake) Put(updater func() (interface{}, error), key string, ttl time.Duration) error

Put puts data into storage

func (*StorageAutoCacheFake) Remove

func (storage *StorageAutoCacheFake) Remove(key string)

Remove removes value by key

type StructCache

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

StructCache is simple storage with locking

func NewStructCacheObject

func NewStructCacheObject(limit int, logger IStructCacheLogger, metric metric.Metric) *StructCache

NewStructCacheObject returns new instance of StructCache

func (*StructCache) Close

func (cache *StructCache) Close()

func (*StructCache) Count

func (cache *StructCache) Count() int

Count elements from cache

Example
structCache := NewStructCacheObject(8000, nil, dummy.NewMetric())

k := &Key{
	Set: "set1",
	Pk:  "1",
}
data := "data"

ttl := 5 * time.Minute

structCache.Put(data, k, ttl)

k = &Key{
	Set: "set1",
	Pk:  "2",
}
structCache.Put(data, k, ttl)

k = &Key{
	Set: "set2",
	Pk:  "1",
}
structCache.Put(data, k, ttl)

count := structCache.Count()

fmt.Println(count)
Output:

3

func (*StructCache) Find

func (cache *StructCache) Find(maskedKey string, limit int) []string

Find search key by mask

Example
structCache := NewStructCacheObject(8000, nil, dummy.NewMetric())

k := &Key{
	Set: "set1",
	Pk:  "mask",
}
data := "data"

ttl := 5 * time.Minute

structCache.Put(data, k, ttl)

cnt := structCache.Find("as", 1)

fmt.Println(cnt)
Output:

[mask]

func (*StructCache) Flush

func (cache *StructCache) Flush() int

Flush removes all entries from cache and returns number of flushed entries

Example
structCache := NewStructCacheObject(8000, nil, dummy.NewMetric())

k := &Key{
	Set: "set1",
	Pk:  "1",
}
data := "data"

ttl := 5 * time.Minute

structCache.Put(data, k, ttl)

k = &Key{
	Set: "set2",
	Pk:  "1",
}
structCache.Put(data, k, ttl)

structCache.Flush()

count := structCache.Count()

fmt.Println(count)
Output:

0

func (*StructCache) Get

func (cache *StructCache) Get(key *Key) (interface{}, bool)

Get returns value by key

Example
structCache := NewStructCacheObject(8000, nil, dummy.NewMetric())

k := &Key{
	Set: "set",
	Pk:  "1",
}
data := "The essential is invisible to the eyes, we can not truly see but with the eyes of the heart."

ttl := 5 * time.Minute

structCache.Put(data, k, ttl)

infResult, find := structCache.Get(k)
if !find {
	panic("Key is not found")
}

result, ok := infResult.(string)
if !ok {
	panic("Data have wrong type")
}

fmt.Println(string(result))
Output:

The essential is invisible to the eyes, we can not truly see but with the eyes of the heart.

func (*StructCache) GetWithTime

func (cache *StructCache) GetWithTime(key *Key) (interface{}, time.Time, bool)

GetWithTime returns value and create time(UTC) by key

func (*StructCache) Put

func (cache *StructCache) Put(data interface{}, key *Key, ttl time.Duration) error

Put puts elements into storage

Example
structCache := NewStructCacheObject(8000, nil, dummy.NewMetric())

k := &Key{
	Set: "set1",
	Pk:  "1",
}
data := "data"

ttl := 5 * time.Minute

structCache.Put(data, k, ttl)

data2 := "Your most unhappy customers are your greatest source of learning."
structCache.Put(data2, k, ttl)

infResult, find := structCache.Get(k)
if !find {
	panic("Data was not found")
}

result, ok := infResult.(string)
if !ok {
	panic("Data have wrong type")
}

fmt.Println(result)
Output:

Your most unhappy customers are your greatest source of learning.

func (*StructCache) RegisterCacheSet

func (cache *StructCache) RegisterCacheSet(setName string, limit int, ticker *time.Ticker) error

func (*StructCache) Remove

func (cache *StructCache) Remove(key *Key)

Remove removes value by key

Example
structCache := NewStructCacheObject(8000, nil, dummy.NewMetric())

k := &Key{
	Set: "set1",
	Pk:  "1",
}
data := "data"

ttl := 5 * time.Minute

structCache.Put(data, k, ttl)

structCache.Remove(k)

count := structCache.Count()

fmt.Println(count)
Output:

0

func (*StructCache) SetLimit

func (cache *StructCache) SetLimit(limit int)
Example
structCache := NewStructCacheObject(1000, nil, dummy.NewMetric())
structCache.SetLimit(1)

k := &Key{
	Set: "set1",
	Pk:  "1",
}
data := "data"

ttl := 5 * time.Minute

structCache.Put(data, k, ttl)

k = &Key{
	Set: "set1",
	Pk:  "2",
}
structCache.Put(data, k, ttl)

cnt := structCache.Count()

fmt.Println(cnt)
Output:

1

type StructCacheDummy

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

StructCacheDummy implements cache that accept any data but always return null

func NewStructCacheObjectDummy

func NewStructCacheObjectDummy(logger IStructCacheLogger) *StructCacheDummy

NewStructCacheObjectDummy returns new instance of StructCacheDummy

func (*StructCacheDummy) Close

func (cache *StructCacheDummy) Close()

Close do nothing

func (*StructCacheDummy) Count

func (cache *StructCacheDummy) Count() int

Count returns number of cache entries

func (*StructCacheDummy) Flush

func (cache *StructCacheDummy) Flush() int

Flush removes all entries from cache and returns number of flushed entries

func (*StructCacheDummy) Get

func (cache *StructCacheDummy) Get(key *Key) (data interface{}, ok bool)

Get returns nil, do nothing

func (*StructCacheDummy) GetWithTime

func (cache *StructCacheDummy) GetWithTime(key *Key) (data interface{}, dt time.Time, ok bool)

GetWithTime returns nil, do nothing

func (*StructCacheDummy) Put

func (cache *StructCacheDummy) Put(data interface{}, key *Key, ttl time.Duration) error

Put returns nil, do nothing

func (*StructCacheDummy) RegisterCacheSet

func (cache *StructCacheDummy) RegisterCacheSet(setName string, limit int, ticker *time.Ticker) error

func (*StructCacheDummy) Remove

func (cache *StructCacheDummy) Remove(key *Key)

Remove returns nil, do nothing

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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