Documentation
¶
Index ¶
- Variables
- func Get[RESULT any](cache *Cache, key string, validate func(RESULT) error) (item RESULT, err error)
- func NoValidation[T any](T) error
- func Use[T any](cache *Cache, key string, get func() (T, error), validate func(T) error, ...) (result T, err error)
- func UseWithAsyncRefresh[T any](cache *Cache, key string, get func() (T, error), validate func(T) error, ...) (result T, err error)
- func UseWithExpInGet[T any](cache *Cache, key string, get func() (T, time.Duration, error), ...) (result T, err error)
- type Cache
- type CacheImpl
- type Config
- type Signal
- type ToKey
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = cacheerrors.ErrNotFound
Functions ¶
func Get ¶
func Get[RESULT any](cache *Cache, key string, validate func(RESULT) error) (item RESULT, err error)
Get retrieves values from the Cache, casts the value to the RESULT type and validates the result. The 'validate' function parameter will be called at the end to ensure valid cache values. If the validation error is returned. The validation prevents the use of stale or poisoned cache values. If no validation is needed, the NoValidation[RESULT] function or nil can be used. Get has to be a generic function because else we would lose the type information on l2 cache promotion to l2
func NoValidation ¶
func Use ¶
func Use[T any](cache *Cache, key string, get func() (T, error), validate func(T) error, exp time.Duration, l2Exp ...time.Duration) (result T, err error)
Use tries to retrieve a key from the Cache and cast the result as T if unsuccessful (because a cache-miss or a validation error) the cache is updated wit a value received from the 'get' function parameter the 'exp' and 'l2Exp' parameters are passed to the Cache.Set method and define the expiration time of newly cached values
- 'exp' defines the time until the value is expired and cant be retrieved
- 'l2Exp' (optional) is used to define a separate expiration date for the l2 cache (only used if Cache.l2 is set, only first value is used,if no l2Exp is set, the exp parameter is used)
- the 'validate' function parameter is passed to the Get function to prevent poisoned or stale cache values; if no validation is needed 'NoValidation[T]' or 'nil' may be used
func UseWithAsyncRefresh ¶
func UseWithAsyncRefresh[T any](cache *Cache, key string, get func() (T, error), validate func(T) error, exp time.Duration, l2Exp ...time.Duration) (result T, err error)
UseWithAsyncRefresh returns value from cache/fallback and later tries to update cache from source
func UseWithExpInGet ¶
func UseWithExpInGet[T any](cache *Cache, key string, get func() (T, time.Duration, error), validate func(T) error, fallbackExp time.Duration) (result T, err error)
UseWithExpInGet tries to retrieve a key from the Cache. if unsuccessful (because of a cache-miss or a validation error) the cache is updated wit a value received from the 'get' function parameter. the 'validate' function parameter is passed to the Get function to prevent poisoned or stale cache values.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
func (*Cache) Set ¶
func (this *Cache) Set(key string, value interface{}, exp time.Duration, l2Exp ...time.Duration) (err error)
Set updates the Cache with the key/value pair:
- 'exp' defines the time until the value is expired and cant be retrieved
- 'l2Exp' (optional) is used to define a separate expiration date for the l2 cache (only used if Cache.l2 is set, only first value is used,if no l2Exp is set, the exp parameter is used)
type CacheImpl ¶
type CacheImpl = interfaces.CacheImpl
type Config ¶
type Config struct {
//optional, defaults to localcache.Cache (or L1Provider if provided) with 60s cache duration and 1s cleanup interval
L1 CacheImpl
// optional, may be used to create L1
//
//example value:
// localcache.NewProvider(10*time.Minute, 50*time.Millisecond)
L1Provider func() (CacheImpl, error)
//optional, second layer of cache, example: memcached
L2 CacheImpl
// optional, may be used to create L2
//
// example value:
// memcached.NewProvider(10, 10*time.Second, memcachUrls...)
L2Provider func() (CacheImpl, error) //optional, may be used to create L2
// optional, only used in Use() and UseWithExpInGet() as a fallback to the get parameter in the Use function.
// use-case: service may lose internet connection -> store fallback on a local json file.
Fallback *fallback.Fallback
//optional, used to create Fallback
// use-case: service may lose internet connection -> store fallback on a local json file.
//
//example value:
// fallback.NewProvider("/fb.json")
FallbackProvider func() (*fallback.Fallback, error)
Debug bool
//optional, may be used to accumulate statistics/metrics of the cache use
ReadCacheHook func(duration time.Duration)
//optional, may be used to accumulate statistics/metrics of the cache use
CacheMissHook func()
//optional, used in combination with a signal.Broker (optionally defined in CacheInvalidationSignalBroker). this map stores functions to translate a signal to a cache-key that should be invalidated/deleted.
// the ToKey function may be nil if the cache should be fully reset on receiving the signal.
//
//example:
// c, err := cache.New(cache.Config{
// CacheInvalidationSignalHooks: map[cache.Signal]cache.ToKey{
// signal.Known.CacheInvalidationAll: nil,
// signal.Known.ConceptCacheInvalidation: func(signalValue string) (cacheKey string) {
// return "concept." + signalValue
// },
// signal.Known.CharacteristicCacheInvalidation: func(signalValue string) (cacheKey string) {
// return "characteristics." + signalValue
// },
// signal.Known.FunctionCacheInvalidation: func(signalValue string) (cacheKey string) {
// return "functions." + signalValue
// },
// signal.Known.AspectCacheInvalidation: nil, //invalidate everything, because an aspect corresponds to multiple aspect-nodes
// },
// })
// if err != nil {
// t.Error(err)
// return
// }
//
// err = invalidator.StartKnownCacheInvalidators(ctx, kafka.Config{
// KafkaUrl: kafkaUrl,
// ConsumerGroup: "test",
// StartOffset: kafka.LastOffset,
// Wg: wg,
// }, invalidator.KnownTopics{
// AspectTopic: "aspects"
// ConceptTopic: "concepts"
// CharacteristicTopic: "characteristics",
// FunctionTopic: "functions",
// }, nil)
// if err != nil {
// t.Error(err)
// return
// }
//
// err = invalidator.StartCacheInvalidatorAll(ctx, kafka.Config{
// KafkaUrl: kafkaUrl,
// ConsumerGroup: "test",
// StartOffset: kafka.LastOffset,
// Wg: wg,
// }, []string{"topic-where-all-cache-should-be-reset"}, nil)
// if err != nil {
// t.Error(err)
// return
// }
CacheInvalidationSignalHooks map[Signal]ToKey
//optional, defaults to signal.DefaultBroker if CacheInvalidationSignalHooks is used
CacheInvalidationSignalBroker *signal.Broker
}