Skip to content

Cache Management Utility

Easily manage browser cache data with this utility, leveraging the Cache API for storing, retrieving, updating, deleting, and clearing cached data. Includes support for expiration times and robust error handling.


Installation

Install the package using npm:

bash
npm install cache-manager-soumojit-shome

Features

  • Add data to cache with customizable expiration time.
  • Retrieve and validate cached data.
  • Update existing cached data.
  • Delete specific cached data.
  • Clear all cached data for a specific cache.

Functions

AddDataIntoCache

Adds data to the cache with an expiration time.

Parameters:

  • docId (string): Unique identifier for the document.
  • dataSet (object): Data to store in the cache.
  • expirationTime (number, optional): Expiration time in milliseconds (default: 30 minutes).
  • cacheName (string): Name of the cache.
  • baseUrl (string): Base URL for generating cache keys.

Returns:

  • { success: true } if the data was added successfully.
  • { success: false, message: string, error?: string } in case of failure.

Example:

javascript
await AddDataIntoCache(
  "doc123",
  { key: "value" },
  1000 * 60 * 10, // 10 minutes expiration
  "myCache",
  "https://example.com"
);

GetDataFromCache

Retrieves data from the cache and verifies if it is still valid.

Parameters:

  • docId (string): Unique identifier for the document.
  • cacheName (string): Name of the cache.
  • baseUrl (string): Base URL for generating cache keys.

Returns:

  • { success: true, data: object } if the data was retrieved successfully.
  • { success: false, message: string, error?: string } in case of failure or expiration.

Example:

javascript
const result = await GetDataFromCache("doc123", "myCache", "https://example.com");
if (result.success) {
  console.log("Cached Data:", result.data);
} else {
  console.error("Error fetching cache:", result.message);
}

UpdateDataInCache

Updates an existing cached document by merging new data.

Parameters:

  • docId (string): Unique identifier for the document.
  • updatedDataSet (object): Data to merge with the existing cached document.
  • cacheName (string): Name of the cache.
  • baseUrl (string): Base URL for generating cache keys.

Returns:

  • { success: true, data: object } if the update was successful.
  • { success: false, message: string, error?: string } in case of failure.

Example:

javascript
await UpdateDataInCache(
  "doc123",
  { newKey: "newValue" },
  "myCache",
  "https://example.com"
);

DeleteDataFromCache

Deletes a specific document from the cache.

Parameters:

  • docId (string): Unique identifier for the document.
  • cacheName (string): Name of the cache.
  • baseUrl (string): Base URL for generating cache keys.

Returns:

  • { success: true } if the document was deleted successfully.
  • { success: false, message: string, error?: string } in case of failure.

Example:

javascript
await DeleteDataFromCache("doc123", "myCache", "https://example.com");

ClearWholeCacheData

Clears all data from a specified cache.

Parameters:

  • cacheName (string): Name of the cache.

Returns:

  • { success: true } if the cache was cleared successfully.
  • { success: false, message: string, error?: string } in case of failure.

Example:

javascript
await ClearWholeCacheData("myCache");

Notes

  • The functions rely on the Cache API, which is not available in all environments (e.g., server-side Node.js).
  • Always handle the success and error properties appropriately in your application to ensure a smooth user experience.

Exported Functions

  • AddDataIntoCache
  • GetDataFromCache
  • UpdateDataInCache
  • DeleteDataFromCache
  • ClearWholeCacheData

Developer Details

Want to learn more about the developer or explore other projects? Check out these links: