# 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:

```javascript
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**:

  ```javascript
  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**:

  ```javascript
  cache.set('user:123', { name: 'John Doe' }, 600); // Stores user data for 10 minutes
  ```
* **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**:

  ```javascript
  const user = cache.get('user:123'); // Retrieves user data
  if (user) {
      console.log('User data:', user);
  } else {
      console.log('User not found or expired.');
  }
  ```
* **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**:

  ```javascript
  cache.clear(); // Clears all items from the cache
  ```
* **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:

```javascript
const AVSCache = require('avs-sdk').AVSCache;

const cache = new AVSCache(500); // Create a cache instance with a maximum size of 500

// Set cache items
cache.set('user:123', { name: 'John Doe' }, 600); // User data with 10 minutes TTL
cache.set('session:456', { token: 'abc123' }); // Session data with default TTL

// Get a cached item
const user = cache.get('user:123');
if (user) {
    console.log('User data:', user); // Outputs: User data: { name: 'John Doe' }
} else {
    console.log('User not found or expired.');
}

// Clear the cache
cache.clear(); // All cache items are removed
```

#### 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://shivanshs-organization-2.gitbook.io/avs-sdk/getting-started/avs-cache.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
