Documentation ¶
Index ¶
- Variables
- func NewDefaultCacheFactory() *build.Factory
- type CacheEntry
- type ICache
- type MemoryCache
- func (c *MemoryCache[T]) Cleanup()
- func (c *MemoryCache[T]) Clear(ctx context.Context, correlationId string) error
- func (c *MemoryCache[T]) Configure(ctx context.Context, cfg *config.ConfigParams)
- func (c *MemoryCache[T]) Contains(ctx context.Context, correlationId string, key string) bool
- func (c *MemoryCache[T]) Remove(ctx context.Context, correlationId string, key string) error
- func (c *MemoryCache[T]) Retrieve(ctx context.Context, correlationId string, key string) (T, error)
- func (c *MemoryCache[T]) Store(ctx context.Context, correlationId string, key string, value T, timeout int64) (T, error)
- type NullCache
- func (c *NullCache[T]) Contains(ctx context.Context, correlationId string, key string) bool
- func (c *NullCache[T]) Remove(ctx context.Context, correlationId string, key string) error
- func (c *NullCache[T]) Retrieve(ctx context.Context, correlationId string, key string) (T, error)
- func (c *NullCache[T]) Store(ctx context.Context, correlationId string, key string, value T, timeout int64) (T, error)
Constants ¶
This section is empty.
Variables ¶
var MemoryCacheDescriptor = refer.NewDescriptor("pip-services", "cache", "memory", "*", "1.0")
var NullCacheDescriptor = refer.NewDescriptor("pip-services", "cache", "null", "*", "1.0")
Functions ¶
func NewDefaultCacheFactory ¶
NewDefaultCacheFactory create a new instance of the factory.
Returns: *build.Factory
Types ¶
type CacheEntry ¶
type CacheEntry[T any] struct { // contains filtered or unexported fields }
CacheEntry Data object to store cached values with their keys used by MemoryCache
func NewCacheEntry ¶
func NewCacheEntry[T any](key string, value T, timeout int64) *CacheEntry[T]
NewCacheEntry creates a new instance of the cache entry and assigns its values.
Parameters: - key string a unique key to locate the value. - value T a value to be stored. - timeout int64 expiration timeout in milliseconds. Returns *CacheEntry
func (*CacheEntry[T]) Expiration ¶
func (c *CacheEntry[T]) Expiration() time.Time
Expiration gets the expiration timeout.
Returns time.Time the expiration timeout in milliseconds.
func (*CacheEntry[T]) IsExpired ¶
func (c *CacheEntry[T]) IsExpired() bool
IsExpired checks if this value already expired.
Returns: bool true if the value already expires and false otherwise.
func (*CacheEntry[T]) Key ¶
func (c *CacheEntry[T]) Key() string
Key gets the key to locate the cached value.
Returns: string the value key.
func (*CacheEntry[T]) SetValue ¶
func (c *CacheEntry[T]) SetValue(value T, timeout int64)
SetValue a new value and extends its expiration.
Parameters: - value any a new cached value. - timeout int64 an expiration timeout in milliseconds.
func (*CacheEntry[T]) Value ¶
func (c *CacheEntry[T]) Value() T
Value gets the cached value.
Returns: any the value object.
type ICache ¶
type ICache[T any] interface { // Retrieve cached value from the cache using its key. // If value is missing in the cache or expired it returns nil. Retrieve(ctx context.Context, correlationId string, key string) (T, error) // Store value in the cache with expiration time. Store(ctx context.Context, correlationId string, key string, value T, timeout int64) (T, error) // Remove a value from the cache by its key. Remove(ctx context.Context, correlationId string, key string) error // Contains check is value stores Contains(ctx context.Context, correlationId string, key string) bool }
ICache interface for caches that are used to cache values to improve performance.
type MemoryCache ¶
type MemoryCache[T any] struct { // contains filtered or unexported fields }
MemoryCache that stores values in the process memory.
Configuration parameters: - options: - timeout: default caching timeout in milliseconds (default: 1 minute) - max_size: maximum number of values stored in this cache (default: 1000)
see ICache
Example: cache := NewMemoryCache[string](); res, err := cache.Store(contex.Background(), "123", "key1", "ABC", 10000);
func NewMemoryCache ¶
func NewMemoryCache[T any]() *MemoryCache[T]
NewMemoryCache creates a new instance of the cache. Returns: *MemoryCache
func NewMemoryCacheFromConfig ¶
func NewMemoryCacheFromConfig[T any](ctx context.Context, cfg *config.ConfigParams) *MemoryCache[T]
NewMemoryCacheFromConfig creates a new instance of the cache.
Parameters: cfg *config.ConfigParams configuration parameters to be set. Returns: *MemoryCache
func (*MemoryCache[T]) Cleanup ¶
func (c *MemoryCache[T]) Cleanup()
Cleanup memory cache, public thread save method
func (*MemoryCache[T]) Clear ¶
func (c *MemoryCache[T]) Clear(ctx context.Context, correlationId string) error
Clear a value from the cache.
Parameters: - ctx context.Context - correlationId string transaction id to trace execution through call chain.
func (*MemoryCache[T]) Configure ¶
func (c *MemoryCache[T]) Configure(ctx context.Context, cfg *config.ConfigParams)
Configure configures component by passing configuration parameters.
Parameters: config *config.ConfigParams configuration parameters to be set.
func (*MemoryCache[T]) Contains ¶
Contains check is value contains in cache and time not expire.
Parameters: - ctx context.Context - correlationId string transaction id to trace execution through call chain.\ - key string a unique value key. Returns: bool
func (*MemoryCache[T]) Remove ¶
Remove a value from the cache by its key.
Parameters: - ctx context.Context - correlationId string transaction id to trace execution through call chain.\ - key string a unique value key. Returns: error
func (*MemoryCache[T]) Retrieve ¶
Retrieve cached value from the cache using its key. If value is missing in the cache or expired it returns null.
Parameters: - ctx context.Context - correlationId string transaction id to trace execution through call chain. - key string a unique value key. Returns T, error
func (*MemoryCache[T]) Store ¶
func (c *MemoryCache[T]) Store(ctx context.Context, correlationId string, key string, value T, timeout int64) (T, error)
Store value in the cache with expiration time, if success return stored value.
Parameters: - ctx context.Context - correlationId string transaction id to trace execution through call chain. - key string a unique value key. - value T a value to store. - timeout int64 expiration timeout in milliseconds. Returns T, error
type NullCache ¶
type NullCache[T any] struct{}
NullCache Dummy cache implementation that doesn't do anything. It can be used in testing or in situations when cache is required but shall be disabled.
func NewNullCache ¶
NewNullCache creates a new instance of the cache. Returns: *NullCache
func (*NullCache[T]) Contains ¶
Contains check is value contains in cache and time not expire.
Parameters: - ctx context.Context - correlationId string transaction id to trace execution through call chain.\ - key string a unique value key. Returns: bool
func (*NullCache[T]) Remove ¶
Remove a value from the cache by its key.
Parameters: - ctx context.Context - correlationId string transaction id to trace execution through call chain.\ - key string a unique value key. Returns: error
func (*NullCache[T]) Retrieve ¶
Retrieve retrieves cached value from the cache using its key. If value is missing in the cache or expired it returns null.
Parameters: - ctx context.Context - correlationId string transaction id to trace execution through call chain. - key string a unique value key. Returns: T, error
func (*NullCache[T]) Store ¶
func (c *NullCache[T]) Store(ctx context.Context, correlationId string, key string, value T, timeout int64) (T, error)
Store value in the cache with expiration time, if success return stored value.
Parameters: - ctx context.Context - correlationId string transaction id to trace execution through call chain. - key string a unique value key. - value T a value to store. - timeout int64 expiration timeout in milliseconds. Returns T, error