Skip to main content

redis.go

redis.go - Overview

  1. Overview

This file implements a Redis-based caching solution, providing functionalities to connect to a Redis server, store, retrieve, and remove data, manage TTLs, and retrieve keys.

  1. Detailed Documentation

cache

  • Purpose: Struct that holds the redis client and options.
  • Fields:
    • client: A pointer to the redis client. Type: *redis.Client
    • opts: A pointer to the options for the redis cache. Type: *Options

New

  • Purpose: Creates a new cache instance with the given options. If no options are provided, default options are used.
  • Parameters:
    • opts: A pointer to the options for the redis cache. Type: *Options
  • Returns: A pointer to a new cache instance. Type: *cache

WithClient

  • Purpose: Creates a new cache instance with a pre-existing Redis client.
  • Parameters:
    • client: A pointer to an existing redis client. Type: *redis.Client
  • Returns: A pointer to a new cache instance. Type: *cache

Connect

  • Purpose: Establishes a connection to the Redis server using the options provided.
  • Parameters: None
  • Returns: An error if the connection fails, otherwise nil. Type: error

Store

  • Purpose: Stores data in the Redis cache with a given key and TTL (Time To Live).
  • Parameters:
    • cacheKey: The key to store the data under. Type: string
    • data: The data to be stored as a byte array. Type: []byte
    • ttl: The time duration for which the data should be cached. Type: time.Duration
  • Returns: An error if storing the data fails, otherwise nil. Type: error

Retrieve

  • Purpose: Retrieves data from the Redis cache based on the provided key.
  • Parameters:
    • cacheKey: The key to retrieve the data for. Type: string
    • allowExpired: Not used in the code.
  • Returns:
    • []byte: The retrieved data as a byte array. Returns nil if the key is not found or an error occurs.
    • status.RetrieveStatus: The status of the retrieval operation.
    • error: An error if the retrieval fails (e.g., connection error), otherwise nil.

SetTTL

  • Purpose: Sets the TTL for an existing cache entry.
  • Parameters:
    • cacheKey: The key of the cache entry to update. Type: string
    • ttl: The new TTL for the cache entry. Type: time.Duration
  • Returns: None

Remove

  • Purpose: Removes a single cache entry from Redis.
  • Parameters:
    • cacheKey: The key of the cache entry to remove. Type: string
  • Returns: None

BulkRemove

  • Purpose: Removes multiple cache entries from Redis.
  • Parameters:
    • cacheKeys: A slice of keys representing the cache entries to remove. Type: []string
  • Returns: None

Close

  • Purpose: Closes the connection to the Redis server.
  • Parameters: None
  • Returns: An error if closing the connection fails, otherwise nil. Type: error

Ping

  • Purpose: Pings the Redis server to check the connection.
  • Parameters: None
  • Returns: An error if the ping fails, otherwise nil. Type: error

GetClient

  • Purpose: Returns the Redis client instance.
  • Parameters: None
  • Returns: The Redis client. Type: *redis.Client

GetOptions

  • Purpose: Returns the options used to configure the Redis cache.
  • Parameters: None
  • Returns: The options for the redis cache. Type: *Options

GetTTL

  • Purpose: Retrieves the Time To Live (TTL) for a given cache key.
  • Parameters:
    • cacheKey: The key of the cache entry. Type: string
  • Returns: The TTL for the key. Type: time.Duration

GetKeys

  • Purpose: Retrieves keys matching a given pattern from the Redis cache.
  • Parameters:
    • pattern: The pattern to match keys against. Type: string
  • Returns:
    • []string: A slice of keys that match the pattern.
    • error: An error, if any occurred.

GetKeysWithTTL

  • Purpose: Retrieves keys matching a given pattern along with their TTLs.
  • Parameters:
    • pattern: The pattern to match keys against. Type: string
  • Returns:
    • map[string]time.Duration: A map of keys to their TTLs.
    • error: An error, if any occurred.

Include in Getting Started: NO