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): voidParameters
| Parameter | Type | Required | Description |
|---|---|---|---|
key | string | Optional | The 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
- Use specific cache keys when you need granular control over cache invalidation.
- Prefer targeted clearance over global clearance when possible.
- Consider automatic cache clearing when the underlying data changes.
- For long-running applications, implement periodic cache clearance.
Last updated: Mon, Jun 15, 2026 06:09:51AM (UTC)
Binary Search
Performs a binary search on a sorted array using a custom key selector function.
Unit
A comprehensive unit conversion utility supporting conversions between various measurement systems including length, mass, temperature, volume, area, speed, time, digital storage, power, energy, pressure, and frequency.
