Documentation ¶
Index ¶
- Constants
- Variables
- func CreateAerospikeClient(config *AerospikeConfig, logger IAerospikeCacheLogger) (*aerospike.Client, error)
- type AerospikeCache
- func (a *AerospikeCache) ClearSet(set string) error
- func (a *AerospikeCache) Client() *aerospike.Client
- func (a *AerospikeCache) Close()
- func (a *AerospikeCache) Count() int
- func (a *AerospikeCache) CreateTagsIndex(aerospikeIndex AerospikeIndex) error
- func (a *AerospikeCache) Flush() int
- func (a *AerospikeCache) Get(key *Key) ([]byte, bool)
- func (a *AerospikeCache) Put(data []byte, key *Key, ttl time.Duration)
- func (a *AerospikeCache) Remove(key *Key) (err error)
- func (a *AerospikeCache) ScanKeys(set string) ([]Key, error)
- func (a *AerospikeCache) SetCachePrefix(prefix string)
- type AerospikeConfig
- type AerospikeIndex
- type BlackholeCache
- func (cache *BlackholeCache) ClearSet(set string) error
- func (cache *BlackholeCache) Close()
- func (cache *BlackholeCache) Count() int
- func (cache *BlackholeCache) Flush() int
- func (cache *BlackholeCache) Get(key *Key) (data []byte, ok bool)
- func (cache *BlackholeCache) Put(data []byte, key *Key, ttl time.Duration)
- func (cache *BlackholeCache) Remove(key *Key) (err error)
- func (cache *BlackholeCache) ScanKeys(set string) ([]Key, error)
- type Entry
- type EntryAutoCache
- type IAerospikeCacheLogger
- type IAutoCache
- type IAutoCacheLogger
- type IByteCache
- type IFlushable
- type ILimitSetter
- type IMemoryCacheLogger
- type IStructCache
- type IStructCacheDebug
- type IStructCacheLogger
- type Key
- type NilLogger
- func (this *NilLogger) Critical(...interface{})
- func (this *NilLogger) Criticalf(message string, args ...interface{})
- func (this *NilLogger) Debug(...interface{})
- func (this *NilLogger) Debugf(message string, args ...interface{})
- func (this *NilLogger) Errorf(message string, args ...interface{})
- func (this *NilLogger) IsDebugEnabled() bool
- func (this *NilLogger) Printf(format string, v ...interface{})
- func (this *NilLogger) Warning(...interface{})
- func (this *NilLogger) Warningf(message string, args ...interface{})
- type StorageAutoCache
- type StorageAutoCacheFake
- type StructCache
- func (cache *StructCache) Close()
- func (cache *StructCache) Count() int
- func (cache *StructCache) Find(maskedKey string, limit int) []string
- func (cache *StructCache) Flush() int
- func (cache *StructCache) Get(key *Key) (interface{}, bool)
- func (cache *StructCache) GetWithTime(key *Key) (interface{}, time.Time, bool)
- func (cache *StructCache) Put(data interface{}, key *Key, ttl time.Duration) error
- func (cache *StructCache) RegisterCacheSet(setName string, limit int, ticker *time.Ticker) error
- func (cache *StructCache) Remove(key *Key)
- func (cache *StructCache) SetLimit(limit int)
- type StructCacheDummy
- func (cache *StructCacheDummy) Close()
- func (cache *StructCacheDummy) Count() int
- func (cache *StructCacheDummy) Flush() int
- func (cache *StructCacheDummy) Get(key *Key) (data interface{}, ok bool)
- func (cache *StructCacheDummy) GetWithTime(key *Key) (data interface{}, dt time.Time, ok bool)
- func (cache *StructCacheDummy) Put(data interface{}, key *Key, ttl time.Duration) error
- func (cache *StructCacheDummy) RegisterCacheSet(setName string, limit int, ticker *time.Ticker) error
- func (cache *StructCacheDummy) Remove(key *Key)
Examples ¶
Constants ¶
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 )
const (
RetryTimeout = time.Second * 10
)
Variables ¶
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) 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 ¶
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) 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
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
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 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 NilLogger ¶
type NilLogger struct { }
func NewNilLogger ¶
func NewNilLogger() *NilLogger
func (*NilLogger) IsDebugEnabled ¶
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 (*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) 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 (*StructCacheDummy) Remove ¶
func (cache *StructCacheDummy) Remove(key *Key)
Remove returns nil, do nothing
Source Files ¶
- auto_cache.go
- auto_cache_entry.go
- auto_cache_fake.go
- auto_cache_interface.go
- byte_cache_aerospike.go
- byte_cache_aerospike_config.go
- byte_cache_aerospike_indexes.go
- byte_cache_blackhole.go
- byte_cache_interface.go
- byte_cache_wrapper.go
- entry.go
- interface.go
- key.go
- nil_logger.go
- struct_cache.go
- struct_cache_dummy.go
- struct_cache_helper.go
- struct_cache_interface.go
- ttls.go