Toolbox-XToolbox-X

Clear Cache

Manages the internal cache of search results by either clearing a specific cache entry or purging all cached data.

clearCache()

Manages the internal cache of search results by either clearing a specific cache entry or purging all cached data.

Signature

clearCache(key?: string): void

Parameters

ParameterTypeRequiredDescription
keystringOptionalThe specific cache key to remove. When omitted, clears all cached results.

Description

The clearCache method provides control over the Finder instance's caching mechanism. It allows you to:

  • Clear all cached search results (global cache clearance).
  • Remove a specific cache entry by its key (targeted clearance).
  • Maintain optimal memory usage by removing stale cache entries.

Cache management is particularly useful when:

  • The underlying dataset changes and cached results become invalid.
  • You need to free up memory by removing cached data.
  • Implementing time-sensitive data updates.

Example Usage

import { Finder } from 'toolbox-x';

const products = [
  { id: 1, name: "Laptop" },
  { id: 2, name: "Phone" }
];

const productFinder = new Finder(products);
// ... perform several searches ...

// Clear entire cache when products update
productFinder.clearCache();

Performance Notes

  • Clearing by key: $O(1)$ operation.
  • Clearing all: $O(n)$ operation (where $n$ is the number of cache entries).
  • Memory Impact: Immediately frees memory used by cleared cache entries.
  • Thread Safety: Cache operations are synchronous and thread-safe.

Best Practices

  1. Use specific cache keys when you need granular control over cache invalidation.
  2. Prefer targeted clearance over global clearance when possible.
  3. Consider automatic cache clearing when the underlying data changes.
  4. For long-running applications, implement periodic cache clearance.

Last updated: Mon, Jun 15, 2026 06:09:51AM (UTC)

On this page