Skip to main content

cache.go

cache.go - Overview

  1. Overview

This file implements an in-memory cache using the go-cache library. It provides functionalities to store, retrieve, update, and remove data from the cache.

  1. Detailed Documentation

cache

  • Purpose: Represents the in-memory cache structure.
  • Fields:
    • cc: A pointer to a go_cache.Cache object, which is the underlying in-memory cache.

New

  • Purpose: Creates a new in-memory cache instance.
  • Parameters:
    • opts: A pointer to an Options struct, which configures the cache's TTL and cleanup interval. If opts is nil, it uses default options.
  • Returns: A pointer to a new cache object.

Connect

  • Purpose: A placeholder function; it performs no operation as the in-memory cache does not require a connection.
  • Parameters: None
  • Returns: nil

Store

  • Purpose: Stores data in the cache with a given key and TTL (time-to-live).
  • Parameters:
    • cacheKey: The key under which the data will be stored (string).
    • data: The data to be stored (byte slice).
    • ttl: The time duration for which the data will be stored in the cache (time.Duration).
  • Returns: nil

Retrieve

  • Purpose: Retrieves data from the cache based on the given key.
  • Parameters:
    • cacheKey: The key of the data to retrieve (string).
    • allowExpired: A boolean indicating whether to return expired data (bool). This parameter is not used in the implementation.
  • Returns:
    • []byte: The data retrieved from the cache. Returns nil if the key is not found.
    • status.RetrieveStatus: The status of the retrieval operation. status.RetrieveStatusHit if the key is found, status.RetrieveStatusKeyMiss if not.
    • error: Returns nil.

SetTTL

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

Remove

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

BulkRemove

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

Close

  • Purpose: A placeholder function; it performs no operation as the in-memory cache does not require closing.
  • Parameters: None
  • Returns: nil

Configuration

  • Purpose: Returns the cache configuration.
  • Parameters: None
  • Returns: A pointer to an Options struct representing the cache configuration. Returns nil.
  1. Code Examples

Not applicable as the code is self-explanatory.

Include in Getting Started: NO