cache

package
Version: v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 6 Imported by: 3

Documentation

Overview

Abstract implementation of various distributed caches. We can save an object to cache and retrieve it object by its key, using various implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDefaultCacheFactory

func NewDefaultCacheFactory() *build.Factory

Create a new instance of the factory. Returns *build.Factory

Types

type CacheEntry

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

Data object to store cached values with their keys used by MemoryCache

func NewCacheEntry

func NewCacheEntry(key string, value interface{}, timeout int64) *CacheEntry

Creates a new instance of the cache entry and assigns its values. Parameters:

  • key string a unique key to locate the value.
  • value interface{] a value to be stored.
  • timeout int64 expiration timeout in milliseconds.

Returns *CacheEntry

func (*CacheEntry) Expiration

func (c *CacheEntry) Expiration() time.Time

Gets the expiration timeout. Returns time.Time the expiration timeout in milliseconds.

func (*CacheEntry) IsExpired

func (c *CacheEntry) IsExpired() bool

Checks if this value already expired. Returns bool true if the value already expires and false otherwise.

func (*CacheEntry) Key

func (c *CacheEntry) Key() string

Gets the key to locate the cached value. Returns string the value key.

func (*CacheEntry) SetValue

func (c *CacheEntry) SetValue(value interface{}, timeout int64)

Sets a new value and extends its expiration. Parameters:

  • value interface{} a new cached value.
  • timeout int64 a expiration timeout in milliseconds.

func (*CacheEntry) Value

func (c *CacheEntry) Value() interface{}

Gets the cached value. Returns interface{} the value object.

type ICache

type ICache interface {
	// Retrieves cached value from the cache using its key. If value is missing in the cache or expired it returns nil.
	Retrieve(correlationId string, key string) (interface{}, error)
	// Retrieves cached value from the cache using its key into reference object. If value is missing in the cache or expired it returns false.
	RetrieveAs(correlationId string, key string, result interface{}) (interface{}, error)
	// Stores value in the cache with expiration time.
	Store(correlationId string, key string, value interface{}, timeout int64) (interface{}, error)
	// Removes a value from the cache by its key.
	Remove(correlationId string, key string) error
}

Interface for caches that are used to cache values to improve performance.

type MemoryCache

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

Cache that stores values in the process memory.

Remember: This implementation is not suitable for synchronization of distributed processes.

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();
res, err := cache.Store("123", "key1", "ABC", 10000);

func NewMemoryCache

func NewMemoryCache() *MemoryCache

Creates a new instance of the cache. Returns *MemoryCache

func NewMemoryCacheFromConfig

func NewMemoryCacheFromConfig(cfg *config.ConfigParams) *MemoryCache

Creates a new instance of the cache. Parameters

  • cfg *config.ConfigParams configuration parameters to be set.

Returns *MemoryCache

func (*MemoryCache) Cleanup

func (c *MemoryCache) Cleanup()

Cleanup memory cache

func (*MemoryCache) Clear

func (c *MemoryCache) Clear(correlationId string) error

Clear a value from the cache. Parameters:

  • correlationId string transaction id to trace execution through call chain.

func (*MemoryCache) Configure

func (c *MemoryCache) Configure(cfg *config.ConfigParams)

Configures component by passing configuration parameters. Parameters:

  • config *config.ConfigParams configuration parameters to be set.

func (*MemoryCache) Remove

func (c *MemoryCache) Remove(correlationId string, key string) error

Removes a value from the cache by its key. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • key string a unique value key.

Returns error

func (*MemoryCache) Retrieve

func (c *MemoryCache) Retrieve(correlationId string, key string) (interface{}, error)

Retrieves cached value from the cache using its key. If value is missing in the cache or expired it returns null. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • key string a unique value key.

Returns interface{}, error

func (*MemoryCache) RetrieveAs added in v1.0.6

func (c *MemoryCache) RetrieveAs(correlationId string, key string, result interface{}) (interface{}, error)

Retrive cached value from the cache using its key and restore into reference object. If value is missing in the cache or expired it returns false. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • key string a unique value key.
  • result pointer to object for restore

Returns bool, error

func (*MemoryCache) Store

func (c *MemoryCache) Store(correlationId string, key string, value interface{}, timeout int64) (interface{}, error)

Stores value in the cache with expiration time, if success return stored value. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • key string a unique value key.
  • value interface{} a value to store.
  • timeout int64 expiration timeout in milliseconds.

Returns interface{}, error

type NullCache

type NullCache struct{}

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

func NewNullCache() *NullCache

Creates a new instance of the cache. Returns *NullCache

func (*NullCache) Remove

func (c *NullCache) Remove(correlationId string, key string) error

Removes a value from the cache by its key. Parameters:

  • correlationId string transaction id to trace execution through call chain.\
  • key string a unique value key.

Returns error

func (*NullCache) Retrieve

func (c *NullCache) Retrieve(correlationId string, key string) (interface{}, error)

Retrieves cached value from the cache using its key. If value is missing in the cache or expired it returns null. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • key string a unique value key.

Returns interface{}, error

func (*NullCache) RetrieveAs added in v1.0.6

func (c *NullCache) RetrieveAs(correlationId string, key string, result interface{}) (interface{}, error)

Retrieve cached value from the cache using its key and restore into reference object. If value is missing in the cache or expired it returns false. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • key string a unique value key.
  • refObj pointer to object for restore

Returns bool, error

func (*NullCache) Store

func (c *NullCache) Store(correlationId string, key string, value interface{}, timeout int64) (interface{}, error)

Stores value in the cache with expiration time, if success return stored value. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • key string a unique value key.
  • value interface{} a value to store.
  • timeout int64 expiration timeout in milliseconds.

Returns interface{}, error

Jump to

Keyboard shortcuts

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