The N-Way Set Associative Cache is a high-performance caching library implemented in TypeScript. It provides efficient in-memory caching with customizable eviction policies, making it suitable for applications that require controlled cache behavior. The cache is designed to optimize lookups and store frequently accessed data while ensuring flexible eviction strategies.
Install the package using npm or yarn:
# Using npm
npm install n-way-set-associative-cache
# Using yarn
yarn add n-way-set-associative-cache
Below is an example of how to use the N-Way Set Associative Cache in a Node.js project:
import { NWaySetAssociativeCache } from 'n-way-set-associative-cache';
// Create a cache with 4 sets and 2 items per set with LRU policy as default
const cache = new NWaySetAssociativeCache<string, number>(4, 2);
// Insert values into the cache
cache.set('key1', 100);
cache.set('key2', 200);
// Retrieve values from the cache
console.log(cache.get('key1')); // Output: 100
// Check if a key exists
console.log(cache.has('key2')); // Output: true
// Remove an entry
cache.delete('key1');
new NWaySetAssociativeCache<K, V>( capacity: number, ways: number)
-
capacity
: Total number of items cache can hold. -
ways
: The number of items in each set. (Total sets = capacity / ways, capacity must be a multiple of the number of ways)
-
put(key: K, value: V): void
- Adds a key-value pair to the cache. -
get(key: K): V | undefined
- Retrieves a value by key. -
has(key: K): boolean
- Checks if the key exists in the cache. -
delete(key: K): boolean
- Removes a key from the cache. -
clear(): void
- Clears all entries from the cache. -
listAll(): { key: K; value: V; }[]
- Lists all items in the cache in all sets
To build the project, ensure you have the latest versions of Node.js and npm installed.
npm run build
Run the test suite using the following command:
npm run test
We welcome contributions! To contribute:
- Fork the repository.
- Create a new branch for your feature or fix.
- Commit your changes with clear messages.
- Submit a pull request for review.
Before submitting, please ensure your code adheres to the project's coding standards and includes relevant tests.
- Initial release of N-Way Set Associative Cache.
- Implemented core caching functionality with configurable eviction policies.
- Provided a flexible API for insertion, retrieval, and eviction.
For a complete list of changes, see the CHANGELOG.
For more details, visit the project's repository.