Documentation ¶
Overview ¶
Package Cache contains the CacheManager and CacheStorage formal interface definitions, a SimpleCacheManager, AutoCacheManager and RedisCacheStorage implementations.
Package Cache contains ...
Index ¶
- Constants
- func GetIdFieldStr(object interface{}) (string, error)
- func GetKey(value reflect.Value) (string, error)
- func MinTTL(ttl1 int, ttl2 int) int
- type AutoCacheManager
- func (c AutoCacheManager) GetCache(cacheKey string) (CacheRegistry, error)
- func (c AutoCacheManager) GetCacheTTL(cacheKey string) (int, error)
- func (c AutoCacheManager) GetCaches(cacheKeys ...string) (map[string]CacheRegistry, error)
- func (c AutoCacheManager) Invalidate(cacheKeys ...string) error
- func (c AutoCacheManager) SetCache(cacheRegistries ...CacheRegistry) error
- func (c AutoCacheManager) Validade() bool
- type CacheManager
- type CacheRegistry
- type CacheStorage
- type Cacheable
- type ExposeTTL
- type RedisCacheStorage
- func (s RedisCacheStorage) DeleteValues(cacheKeys ...string) error
- func (s RedisCacheStorage) GetActualTTL(mapCacheRegistry map[string]CacheRegistry) (map[string]CacheRegistry, error)
- func (s RedisCacheStorage) GetTTL(key string) (int, error)
- func (s RedisCacheStorage) GetTTLMap(keys []string) map[string]int
- func (s RedisCacheStorage) GetValuesMap(cacheKeys ...string) (map[string]CacheRegistry, error)
- func (s RedisCacheStorage) SetExpireTTL(cacheRegistries ...CacheRegistry)
- func (s RedisCacheStorage) SetValues(registries ...CacheRegistry) error
- type SimpleCacheManager
- func (c SimpleCacheManager) GetCache(cacheKey string) (CacheRegistry, error)
- func (c SimpleCacheManager) GetCacheTTL(cacheKey string) (int, error)
- func (c SimpleCacheManager) GetCaches(cacheKeys ...string) (map[string]CacheRegistry, error)
- func (c SimpleCacheManager) Invalidate(cacheKeys ...string) error
- func (c SimpleCacheManager) SetCache(cacheRegistry ...CacheRegistry) error
- func (c SimpleCacheManager) Validade() bool
- type Stats
- type UpdateCachedRelated
- type UpdaterCacheManager
Constants ¶
const CacheTreeRefPrefix = "attTreeRef"
const TTL_INFINITY = -1
Variables ¶
This section is empty.
Functions ¶
func GetIdFieldStr ¶
Return the "Id" field, if it exists, as a string val Error if object does't have an Id field
Types ¶
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) GetCacheTTL ¶
func (c AutoCacheManager) GetCacheTTL(cacheKey string) (int, error)
return time to live
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) //return time to live of cacheKey GetCacheTTL(cacheKey string) (int, 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 Ttl int //time to live HasValue bool //return the presence of cached value on cache storage. Useful in batch operations and to avoid nil errors }
CacheRegistry: Contains struct payload to be cached and additional information about cache registry Cachemanager operation must return cache registry always
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) //recover ttl of registry GetTTL(keys string) (int, 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 ExposeTTL ¶
means that some struct will define their own ttl. cacheRegistry will copy this ttl definition for cache operations
type RedisCacheStorage ¶
type RedisCacheStorage struct {
// contains filtered or unexported fields
}
Cache storage implementation using redis as key/value storage
func NewRedisCacheStorage ¶
func NewRedisCacheStorage(hostPort string, password string, maxIdle int, readTimeout int, ttlReadTimeout int, cacheArea string) RedisCacheStorage
instantiate a new cachestorage redis
func (RedisCacheStorage) DeleteValues ¶
func (s RedisCacheStorage) DeleteValues(cacheKeys ...string) error
delete values from redis
func (RedisCacheStorage) GetActualTTL ¶
func (s RedisCacheStorage) GetActualTTL(mapCacheRegistry map[string]CacheRegistry) (map[string]CacheRegistry, error)
Recover current ttl information about registries
func (RedisCacheStorage) GetTTL ¶
func (s RedisCacheStorage) GetTTL(key string) (int, error)
Recover current ttl information about registry
func (RedisCacheStorage) GetTTLMap ¶
func (s RedisCacheStorage) GetTTLMap(keys []string) map[string]int
Recover current ttl information about registries
func (RedisCacheStorage) GetValuesMap ¶
func (s RedisCacheStorage) GetValuesMap(cacheKeys ...string) (map[string]CacheRegistry, 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) error
save informed registries on redis
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) GetCacheTTL ¶
func (c SimpleCacheManager) GetCacheTTL(cacheKey string) (int, error)
return time to live
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
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