client

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultCacheTTL specifies the default item TTL for in memory cache
	DefaultCacheTTL time.Duration = 30 * time.Second
)
View Source
const SkipCacheTTL time.Duration = 0

SkipCacheTTL is TTL set when cache is not used

Variables

This section is empty.

Functions

func GetItemFromCache

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

GetItemFromCache gets the the value stored in key from the cache. The value could a single item or an array of items

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, ttl time.Duration)

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, 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, 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)
}

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

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
	// TTL returns the TTL for the item
	TTL(c CacheTTLProvider) time.Duration
}

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() *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, force bool) error

DeleteValue deletes the value(s) in passed in keys

func (*InMemoryClientCacheProvider) Flush

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

func (*InMemoryClientCacheProvider) GetValue

GetValue gets the value in CacheKey (if any) and tries to lock the key for Read is lockOnMiss = 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 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) *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, force bool) error

DeleteValue deletes the value(s) in passed in keys

func (*RedisClientCacheProvider) Flush

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

func (*RedisClientCacheProvider) GetValue

func (c *RedisClientCacheProvider) GetValue(ctx context.Context, keyIn shared.CacheKey, lockOnMiss bool) (*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) 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