cache.go
cache.go - Overview
-
Overview This file defines the interface for a generic cache, along with options, key generation, and functions to load cache configurations from YAML files. It supports different cache providers like Redis and in-memory.
-
Detailed Documentation
Options
- Purpose: Defines the configuration options for the cache.
- Parameters:
Name
(string): The name of the cache. This field is ignored during YAML unmarshaling.Provider
(string): The cache provider to use (e.g., "redis", "inmemory").Redis
(*redis.Options): Configuration options for the Redis cache provider.InMemory
(*inmemory.Options): Configuration options for the in-memory cache provider.
- Returns: None
Cache
(interface)
- Purpose: Defines the interface for interacting with the cache.
- Methods:
Connect() error
: Establishes a connection to the cache.- Returns:
error
: An error if the connection fails,nil
otherwise.
- Returns:
Store(cacheKey string, data []byte, ttl time.Duration) error
: Stores data in the cache with a given key and TTL (time-to-live).- Parameters:
cacheKey
(string): The key to store the data under.data
([]byte): The data to store.ttl
(time.Duration): The time-to-live for the data.
- Returns:
error
: An error if storing fails,nil
otherwise.
- Parameters:
Retrieve(cacheKey string, allowExpired bool) ([]byte, status.RetrieveStatus, error)
: Retrieves data from the cache for a given key.- Parameters:
cacheKey
(string): The key to retrieve data for.allowExpired
(bool): Indicates if expired data should be returned.
- Returns:
[]byte
: The retrieved data.status.RetrieveStatus
: The status of the retrieval operation.error
: An error if retrieval fails,nil
otherwise.
- Parameters:
SetTTL(cacheKey string, ttl time.Duration)
: Sets a new TTL for a given cache key.- Parameters:
cacheKey
(string): The key to update the TTL for.ttl
(time.Duration): The new time-to-live.
- Returns: None
- Parameters:
Remove(cacheKey string)
: Removes data from the cache for a given key.- Parameters:
cacheKey
(string): The key to remove.
- Returns: None
- Parameters:
BulkRemove(cacheKeys []string)
: Removes multiple entries from the cache.- Parameters:
cacheKeys
([]string): A list of cache keys to remove.
- Returns: None
- Parameters:
Close() error
: Closes the connection to the cache.- Returns:
error
: An error if closing fails,nil
otherwise.
- Returns:
KeyGenerator
(interface)
- Purpose: Defines the interface for generating cache keys.
- Methods:
GenerateKeys(*v3.QueryRangeParamsV3) map[string]string
: Generates cache keys for the given query range parameters.- Parameters:
*v3.QueryRangeParamsV3
: The query range parameters.
- Returns:
map[string]string
: A map where the key is the query name and the value is the cache key.
- Parameters:
LoadFromYAMLCacheConfig(yamlConfig []byte) (*Options, error)
- Purpose: Loads cache options from a YAML configuration provided as a byte slice.
- Parameters:
yamlConfig
([]byte): The YAML configuration as a byte slice.
- Returns:
*Options
: The loaded cache options.error
: An error if unmarshaling fails,nil
otherwise.
LoadFromYAMLCacheConfigFile(configFile string) (*Options, error)
- Purpose: Loads cache options from a YAML configuration file.
- Parameters:
configFile
(string): The path to the YAML configuration file.
- Returns:
*Options
: The loaded cache options.error
: An error if reading the file or unmarshaling fails,nil
otherwise.
NewCache(options *Options) Cache
- Purpose: Creates a new cache instance based on the provided options.
- Parameters:
options
(*Options): The cache options.
- Returns:
Cache
: A new cache instance, ornil
if the provider is not supported.
- Code Examples
Not applicable.
Include in Getting Started: NO