client

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// RegionalRedisCacheName is default name of the regional redis cache
	RegionalRedisCacheName = "redisRegionalCache"
)
View Source
const SkipCacheTTL time.Duration = 0

SkipCacheTTL is TTL set when cache is not used

Variables

This section is empty.

Functions

func DeleteItemFromCache added in v0.7.7

func DeleteItemFromCache[item CacheSingleItem](ctx context.Context, c CacheManager, i item, sentinel shared.CacheSentinel)

DeleteItemFromCache deletes the values stored in key associated with the item from the cache.

func GetItemFromCache

func GetItemFromCache[item CacheSingleItem](ctx context.Context, c CacheManager, key shared.CacheKey, lockOnMiss bool) (*item, shared.CacheSentinel, shared.CacheSentinel, error)

GetItemFromCache gets the the value stored in key from the cache. The value should be single item

func GetItemFromCacheWithModifiedKey added in v1.0.0

func GetItemFromCacheWithModifiedKey[item CacheSingleItem](ctx context.Context, c CacheManager, key shared.CacheKey, isModifiedKey shared.CacheKey, lockOnMiss bool) (*item, shared.CacheSentinel, shared.CacheSentinel, error)

GetItemFromCacheWithModifiedKey gets the the value stored in the key from the cache, while also returning the state of isModified key. The value should be single item

func GetItemsArrayFromCache added in v0.8.2

func GetItemsArrayFromCache[item CacheSingleItem](ctx context.Context, c CacheManager, key shared.CacheKey, lockOnMiss bool) (*[]item, shared.CacheSentinel, shared.CacheSentinel, error)

GetItemsArrayFromCache gets the value stored in key from the cache. The value should be an array of items

func GetItemsFromCache added in v0.8.0

func GetItemsFromCache[item CacheSingleItem](ctx context.Context, c CacheManager, keys []shared.CacheKey, locksOnMiss []bool) ([]*item, []shared.CacheSentinel, error)

GetItemsFromCache gets the the values stored in keys from the cache.

func ReleaseItemLock

func ReleaseItemLock[item CacheSingleItem](ctx context.Context, c CacheManager, lockType shared.SentinelType, i item, sentinel shared.CacheSentinel)

ReleaseItemLock releases the lock for the given item

func ReleasePerItemCollectionLock

func ReleasePerItemCollectionLock[item CacheSingleItem](ctx context.Context, c CacheManager, additionalColKeys []shared.CacheKey, i item, sentinel shared.CacheSentinel)

ReleasePerItemCollectionLock releases the lock for the collection associated with a given item

func SaveItemToCache

func SaveItemToCache[item CacheSingleItem](ctx context.Context, c CacheManager, i item, sentinel shared.CacheSentinel,
	clearCollection bool, additionalColKeys []shared.CacheKey)

SaveItemToCache saves the given item to the cache

func SaveItemsFromCollectionToCache

func SaveItemsFromCollectionToCache[item CacheSingleItem](ctx context.Context, c CacheManager, items []item, sentinel shared.CacheSentinel)

SaveItemsFromCollectionToCache saves the items from a given collection into their separate keys

func SaveItemsToCollection

func SaveItemsToCollection[item CacheSingleItem, cItem CacheSingleItem](ctx context.Context, c CacheManager,
	i item, colItems []cItem, lockKey shared.CacheKey, colKey shared.CacheKey, sentinel shared.CacheSentinel, isGlobal bool)

SaveItemsToCollection saves the given collection to collection key associated with the item or global to item type If this is a per item collection than "item" argument is the item with with the collection is associated and "cItems" is the collection to be stored.

func TakeItemLock

func TakeItemLock[item CacheSingleItem](ctx context.Context, lockType shared.SentinelType, c CacheManager, i item) (shared.CacheSentinel, error)

TakeItemLock takes a lock for the given item. Typically used for Create, Update, Delete operations on an item

func TakePerItemCollectionLock

func TakePerItemCollectionLock[item CacheSingleItem](ctx context.Context, lockType shared.SentinelType, c CacheManager, additionalColKeys []shared.CacheKey, i item) (shared.CacheSentinel, error)

TakePerItemCollectionLock takes a lock for the collection associated with a given item

Types

type CacheKeyNameID

type CacheKeyNameID string

CacheKeyNameID is the type for the ID used to identify the cache key name via CacheKeyNameProvider interface

type CacheKeyNameProvider

type CacheKeyNameProvider interface {
	GetKeyName(id CacheKeyNameID, components []string) shared.CacheKey
	// GetKeyNameWithID is a wrapper around GetKeyName that converts the itemID to []string
	GetKeyNameWithID(id CacheKeyNameID, itemID uuid.UUID) shared.CacheKey
	// GetKeyNameWithString is a wrapper around GetKeyName that converts the itemName to []string
	GetKeyNameWithString(id CacheKeyNameID, itemName string) shared.CacheKey
	// GetKeyNameStatic is a wrapper around GetKeyName that passing in empty []string
	GetKeyNameStatic(id CacheKeyNameID) shared.CacheKey
}

CacheKeyNameProvider is the interface for the container that can provide cache names for cache keys that can be shared across different cache providers

type CacheKeyTTLID

type CacheKeyTTLID string

CacheKeyTTLID is the type for the ID used to identify the cache key TTL via CacheTTLProvider interface

type CacheManager

type CacheManager struct {
	P CacheProvider
	N CacheKeyNameProvider
	T CacheTTLProvider
}

CacheManager is the bundle cache classes that are needed to interact with the cache

func NewCacheManager

NewCacheManager returns a new CacheManager with given contents

type CacheProvider

type CacheProvider interface {
	// GetValue gets the value in cache key (if any) and tries to lock the key for Read is lockOnMiss = true
	GetValue(ctx context.Context, key shared.CacheKey, lockOnMiss bool) (*string, *string, shared.CacheSentinel, error)
	// GetValues gets the value in cache key (if any) and tries to lock the key for Read is lockOnMiss = true
	GetValues(ctx context.Context, keys []shared.CacheKey, lockOnMiss []bool) ([]*string, []*string, []shared.CacheSentinel, error)
	// SetValue sets the value in cache key(s) to val with given expiration time if the sentinel matches lkey and returns true if the value was set
	SetValue(ctx context.Context, lkey shared.CacheKey, keysToSet []shared.CacheKey, val string, sentinel shared.CacheSentinel, ttl time.Duration) (bool, bool, error)
	// DeleteValue deletes the value(s) in passed in keys, force is true also deletes keys with sentinel or tombstone values
	DeleteValue(ctx context.Context, key []shared.CacheKey, setTombstone bool, force bool) error
	// WriteSentinel writes the sentinel value into the given keys, returns NoLockSentinel if it couldn't acquire the lock
	WriteSentinel(ctx context.Context, stype shared.SentinelType, keys []shared.CacheKey) (shared.CacheSentinel, error)
	// ReleaseSentinel clears the sentinel value from the given keys
	ReleaseSentinel(ctx context.Context, keys []shared.CacheKey, s shared.CacheSentinel)
	// AddDependency adds the given cache key(s) as dependencies of an item represented by by key. Fails if any of the dependency keys passed in contain tombstone
	AddDependency(ctx context.Context, keysIn []shared.CacheKey, dependentKey []shared.CacheKey, ttl time.Duration) error
	// ClearDependencies clears the dependencies of an item represented by key and removes all dependent keys from the cache
	ClearDependencies(ctx context.Context, key shared.CacheKey, setTombstone bool) error
	// Flush flushes the cache
	Flush(ctx context.Context, prefix string, flushTombstones bool) error
	// GetCacheName returns the global name of the cache if any
	GetCacheName(ctx context.Context) string
}

CacheProvider is the interface for the cache backend for a given tenant which can be implemented by in-memory, redis, memcache, etc

type CacheSentinelManager added in v1.0.0

type CacheSentinelManager interface {
	GenerateSentinel(stype shared.SentinelType) shared.CacheSentinel
	CanAlwaysSetSentinel(newVal shared.CacheSentinel) bool
	CanSetSentinelGivenCurrVal(currVal shared.CacheSentinel, newVal shared.CacheSentinel) bool
	CanSetValue(currVal string, val string, sentinel shared.CacheSentinel) (set bool, clear bool, conflict bool, refresh bool)
	IsSentinelValue(val string) bool
}

CacheSentinelManager is the interface for managaing cache sentinels to implement concurrency handling

type CacheSingleItem

type CacheSingleItem interface {
	// GetPrimaryKey returns the primary cache key where the item is stored and which is used to lock the item
	GetPrimaryKey(c CacheKeyNameProvider) shared.CacheKey
	// GetSecondaryKeys returns any secondary keys which also contain the item for lookup by another dimension (ie TypeName, Alias, etc)
	GetSecondaryKeys(c CacheKeyNameProvider) []shared.CacheKey
	// GetGlobalCollectionKey returns the key for the collection of all items of this type (ie all ObjectTypes, all EdgeTypes, etc)
	GetGlobalCollectionKey(c CacheKeyNameProvider) shared.CacheKey
	// GetPerItemCollectionKey returns the key for the collection of per item items of another type (ie Edges in/out of a specific Object)
	GetPerItemCollectionKey(c CacheKeyNameProvider) shared.CacheKey
	// GetDependenciesKey returns the key containing dependent keys that should invalidated if the item is invalidated
	GetDependenciesKey(c CacheKeyNameProvider) shared.CacheKey
	// GetDependencyKeys returns the list of keys for items this item depends on (ie Edge depends on both source and target objects)
	GetDependencyKeys(c CacheKeyNameProvider) []shared.CacheKey
	// GetIsModifiedKey returns the key containing a tombstone sentinel if the item has been modified in last TTL seconds
	GetIsModifiedKey(c CacheKeyNameProvider) shared.CacheKey
	// TTL returns the TTL for the item
	TTL(c CacheTTLProvider) time.Duration
	// Validate method is used to validate the item. Every CacheSingleItem is expected to implement Validatable interface
	infra.Validateable
}

CacheSingleItem is an interface for any single non array item that can be stored in the cache This interface also links the type (ObjectType, EdgeType, Object, Edge) with the cache key names for each type of use

type CacheTTLProvider

type CacheTTLProvider interface {
	TTL(id CacheKeyTTLID) time.Duration
}

CacheTTLProvider is the interface for the container that can provide per item cache TTLs

type InMemoryClientCacheProvider

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

InMemoryClientCacheProvider is the base implementation of the CacheProvider interface

func NewInMemoryClientCacheProvider

func NewInMemoryClientCacheProvider(cacheName string, opts ...OptionInMem) *InMemoryClientCacheProvider

NewInMemoryClientCacheProvider creates a new InMemoryClientCacheProvider

func (*InMemoryClientCacheProvider) AddDependency

func (c *InMemoryClientCacheProvider) AddDependency(ctx context.Context, keysIn []shared.CacheKey, values []shared.CacheKey, ttl time.Duration) error

AddDependency adds the given cache key(s) as dependencies of an item represented by by key

func (*InMemoryClientCacheProvider) ClearDependencies

func (c *InMemoryClientCacheProvider) ClearDependencies(ctx context.Context, key shared.CacheKey, setTombstone bool) error

ClearDependencies clears the dependencies of an item represented by key and removes all dependent keys from the cache

func (*InMemoryClientCacheProvider) DeleteValue

func (c *InMemoryClientCacheProvider) DeleteValue(ctx context.Context, keysIn []shared.CacheKey, setTombstone bool, force bool) error

DeleteValue deletes the value(s) in passed in keys

func (*InMemoryClientCacheProvider) Flush

func (c *InMemoryClientCacheProvider) Flush(ctx context.Context, prefix string, flushTombstones bool) error

Flush flushes the cache (applies only to the tenant for which the client was created)

func (*InMemoryClientCacheProvider) GetCacheName added in v0.7.6

func (c *InMemoryClientCacheProvider) GetCacheName(ctx context.Context) string

GetCacheName returns the name of the cache

func (*InMemoryClientCacheProvider) GetValue

GetValue gets the value in CacheKey (if any) and tries to lock the key for Read is lockOnMiss = true

func (*InMemoryClientCacheProvider) GetValues added in v0.8.0

func (c *InMemoryClientCacheProvider) GetValues(ctx context.Context, keys []shared.CacheKey, lockOnMiss []bool) ([]*string, []*string, []shared.CacheSentinel, error)

GetValues gets the values in keys (if any) and tries to lock the key[i] for Read is lockOnMiss[i] = true

func (*InMemoryClientCacheProvider) ReleaseSentinel

func (c *InMemoryClientCacheProvider) ReleaseSentinel(ctx context.Context, keysIn []shared.CacheKey, s shared.CacheSentinel)

ReleaseSentinel clears the sentinel value from the given keys

func (*InMemoryClientCacheProvider) SetValue

func (c *InMemoryClientCacheProvider) SetValue(ctx context.Context, lkeyIn shared.CacheKey, keysToSet []shared.CacheKey, val string,
	sentinel shared.CacheSentinel, ttl time.Duration) (bool, bool, error)

SetValue sets the value in cache key(s) to val with given expiration time if the sentinel matches and returns true if the value was set

func (*InMemoryClientCacheProvider) WriteSentinel

WriteSentinel writes the sentinel value into the given keys

type OptionInMem added in v1.0.0

type OptionInMem interface {
	// contains filtered or unexported methods
}

OptionInMem specifies optional arguement for InMemoryClientCacheProvider

func SentinelManagerInMem added in v1.0.0

func SentinelManagerInMem(sm CacheSentinelManager) OptionInMem

SentinelManagerInMem allows to specify a custom sentinel manager

type OptionRedis added in v1.0.0

type OptionRedis interface {
	// contains filtered or unexported methods
}

OptionRedis specifies optional arguement for RedisClientCacheProvider

func KeyPrefixRedis added in v1.0.0

func KeyPrefixRedis(prefix string) OptionRedis

KeyPrefixRedis allows specifying a key prefix that all keys managed by this cache have to have

func SentinelManagerRedis added in v1.0.0

func SentinelManagerRedis(sm CacheSentinelManager) OptionRedis

SentinelManagerRedis allows specifying a custom CacheSentinelManager

type RedisClientCacheProvider

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

RedisClientCacheProvider is the base implementation of the CacheProvider interface

func NewRedisClientCacheProvider

func NewRedisClientCacheProvider(rc *redis.Client, cacheName string, opts ...OptionRedis) *RedisClientCacheProvider

NewRedisClientCacheProvider creates a new RedisClientCacheProvider

func (*RedisClientCacheProvider) AddDependency

func (c *RedisClientCacheProvider) AddDependency(ctx context.Context, keysIn []shared.CacheKey, values []shared.CacheKey, ttl time.Duration) error

AddDependency adds the given cache key(s) as dependencies of an item represented by by key

func (*RedisClientCacheProvider) ClearDependencies

func (c *RedisClientCacheProvider) ClearDependencies(ctx context.Context, keyIn shared.CacheKey, setTombstone bool) error

ClearDependencies clears the dependencies of an item represented by key and removes all dependent keys from the cache

func (*RedisClientCacheProvider) DeleteValue

func (c *RedisClientCacheProvider) DeleteValue(ctx context.Context, keysIn []shared.CacheKey, setTombstone bool, force bool) error

DeleteValue deletes the value(s) in passed in keys

func (*RedisClientCacheProvider) Flush

func (c *RedisClientCacheProvider) Flush(ctx context.Context, prefix string, flushTombstones bool) error

Flush flushes the cache (applies only to the tenant for which the client was created)

func (*RedisClientCacheProvider) GetCacheName added in v0.7.6

func (c *RedisClientCacheProvider) GetCacheName(ctx context.Context) string

GetCacheName returns the name of the cache

func (*RedisClientCacheProvider) GetValue

func (c *RedisClientCacheProvider) GetValue(ctx context.Context, keyIn shared.CacheKey, lockOnMiss bool) (*string, *string, shared.CacheSentinel, error)

GetValue gets the value in CacheKey (if any) and tries to lock the key for Read is lockOnMiss = true

func (*RedisClientCacheProvider) GetValues added in v0.8.0

func (c *RedisClientCacheProvider) GetValues(ctx context.Context, keysIn []shared.CacheKey, lockOnMiss []bool) ([]*string, []*string, []shared.CacheSentinel, error)

GetValues gets the value in cache keys (if any) and tries to lock the keys[i] for Read is lockOnMiss[i] = true

func (*RedisClientCacheProvider) ReleaseSentinel

func (c *RedisClientCacheProvider) ReleaseSentinel(ctx context.Context, keysIn []shared.CacheKey, s shared.CacheSentinel)

ReleaseSentinel clears the sentinel value from the given keys

func (*RedisClientCacheProvider) SetValue

func (c *RedisClientCacheProvider) SetValue(ctx context.Context, lkeyIn shared.CacheKey, keysToSet []shared.CacheKey, val string,
	sentinel shared.CacheSentinel, ttl time.Duration) (bool, bool, error)

SetValue sets the value in cache key(s) to val with given expiration time if the sentinel matches and returns true if the value was set

func (*RedisClientCacheProvider) WriteSentinel

WriteSentinel writes the sentinel value into the given keys

Jump to

Keyboard shortcuts

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