Skip to main content

provider.go

provider.go - Overview

  1. Overview This file implements a Redis-based cache provider for the SigNoz application. It handles connecting to a Redis server, storing, retrieving, and managing cached data, including setting TTLs and removing entries. It also provides methods for bulk removal, pinging the server, and retrieving keys.

  2. Detailed Documentation

type provider

  • Purpose: Represents the Redis cache provider.
  • Members:
    • client: A pointer to the Redis client (*redis.Client).
    • opts: cache.Redis struct containing the Redis configuration options.

func NewFactory() factory.ProviderFactory[cache.Cache, cache.Config]

  • Purpose: Creates a new factory for the Redis cache provider.
  • Returns: A factory.ProviderFactory that can be used to create new Redis cache providers.

func New(ctx context.Context, settings factory.ProviderSettings, config cache.Config) (cache.Cache, error)

  • Purpose: Creates a new Redis cache provider.
  • Parameters:
    • ctx: The context (context.Context).
    • settings: Factory settings (factory.ProviderSettings).
    • config: The cache configuration (cache.Config).
  • Returns: A cache.Cache interface implemented by the provider struct, and an error if creation fails.

func WithClient(client *redis.Client) *provider

  • Purpose: Creates a new cache provider with a pre-existing Redis client.
  • Parameters:
    • client: A pointer to the Redis client (*redis.Client).
  • Returns: A pointer to a new provider instance.

func (c *provider) Connect(_ context.Context) error

  • Purpose: Connects to the Redis server using the configuration options.
  • Parameters:
    • _: The context (context.Context).
  • Returns: An error if the connection fails, otherwise nil.

func (c *provider) Store(ctx context.Context, cacheKey string, data cache.CacheableEntity, ttl time.Duration) error

  • Purpose: Stores data in the Redis cache with a specified TTL (time-to-live).
  • Parameters:
    • ctx: The context (context.Context).
    • cacheKey: The key under which the data will be stored (string).
    • data: The data to be stored, which must implement the cache.CacheableEntity interface.
    • ttl: The time-to-live for the cached data (time.Duration).
  • Returns: An error if the store operation fails, otherwise nil.

func (c *provider) Retrieve(ctx context.Context, cacheKey string, dest cache.CacheableEntity, allowExpired bool) (cache.RetrieveStatus, error)

  • Purpose: Retrieves data from the Redis cache.
  • Parameters:
    • ctx: The context (context.Context).
    • cacheKey: The key of the data to retrieve (string).
    • dest: A pointer to a variable that implements the cache.CacheableEntity interface, where the retrieved data will be stored.
    • allowExpired: A boolean indicating whether to allow retrieval of expired data (purpose unclear from the code).
  • Returns:
    • cache.RetrieveStatus: An enum indicating the status of the retrieval (Hit, Miss, Error).
    • An error if the retrieve operation fails (e.g., connection error), otherwise nil.

func (c *provider) SetTTL(ctx context.Context, cacheKey string, ttl time.Duration)

  • Purpose: Sets the TTL for a specific cache entry.
  • Parameters:
    • ctx: The context (context.Context).
    • cacheKey: The key of the cache entry to update (string).
    • ttl: The new time-to-live for the cache entry (time.Duration).
  • Returns: None. Logs an error if setting the TTL fails.

func (c *provider) Remove(ctx context.Context, cacheKey string)

  • Purpose: Removes a specific cache entry from the Redis cache.
  • Parameters:
    • ctx: The context (context.Context).
    • cacheKey: The key of the cache entry to remove (string).
  • Returns: None.

func (c *provider) BulkRemove(ctx context.Context, cacheKeys []string)

  • Purpose: Removes multiple cache entries from the Redis cache in a single operation.
  • Parameters:
    • ctx: The context (context.Context).
    • cacheKeys: A slice of strings representing the keys of the cache entries to remove.
  • Returns: None. Logs an error if the deletion fails.

func (c *provider) Close(_ context.Context) error

  • Purpose: Closes the connection to the Redis server.
  • Parameters:
    • _: The context (context.Context).
  • Returns: An error if closing the connection fails, otherwise nil.

func (c *provider) Ping(ctx context.Context) error

  • Purpose: Pings the Redis server to check the connection.
  • Parameters:
    • ctx: The context (context.Context).
  • Returns: An error if the ping fails, otherwise nil.

func (c *provider) GetClient() *redis.Client

  • Purpose: Returns the underlying Redis client.
  • Returns: A pointer to the redis.Client.

func (c *provider) GetTTL(ctx context.Context, cacheKey string) time.Duration

  • Purpose: Retrieves the TTL for a specific cache entry.
  • Parameters:
    • ctx: The context (context.Context).
    • cacheKey: The key of the cache entry (string).
  • Returns: The TTL for the cache entry as a time.Duration. Logs an error if retrieving the TTL fails.

func (c *provider) GetKeys(ctx context.Context, pattern string) ([]string, error)

  • Purpose: Retrieves keys matching a given pattern.
  • Parameters:
    • ctx: The context (context.Context).
    • pattern: The pattern to match keys against (string).
  • Returns: A slice of strings representing the matching keys, and an error if the operation fails.

func (c *provider) GetKeysWithTTL(ctx context.Context, pattern string) (map[string]time.Duration, error)

  • Purpose: Retrieves keys matching a given pattern along with their TTLs.
  • Parameters:
    • ctx: The context (context.Context).
    • pattern: The pattern to match keys against (string).
  • Returns: A map where the key is the cache key (string) and the value is the TTL (time.Duration), and an error if the operation fails.

Include in Getting Started: YES