cache

package
v0.0.0-...-107dafa Latest Latest
Warning

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

Go to latest
Published: May 23, 2016 License: Apache-2.0 Imports: 13 Imported by: 4

Documentation

Overview

Package Cache contains the CacheManager and CacheStorage formal interface definitions, a SimpleCacheManager, AutoCacheManager and RedisCacheStorage implementations.

Package Cache contains ...

Index

Constants

View Source
const CacheTreeRefPrefix = "attTreeRef"
View Source
const TTL_INFINITY = -1

Variables

This section is empty.

Functions

func GetIdFieldStr

func GetIdFieldStr(object interface{}) (string, error)

Return the "Id" field, if it exists, as a string val Error if object does't have an Id field

func GetKey

func GetKey(value reflect.Value) (string, error)

Retrieve the cachekey for some object Error if object is not Cacheable AND is not possible to inter cacheKey

func GetKeys

func GetKeys(qtd int) []string

func GetNameType

func GetNameType(value interface{}) (string, reflect.Type)

func MinTTL

func MinTTL(ttl1 int, ttl2 int) int

calculate the minimal ttl

Types

type Attribute

type Attribute struct {
	Id    int
	Name  string
	Value string
}

func (*Attribute) DecodeMsg

func (z *Attribute) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (Attribute) EncodeMsg

func (z Attribute) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (Attribute) MarshalMsg

func (z Attribute) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (Attribute) Msgsize

func (z Attribute) Msgsize() (s int)

func (*Attribute) UnmarshalMsg

func (z *Attribute) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type AutoCacheManager

type AutoCacheManager struct {
	Ps CacheStorage
}

func (AutoCacheManager) GetCache

func (c AutoCacheManager) GetCache(cacheKey string) (CacheRegistry, error)

get cache for only one cachekey

func (AutoCacheManager) GetCaches

func (c AutoCacheManager) GetCaches(cacheKeys ...string) (map[string]CacheRegistry, error)

implement getCache operation that can recover child data in other cache registries.

func (AutoCacheManager) Invalidate

func (c AutoCacheManager) Invalidate(cacheKeys ...string) error

invalidate cache registry

func (AutoCacheManager) SetCache

func (c AutoCacheManager) SetCache(cacheRegistries ...CacheRegistry) error

set cache implementation

func (AutoCacheManager) Validade

func (c AutoCacheManager) Validade() bool

type CacheManager

type CacheManager interface {
	//set values to cache
	//SetCache(cacheKey string, cacheVal interface{}, ttl int) int
	SetCache(cacheRegistry ...CacheRegistry) error

	//recover value from cache
	//GetCache(cacheKey string) (interface{}, bool, int)
	GetCache(cacheKey string) (CacheRegistry, error)

	//recover value from cache
	//GetCache(cacheKey string) (interface{}, bool, int)
	GetCaches(cacheKey ...string) (map[string]CacheRegistry, error)

	//Invalidate cache registry. Means that this cache registry is not valid or consistent anymore.
	//Do not means that registry was deleted in original data source. This operation don't update parent registries,
	//removing references to this registry. Parent cache search must fail to find this registry,
	//meaning that all cache registry is inconsistent.
	//To represent a delete operation, updating parent registries, use exclusively the DeleteCache operation in UpdaterCacheManager interface.
	Invalidate(cacheKey ...string) error

	//validate readiness of cache operation
	Validade() bool
}

Define an basic contract for CacheManager A good cache manager implementation must optimize caching operations, like to cache child useful objects recursively, detect caching inconsistency, invalidate cache registries invalidate cacheKey and its dependencies etc Cachemanager get operations must return cache registry always, no matter registry exists or not

type CacheRegistry

type CacheRegistry struct {
	CacheKey  string      //unique key in cache
	Payload   interface{} //payload to be cached, usually an byte array
	StoreTTL  float64     // Cache Time to Live duration
	CacheTime time.Time   //date when cache registry was created
	HasValue  bool        //return the presence of cached value on cache storage. Useful in batch operations and to avoid nil errors
	TypeName  string      //type name of payload. Only serializer implementation can see this attribute
}

CacheRegistry: Contains struct payload to be cached and additional information about cache registry Cachemanager operation must return cache registry always

func GetReg

func GetReg(id int) CacheRegistry

func GetRegs

func GetRegs(qtd int) []CacheRegistry

func (*CacheRegistry) DecodeMsg

func (z *CacheRegistry) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (*CacheRegistry) EncodeMsg

func (z *CacheRegistry) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (CacheRegistry) GetTTLSeconds

func (c CacheRegistry) GetTTLSeconds() float64

Calculates TTl of cache registry

func (*CacheRegistry) MarshalMsg

func (z *CacheRegistry) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (*CacheRegistry) Msgsize

func (z *CacheRegistry) Msgsize() (s int)

func (*CacheRegistry) UnmarshalMsg

func (z *CacheRegistry) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type CacheStorage

type CacheStorage interface {
	//include cache registries
	SetValues(values ...CacheRegistry) error

	//recover cache values (map of values, map of hasValue bool, map of ttls, error)
	//GetValues(keys ...string) (map[string]interface{}, map[string]bool, map[string]int, error)
	GetValuesMap(keys ...string) (map[string]CacheRegistry, error)

	//delete cache values
	DeleteValues(cacheKey ...string) error
}

Basic contract for cache storage. Define basic key/value operations could be implemented with any kind of key/value persistence mechanism a good cachestorage implementation can store and recover data efficiently, like batch recover, using an go routine to store data, maybe how to inherity and share cache areas etc

type Cacheable

type Cacheable interface {
	GetCacheKey() string
}

type Car

type Car struct {
	CarId      int               `json:"carId"`
	CarName    string            `json:"carName"`
	Attributes []Attribute       `json:"specializations,omitempty"`
	FlagMap    map[string]string `json:"flagMap"`
	Ttl        int               `json:"-"`
}

func GetCar

func GetCar(id int) Car

func (*Car) DecodeMsg

func (z *Car) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (*Car) EncodeMsg

func (z *Car) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (*Car) MarshalMsg

func (z *Car) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (*Car) Msgsize

func (z *Car) Msgsize() (s int)

func (*Car) UnmarshalMsg

func (z *Car) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type ExposeTTL

type ExposeTTL interface {
	//teste
	GetTtl() float64
	SetTtl(float64) interface{}
}

means that some struct will define their own ttl. cacheRegistry will copy this ttl definition for cache operations

type RedisCacheStorage

type RedisCacheStorage struct {
	Serializer Serializer // usually SerializerGOB implementation
	// contains filtered or unexported fields
}

Cache storage implementation using redis as key/value storage

func NewRedisCacheStorage

func NewRedisCacheStorage(hostPort string, password string, maxIdle int, maxActive int, readTimeout int, cacheArea string, serializer Serializer) RedisCacheStorage

instantiate a new cachestorage redis

func (RedisCacheStorage) DeleteValues

func (s RedisCacheStorage) DeleteValues(cacheKeys ...string) (retErr error)

delete values from redis

func (RedisCacheStorage) GetTTLMap

func (s RedisCacheStorage) GetTTLMap(keys []string) (retTTLMap map[string]int)

Recover current ttl information about registries

func (RedisCacheStorage) GetValuesMap

func (s RedisCacheStorage) GetValuesMap(cacheKeys ...string) (mapResp map[string]CacheRegistry, retError error)

recover all cacheregistries of keys

func (RedisCacheStorage) SetExpireTTL

func (s RedisCacheStorage) SetExpireTTL(cacheRegistries ...CacheRegistry)

set defined ttl to the cache registries

func (RedisCacheStorage) SetValues

func (s RedisCacheStorage) SetValues(registries ...CacheRegistry) (retErr error)

save informed registries on redis

type Serializer

type Serializer interface {

	//register the type for future reference
	Register(sample interface{})

	// MarshalMsg implements msgp.Marshaler
	MarshalMsg(src CacheRegistry, b []byte) (o []byte, err error)

	// UnmarshalMsg implements msgp.Unmarshaler
	UnmarshalMsg(dest CacheRegistry, bts []byte) (resp interface{}, o []byte, err error)

	//prefix for this serializer, enable multiple serializer at same respository (redis)
	GetPrefix() string
}

type SerializerGOB

type SerializerGOB struct {
}

func (SerializerGOB) GetPrefix

func (s SerializerGOB) GetPrefix() string

func (SerializerGOB) MarshalMsg

func (SerializerGOB) MarshalMsg(src CacheRegistry, b []byte) (o []byte, err error)

seralize an objeto to byte array

func (SerializerGOB) Register

func (s SerializerGOB) Register(value interface{})

func (SerializerGOB) UnmarshalMsg

func (SerializerGOB) UnmarshalMsg(dest CacheRegistry, bts []byte) (resp interface{}, o []byte, err error)

deserialize an byte array to object

type SimpleCacheManager

type SimpleCacheManager struct {
	CacheStorage CacheStorage
}

func (SimpleCacheManager) GetCache

func (c SimpleCacheManager) GetCache(cacheKey string) (CacheRegistry, error)

implement getCache operation that can recover child data in other cache registries.

func (SimpleCacheManager) GetCaches

func (c SimpleCacheManager) GetCaches(cacheKeys ...string) (map[string]CacheRegistry, error)

implement getCache operation that can recover child data in other cache registries.

func (SimpleCacheManager) Invalidate

func (c SimpleCacheManager) Invalidate(cacheKeys ...string) error

invalidate cache registry

func (SimpleCacheManager) SetCache

func (c SimpleCacheManager) SetCache(cacheRegistry ...CacheRegistry) error

set cache implementation

func (SimpleCacheManager) Validade

func (c SimpleCacheManager) Validade() bool

type Stats

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

basic structure for statistics service

func NewStats

func NewStats(name string) *Stats

create new stats object for cache statistics

func (*Stats) Hit

func (st *Stats) Hit()

increment cache hit

func (*Stats) Miss

func (st *Stats) Miss()

increment cache Miss

type UpdateCachedRelated

type UpdateCachedRelated interface {
	//who is my parent
	ParentsKey() []string

	//For delete operation
	RemoveFromParent(interface{}) interface{}

	//For insert operations
	InsertInParent(interface{}) interface{}
}

Implementation of this interface means that struct knows how to update his relateds Cached, when the struct is deleted or inserted. There is nothing to do when struct is updated

type UpdaterCacheManager

type UpdaterCacheManager interface {
	CacheManager // import all CacheManager definitions
	//Means that cache registry was deleted on the source, and that parent registries must be updated.
	DeleteCache(cacheRegistry CacheRegistry)
}

Define a extension contract to CacheManager, with DeleteCache operation. UpdateCacheManager contract can be used, actively, in events of Create, Update and Delete of registries. CacheManager can be used only, passively, in Read operations

Jump to

Keyboard shortcuts

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