AVS Cache

How AVS Cache Works

The AVS Cache component within the AVS-SDK is designed to provide efficient caching solutions for your application. By storing key-value pairs with optional time-to-live (TTL) settings, AVS Cache helps optimize data retrieval and reduce redundant processing. This guide outlines the AVS Cache setup and functionality, enhancing the performance of your applications.


Importing and Using AVS Cache

To incorporate AVS Cache into your application, begin by importing it from the AVS-SDK. Here’s how to do this:

const AVSCache = require('avs-sdk').AVSCache; // Import AVSCache from avs-sdk

After importing, create an instance of AVSCache to start caching your data effectively.


AVS Cache: Functions and Usage Overview

The AVS Cache module provides essential functions for storing, retrieving, and managing cached data. Here’s a breakdown of the available functions and how they can be utilized.


Available Functions

Constructor - AVSCache(maxSize)

  • Purpose: Initializes an instance of AVSCache with a specified maximum size for the cache.

  • Parameters:

    • maxSize: (optional) Maximum number of items the cache can hold. Default is 1000.

  • Usage: Create an instance to start caching data with a defined limit.

  • Example:

    const cache = new AVSCache(500); // Initializes cache with a max size of 500

Set Item - set(key, value, ttl)

  • Purpose: Stores a key-value pair in the cache with an optional TTL (time-to-live).

  • Parameters:

    • key: The unique identifier for the cache item.

    • value: The value associated with the key.

    • ttl: (optional) Time in seconds before the item expires. Default is 3600 seconds (1 hour).

  • Usage: Call set to add items to the cache.

  • Example:

  • What to Expect: If the cache exceeds its maximum size, the oldest item is removed to make room for the new entry.


Get Item - get(key)

  • Purpose: Retrieves the value associated with the specified key from the cache.

  • Parameters:

    • key: The unique identifier for the cached item.

  • Usage: Call get to retrieve cached data.

  • Example:

  • What to Expect: Returns the value if it exists and is not expired; otherwise, it returns null.


Clear Cache - clear()

  • Purpose: Clears all items from the cache.

  • Usage: Call clear to remove all cached data.

  • Example:

  • What to Expect: All cached entries will be removed, freeing up memory.


Putting It All Together

With AVS Cache, you can efficiently manage cached data, ensuring quick access to frequently used information while controlling memory usage. Here’s a full example demonstrating the usage of AVS Cache functions:

Summary of Functionality

  • AVSCache(maxSize): Initializes an AVSCache instance with an optional maximum size for cached items.

  • set(key, value, ttl): Stores a key-value pair in the cache with an optional TTL, automatically managing the cache size.

  • get(key): Retrieves the value associated with the specified key, returning null if the item does not exist or has expired.

  • clear(): Clears all items from the cache, freeing up memory.

The AVS Cache module enhances your application’s efficiency by enabling fast data access and minimizing redundant processing. Use it to optimize performance while managing memory effectively.

Last updated