cache

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// SentinelTTL value when setting a sentinel value in the cache
	SentinelTTL = 60 * time.Second
	// InvalidationTombstoneTTL value when setting a tombstone value in the cache for cross region invalidation
	InvalidationTombstoneTTL = 5 * time.Second

	// TombstoneSentinel represents the sentinel value for a tombstone
	TombstoneSentinel Sentinel = "Tombstone"

	// NoLockSentinel represents the sentinel value for no lock
	NoLockSentinel Sentinel = ""
)
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 CreateItem

func CreateItem[item SingleItem](ctx context.Context, cm *Manager, id uuid.UUID, i *item, keyID KeyNameID, secondaryKey Key, ifNotExists bool,
	bypassCache bool, additionalKeysToClear []Key, action func(i *item) (*item, error), equals func(input *item, current *item) bool) (*item, error)

CreateItem creates an item

func CreateItemClient

func CreateItemClient[item SingleItem](ctx context.Context, cm *Manager, id uuid.UUID, i *item, keyID KeyNameID, secondaryKey Key, ifNotExists bool,
	bypassCache bool, additionalKeysToClear []Key, action func(i *item) (*item, error), equals func(input *item, current *item) bool) (*item, error)

CreateItemClient creates an item (wrapper for calls from client)

func CreateItemServer

func CreateItemServer[item SingleItem](ctx context.Context, cm *Manager, i *item, keyID KeyNameID, additionalKeysToClear []Key, action func(i *item) error) error

CreateItemServer creates an item (wrapper for calls from ORM)

func DeleteItemFromCache

func DeleteItemFromCache[item SingleItem](ctx context.Context, c Manager, i item, sentinel Sentinel)

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

func GetItem

func GetItem[item SingleItem](ctx context.Context, cm *Manager, id uuid.UUID, keyID KeyNameID, modifiedKeyID KeyNameID, bypassCache bool, action func(id uuid.UUID, conflict Sentinel, i *item) error) (*item, error)

GetItem returns the item

func GetItemClient

func GetItemClient[item SingleItem](ctx context.Context, cm Manager, id uuid.UUID, keyID KeyNameID, bypassCache bool, action func(id uuid.UUID, conflict Sentinel, i *item) error) (*item, error)

GetItemClient returns the item (wrapper for calls from client)

func GetItemFromCache

func GetItemFromCache[item SingleItem](ctx context.Context, c Manager, key Key, lockOnMiss bool) (*item, Sentinel, Sentinel, error)

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

func GetItemFromCacheWithModifiedKey

func GetItemFromCacheWithModifiedKey[item SingleItem](ctx context.Context, c Manager, key Key, isModifiedKey Key, lockOnMiss bool) (*item, Sentinel, Sentinel, 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

func GetItemsArrayFromCache[item SingleItem](ctx context.Context, c Manager, key Key, lockOnMiss bool) (*[]item, Sentinel, Sentinel, error)

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

func IsInvalidatingSentinelValue

func IsInvalidatingSentinelValue(v Sentinel) bool

IsInvalidatingSentinelValue returns true if the sentinel requires invalidating the value across other

func IsTombstoneSentinel

func IsTombstoneSentinel(data string) bool

IsTombstoneSentinel returns true if the given data is a tombstone sentinel

func ReleaseItemLock

func ReleaseItemLock[item SingleItem](ctx context.Context, c Manager, lockType SentinelType, i item, sentinel Sentinel)

ReleaseItemLock releases the lock for the given item

func ReleasePerItemCollectionLock

func ReleasePerItemCollectionLock[item SingleItem](ctx context.Context, c Manager, additionalColKeys []Key, i item, sentinel Sentinel)

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

func SaveItemToCache

func SaveItemToCache[item SingleItem](ctx context.Context, c Manager, i item, sentinel Sentinel,
	clearCollection bool, additionalColKeys []Key)

SaveItemToCache saves the given item to the cache

func SaveItemsFromCollectionToCache

func SaveItemsFromCollectionToCache[item SingleItem](ctx context.Context, c Manager, items []item, sentinel Sentinel)

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

func SaveItemsToCollection

func SaveItemsToCollection[item SingleItem, cItem SingleItem](ctx context.Context, c Manager,
	i item, colItems []cItem, lockKey Key, colKey Key, sentinel Sentinel, 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 ServerGetItem

func ServerGetItem[item SingleItem](ctx context.Context, cm *Manager, id uuid.UUID, keyID KeyNameID, modifiedKeyID KeyNameID, action func(id uuid.UUID, conflict Sentinel, i *item) error) (*item, error)

ServerGetItem returns the item (wrapper for calls from ORM)

Types

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 []Key, values []Key, 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 Key, 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 []Key, 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

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

GetCacheName returns the name of the cache

func (*InMemoryClientCacheProvider) GetValue

func (c *InMemoryClientCacheProvider) GetValue(ctx context.Context, keyIn Key, lockOnMiss bool) (*string, *string, Sentinel, error)

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

func (*InMemoryClientCacheProvider) GetValues

func (c *InMemoryClientCacheProvider) GetValues(ctx context.Context, keys []Key, lockOnMiss []bool) ([]*string, []*string, []Sentinel, error)

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

func (*InMemoryClientCacheProvider) LogKeyValues added in v1.2.0

func (c *InMemoryClientCacheProvider) LogKeyValues(ctx context.Context, prefix string) error

LogKeyValues logs all keys and values in the cache

func (*InMemoryClientCacheProvider) RegisterInvalidationHandler

func (c *InMemoryClientCacheProvider) RegisterInvalidationHandler(ctx context.Context, handler InvalidationHandler, key Key) error

RegisterInvalidationHandler registers a handler for cache invalidation

func (*InMemoryClientCacheProvider) ReleaseSentinel

func (c *InMemoryClientCacheProvider) ReleaseSentinel(ctx context.Context, keysIn []Key, s Sentinel)

ReleaseSentinel clears the sentinel value from the given keys

func (*InMemoryClientCacheProvider) SetValue

func (c *InMemoryClientCacheProvider) SetValue(ctx context.Context, lkeyIn Key, keysToSet []Key, val string,
	sentinel Sentinel, 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

func (c *InMemoryClientCacheProvider) WriteSentinel(ctx context.Context, stype SentinelType, keysIn []Key) (Sentinel, error)

WriteSentinel writes the sentinel value into the given keys

type InvalidationHandler

type InvalidationHandler func(ctx context.Context, key Key) error

InvalidationHandler is the type for a function that is called when the cache is invalidated

type Key

type Key string

Key is the type storing the cache key name. It is a string but is a separate type to avoid bugs related to mixing up strings.

type KeyNameID

type KeyNameID string

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

type KeyNameProvider

type KeyNameProvider interface {
	GetKeyName(id KeyNameID, components []string) Key
	// GetKeyNameWithID is a wrapper around GetKeyName that converts the itemID to []string
	GetKeyNameWithID(id KeyNameID, itemID uuid.UUID) Key
	// GetKeyNameWithString is a wrapper around GetKeyName that converts the itemName to []string
	GetKeyNameWithString(id KeyNameID, itemName string) Key
	// GetKeyNameStatic is a wrapper around GetKeyName that passing in empty []string
	GetKeyNameStatic(id KeyNameID) Key
	// GetPrefix returns the prefix for the cache keys
	GetPrefix() string
	// GetAllKeyIDs returns all the key IDs that are used by the cache
	GetAllKeyIDs() []string
}

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

type KeyTTLID

type KeyTTLID string

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

type Manager

type Manager struct {
	P Provider
	N KeyNameProvider
	T TTLProvider
}

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

func NewManager

func NewManager(p Provider, n KeyNameProvider, t TTLProvider) Manager

NewManager returns a new CacheManager with given contents

func (Manager) Flush

func (cm Manager) Flush(ctx context.Context, objType string) error

Flush flushes the cache

type OptionInMem

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

OptionInMem specifies optional arguement for InMemoryClientCacheProvider

func SentinelManagerInMem

func SentinelManagerInMem(sm SentinelManager) OptionInMem

SentinelManagerInMem allows to specify a custom sentinel manager

type OptionRedis

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

OptionRedis specifies optional arguement for RedisClientCacheProvider

func KeyPrefixRedis

func KeyPrefixRedis(prefix string) OptionRedis

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

func ReadOnlyRedis

func ReadOnlyRedis() OptionRedis

ReadOnlyRedis specifies that the cache provider will not make any modfications It will only read the values via GetValues and GetValue if they are there and make not other modifications otherwise

func SentinelManagerRedis

func SentinelManagerRedis(sm SentinelManager) OptionRedis

SentinelManagerRedis allows specifying a custom CacheSentinelManager

type Provider

type Provider 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 Key, lockOnMiss bool) (*string, *string, Sentinel, 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 []Key, lockOnMiss []bool) ([]*string, []*string, []Sentinel, 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 Key, keysToSet []Key, val string, sentinel Sentinel, 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 []Key, 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 SentinelType, keys []Key) (Sentinel, error)
	// ReleaseSentinel clears the sentinel value from the given keys
	ReleaseSentinel(ctx context.Context, keys []Key, s Sentinel)
	// 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 []Key, dependentKey []Key, 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 Key, 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
	// RegisterInvalidationHandler registers a handler to be called when the specified key is invalidated
	RegisterInvalidationHandler(ctx context.Context, handler InvalidationHandler, key Key) error
	// LogKeyValues is debugging only method that logs the values of the keys with the given prefix
	LogKeyValues(ctx context.Context, prefix string) error
}

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

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 []Key, values []Key, 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 Key, 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 []Key, 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

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 Key, lockOnMiss bool) (*string, *string, Sentinel, error)

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

func (*RedisClientCacheProvider) GetValues

func (c *RedisClientCacheProvider) GetValues(ctx context.Context, keysIn []Key, lockOnMiss []bool) ([]*string, []*string, []Sentinel, 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) LogKeyValues added in v1.2.0

func (c *RedisClientCacheProvider) LogKeyValues(ctx context.Context, prefix string) error

LogKeyValues logs the key values in the cache with given prefix

func (*RedisClientCacheProvider) RegisterInvalidationHandler

func (c *RedisClientCacheProvider) RegisterInvalidationHandler(ctx context.Context, handler InvalidationHandler, key Key) error

RegisterInvalidationHandler registers a handler for cache invalidation

func (*RedisClientCacheProvider) ReleaseSentinel

func (c *RedisClientCacheProvider) ReleaseSentinel(ctx context.Context, keysIn []Key, s Sentinel)

ReleaseSentinel clears the sentinel value from the given keys

func (*RedisClientCacheProvider) SetValue

func (c *RedisClientCacheProvider) SetValue(ctx context.Context, lkeyIn Key, keysToSet []Key, val string,
	sentinel Sentinel, 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

func (c *RedisClientCacheProvider) WriteSentinel(ctx context.Context, stype SentinelType, keysIn []Key) (Sentinel, error)

WriteSentinel writes the sentinel value into the given keys

type Sentinel

type Sentinel string

Sentinel is the type storing in the cache marker for in progress operation

func GetItemsFromCache

func GetItemsFromCache[item SingleItem](ctx context.Context, c Manager, keys []Key, locksOnMiss []bool) ([]*item, []Sentinel, error)

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

func TakeItemLock

func TakeItemLock[item SingleItem](ctx context.Context, lockType SentinelType, c Manager, i item) (Sentinel, error)

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

func TakePerItemCollectionLock

func TakePerItemCollectionLock[item SingleItem](ctx context.Context, lockType SentinelType, c Manager, additionalColKeys []Key, i item) (Sentinel, error)

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

type SentinelManager

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

SentinelManager is the interface for managing cache sentinels to implement concurrency handling

type SentinelType

type SentinelType string

SentinelType captures the type of the sentinel for different operations

const (
	Create SentinelType = "create"
	Update SentinelType = "update"
	Delete SentinelType = "delete"
	Read   SentinelType = "read"
)

SentinelType names

type SingleItem

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

SingleItem 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 TTLProvider

type TTLProvider interface {
	TTL(id KeyTTLID) time.Duration
}

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

type WriteThroughCacheSentinelManager

type WriteThroughCacheSentinelManager struct {
}

WriteThroughCacheSentinelManager is the implementation of sentinel management for a write through cache

func NewWriteThroughCacheSentinelManager

func NewWriteThroughCacheSentinelManager() *WriteThroughCacheSentinelManager

NewWriteThroughCacheSentinelManager creates a new BaseCacheSentinelManager

func (*WriteThroughCacheSentinelManager) CanAlwaysSetSentinel

func (c *WriteThroughCacheSentinelManager) CanAlwaysSetSentinel(newVal Sentinel) bool

CanAlwaysSetSentinel returns true if the sentinel can be set without reading current value

func (*WriteThroughCacheSentinelManager) CanSetSentinelGivenCurrVal

func (c *WriteThroughCacheSentinelManager) CanSetSentinelGivenCurrVal(currVal Sentinel, newVal Sentinel) bool

CanSetSentinelGivenCurrVal returns true if new sentinel can be set for the given current sentinel

func (*WriteThroughCacheSentinelManager) CanSetValue

func (c *WriteThroughCacheSentinelManager) CanSetValue(currVal string, val string, sentinel Sentinel) (set bool, clear bool, conflict bool, refresh bool)

CanSetValue returns operation to take given existing key value, new value, and sentinel for the operation

func (*WriteThroughCacheSentinelManager) GenerateSentinel

func (c *WriteThroughCacheSentinelManager) GenerateSentinel(stype SentinelType) Sentinel

GenerateSentinel generates a sentinel value for the given sentinel type

func (*WriteThroughCacheSentinelManager) IsDeleteSentinelPrefix

func (c *WriteThroughCacheSentinelManager) IsDeleteSentinelPrefix(v Sentinel) bool

IsDeleteSentinelPrefix returns true if the sentinel value is a delete sentinel

func (*WriteThroughCacheSentinelManager) IsInvalidatingSentinelValue

func (c *WriteThroughCacheSentinelManager) IsInvalidatingSentinelValue(v Sentinel) bool

IsInvalidatingSentinelValue returns true if the sentinel requires invalidating the value across other

func (*WriteThroughCacheSentinelManager) IsReadSentinelPrefix

func (c *WriteThroughCacheSentinelManager) IsReadSentinelPrefix(v Sentinel) bool

IsReadSentinelPrefix returns true if the sentinel value is a read sentinel

func (*WriteThroughCacheSentinelManager) IsSentinelValue

func (c *WriteThroughCacheSentinelManager) IsSentinelValue(v string) bool

IsSentinelValue returns true if the value passed in is a sentinel value

func (*WriteThroughCacheSentinelManager) IsWriteSentinelPrefix

func (c *WriteThroughCacheSentinelManager) IsWriteSentinelPrefix(v Sentinel) bool

IsWriteSentinelPrefix returns true if the sentinel value is a write sentinel

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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