Skip to content
This repository has been archived by the owner on May 3, 2023. It is now read-only.

Latest commit

 

History

History
465 lines (278 loc) · 14.3 KB

godoc.md

File metadata and controls

465 lines (278 loc) · 14.3 KB

cache

import "github.com/Bose/go-cache"

generic.go generic_test_store.go inmemory.go pkcs.go redispool.go

var (
    // ErrPaddingSize - represents padding errors
    ErrPaddingSize = errors.New("padding size error")
)
var (
    // PKCS5 represents pkcs5 struct
    PKCS5 = &pkcs5{}
)
var (
    // PKCS7 - difference with pkcs5 only block must be 8
    PKCS7 = &pkcs5{}
)
func InitReadOnlyRedisCache(readOnlyCacheURL string, cachePassword string, defaultExpMinutes int, maxConnections int, selectDatabase int, logger *logrus.Entry) (interface{}, error)

InitReadOnlyRedisCache - used by microservices to init their read-only redis cache. Returns an interface{} so other caches could be swapped in

func InitRedisCache(useSentinel bool, defaultExpSeconds int, redisPassword []byte, timeoutMilliseconds int, selectDatabase int, logger *logrus.Entry) (interface{}, error)

InitRedisCache - used by microservices to init their redis cache. Returns an interface{} so other caches could be swapped in

func NewSentinelPool(sentinelAddrs []string, masterIdentifier []byte, redisPassword []byte, timeoutMilliseconds int, selectDatabase int, logger *logrus.Entry) *redis.Pool

NewSentinelPool - create a new pool for Redis Sentinel

type GenericCache struct {
    Cache     interface{}
    ReadCache *GenericCache

    DefaultExp time.Duration

    KeyPrefix   []byte
    Logger      *logrus.Entry
    EncryptData bool
    // contains filtered or unexported fields
}

GenericCache - represents the cache

- Cache: empty interface to a persistent cache pool (could be writable if cType == Writable)
- ReadCache: empty interface to a persistent read-only cache pool (cType == ReadOnly)
- sharedSecret: used by GetKey() for generating signatures to be used as an entries primary key
- DefaultExp: the default expiry for entries
- cType: Writable or ReadOnly
- cLevel: L1 (level 1) or L2 (level 2)
- KeyPrefix: a prefex added to each key that's generated by GetKey()
- Logger: the logger to use when writing logs
func NewCacheWithMultiPools(writeCachePool interface{}, readCachePool interface{}, cLevel Level, sharedSecret string, expirySeconds int, keyPrefix []byte, encryptData bool) *GenericCache

NewCacheWithMultiPools - creates a new generic cache for microservices using two Pools. One pool for writes and a separate pool for reads

func NewCacheWithPool(cachePool interface{}, cType Type, cLevel Level, sharedSecret string, expirySeconds int, keyPrefix []byte, encryptData bool) *GenericCache

NewCacheWithPool - creates a new generic cache for microservices using a Pool for connecting (this cache should be read/write)

func (*GenericCache) Add

func (c *GenericCache) Add(key string, data interface{}, exp time.Duration) (err error)

Add - adds an entry to the cache

func (*GenericCache) AddExistingEntry

func (c *GenericCache) AddExistingEntry(key string, entry GenericCacheEntry, expiresAt int64) error

AddExistingEntry -

func (*GenericCache) Decrement

func (c *GenericCache) Decrement(key string, n uint64) (newValue uint64, err error)

Decrement - Decrement an entry in the cache

func (*GenericCache) Delete

func (c *GenericCache) Delete(key string) (err error)

Delete - deletes an entry in the cache

func (*GenericCache) Exists

func (c *GenericCache) Exists(key string) (found bool, entry GenericCacheEntry, err error)

Exists - searches the cache for an entry

func (*GenericCache) Flush

func (c *GenericCache) Flush() error

Flush - Flush all the keys in the cache

func (*GenericCache) Get

func (c *GenericCache) Get(key string, value interface{}) error

Get - retrieves and entry from the cache

func (*GenericCache) GetKey

func (c *GenericCache) GetKey(entryData []byte) string

GetKey - return a key for the entryData

func (*GenericCache) Increment

func (c *GenericCache) Increment(key string, n uint64) (newValue uint64, err error)

Increment - Increment an entry in the cache

func (*GenericCache) NewGenericCacheEntry

func (c *GenericCache) NewGenericCacheEntry(data interface{}, exp time.Duration) GenericCacheEntry

NewGenericCacheEntry creates an entry with the data and all the time attribs set

func (*GenericCache) Replace

func (c *GenericCache) Replace(key string, data interface{}, exp time.Duration) (err error)

Replace - Replace an entry in the cache

func (*GenericCache) Set

func (c *GenericCache) Set(key string, data interface{}, exp time.Duration) (err error)

Set - Set a key in the cache (over writting any existing entry)

type GenericCacheEntry struct {
    Data      interface{}
    TimeAdded int64
    ExpiresAt int64
}

GenericCacheEntry - represents a cached entry...

- Data: the entries data represented as an empty interface
- TimeAdded: epoc at the time of addtion
- ExpiresAd: epoc at the time of expiry

func (*GenericCacheEntry) Expired

func (e *GenericCacheEntry) Expired() bool

Expired - is the entry expired?

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

InMemoryStore - an in memory LRU store with expiry

func NewInMemoryStore(maxEntries int, defaultExpiration, cleanupInterval time.Duration) (*InMemoryStore, error)

NewInMemoryStore - create a new in memory cache

func (*InMemoryStore) Add

func (c *InMemoryStore) Add(key string, value interface{}, exp time.Duration) error

Add - add an entry

func (*InMemoryStore) Decrement

func (c *InMemoryStore) Decrement(key string, n uint64) (uint64, error)

Decrement (see CacheStore interface)

func (*InMemoryStore) Delete

func (c *InMemoryStore) Delete(key string) error

Delete - delete an entry

func (InMemoryStore) DeleteExpired

func (c InMemoryStore) DeleteExpired()

DeleteExpired - Delete all expired items from the cache.

func (*InMemoryStore) Flush

func (c *InMemoryStore) Flush() error

Flush (see CacheStore interface)

func (*InMemoryStore) Get

func (c *InMemoryStore) Get(key string, value interface{}) error

Get - Get an entry

func (*InMemoryStore) Increment

func (c *InMemoryStore) Increment(key string, n uint64) (uint64, error)

Increment (see CacheStore interface)

func (*InMemoryStore) NewGenericCacheEntry

func (c *InMemoryStore) NewGenericCacheEntry(data interface{}, exp time.Duration) (newEntry GenericCacheEntry, err error)

NewGenericCacheEntry - create a new in memory cache entry

func (*InMemoryStore) Replace

func (c *InMemoryStore) Replace(key string, value interface{}, exp time.Duration) error

Replace - replace an entry

func (*InMemoryStore) Set

func (c *InMemoryStore) Set(key string, value interface{}, exp time.Duration) error

Set - set an entry

type Level int

Level - define a level for the cache: L1, L2, etc

const (
    // L1 ...
    L1 Level = iota + 1
    // L2 ...
    L2
)
type Type int

Type - define the type of cache: read or write

const (
    // ReadOnly ...
    ReadOnly Type = iota
    // Writable ...
    Writable
)

Generated by godoc2md