Documentation
¶
Index ¶
- type RedisCache
- func (c *RedisCache[T]) Close(ctx context.Context) error
- func (c *RedisCache[T]) Configure(ctx context.Context, config *cconf.ConfigParams)
- func (c *RedisCache[T]) Contains(ctx context.Context, key string) bool
- func (c *RedisCache[T]) IsOpen() bool
- func (c *RedisCache[T]) Open(ctx context.Context) error
- func (c *RedisCache[T]) Remove(ctx context.Context, key string) error
- func (c *RedisCache[T]) Retrieve(ctx context.Context, key string) (value T, err error)
- func (c *RedisCache[T]) SetReferences(ctx context.Context, references cref.IReferences)
- func (c *RedisCache[T]) Store(ctx context.Context, key string, value T, timeout int64) (result T, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RedisCache ¶
type RedisCache[T any] struct { // contains filtered or unexported fields }
Distributed cache that stores values in Redis in-memory database.
Configuration parameters:
- connection(s):
- discovery_key: (optional) a key to retrieve the connection from IDiscovery
- host: host name or IP address
- port: port number
- uri: resource URI or connection string with all parameters in it
- credential(s):
- store_key: key to retrieve parameters from credential store
- username: user name (currently is not used)
- password: user password
- options:
- retries: number of retries (default: 3)
- timeout: default caching timeout in milliseconds (default: 1 minute)
- db_num: database number in Redis (default 0)
- max_size: maximum number of values stored in this cache (default: 1000)
- cluster: enable redis cluster
References:
- *:discovery:*:*:1.0 (optional) IDiscovery services to resolve connection - *:credential-store:*:*:1.0 (optional) Credential stores to resolve credential
Example:
ctx := context.Background() cache = NewRedisCache[string](); cache.Configure(ctx, cconf.NewConfigParamsFromTuples( "host", "localhost", "port", 6379, )); err = cache.Open(ctx, "123") ... ret, err := cache.Store(ctx, "123", "key1", []byte("ABC")) if err != nil { ... } res, err := cache.Retrieve(ctx, "123", "key1") value, _ := res.([]byte) fmt.Println(string(value)) // Result: "ABC"
func NewRedisCache ¶
func NewRedisCache[T any]() *RedisCache[T]
NewRedisCache method are creates a new instance of this cache.
func (*RedisCache[T]) Close ¶
func (c *RedisCache[T]) Close(ctx context.Context) error
Close method are closes component and frees used resources. Parameters:
- ctx context.Context transaction id to trace execution through call chain.
Retruns: error or nil no errors occured.
func (*RedisCache[T]) Configure ¶
func (c *RedisCache[T]) Configure(ctx context.Context, config *cconf.ConfigParams)
Configure method are configures component by passing configuration parameters.
- ctx context.Context
- config configuration parameters to be set.
func (*RedisCache[T]) Contains ¶
func (c *RedisCache[T]) Contains(ctx context.Context, key string) bool
Contains check is value stores Parameters:
- ctx context.Context transaction id to trace execution through call chain.
- key a unique value key.
func (*RedisCache[T]) IsOpen ¶
func (c *RedisCache[T]) IsOpen() bool
Checks if the component is opened. Returns true if the component has been opened and false otherwise.
func (*RedisCache[T]) Open ¶
func (c *RedisCache[T]) Open(ctx context.Context) error
Open method are opens the component. Parameters:
- ctx context.Context transaction id to trace execution through call chain.
Returns: error or nil no errors occured.
func (*RedisCache[T]) Remove ¶
func (c *RedisCache[T]) Remove(ctx context.Context, key string) error
Removes a value from the cache by its key. Parameters:
- ctx context.Context transaction id to trace execution through call chain.
- key a unique value key.
Returns: error or nil for success
func (*RedisCache[T]) Retrieve ¶
func (c *RedisCache[T]) Retrieve(ctx context.Context, key string) (value T, err error)
Retrieve method are retrieves cached value from the cache using its key. If value is missing in the cache or expired it returns nil. Parameters:
- ctx context.Context transaction id to trace execution through call chain.
- key a unique value key. Retruns: cached value or error.
func (*RedisCache[T]) SetReferences ¶
func (c *RedisCache[T]) SetReferences(ctx context.Context, references cref.IReferences)
Sets references to dependent components.
- ctx context.Context
- references references to locate the component dependencies.
func (*RedisCache[T]) Store ¶
func (c *RedisCache[T]) Store(ctx context.Context, key string, value T, timeout int64) (result T, err error)
Store method are stores value in the cache with expiration time. Parameters:
- ctx context.Context transaction id to trace execution through call chain.
- key a unique value key.
- value a value to store.
- timeout expiration timeout in milliseconds.
Retruns error or nil for success