A simple, lightweight, and easy-to-use cache library for TypeScript and JavaScript projects.
npm install @wxn0brp/ac
import { AnotherCache } from "@wxn0brp/ac";
// Create a new cache with default options
const cache = new AnotherCache<string, string>();
// Set a value with a default TTL
cache.set("key", "value");
// Set a value with a custom TTL (in milliseconds)
cache.set("key2", "value2", 10000); // 10 seconds
// Get a value
const value = cache.get("key");
// Check if a key exists
const hasKey = cache.has("key");
// Delete a key
cache.delete("key");
// Get the cache size
const size = cache.size();
// Clear the cache
cache.clear();
You can configure the cache with the following options:
ttl: The default time-to-live for cache entries in milliseconds. Defaults to 5 minutes.maxSize: The maximum number of entries in the cache. When the cache reaches this size, the oldest entries will be removed. Defaults to no limit.cleanupInterval: The interval in milliseconds at which the cache will be cleaned up. Defaults to 15 minutes.store: A HardStore instance for persistent storage (see below).storeTime: Interval in milliseconds for auto-saving to the store. Defaults to 5 minutes.storeAutoMode: Enables automatic save/load from the store. Defaults to true.import { AnotherCache } from "@wxn0brp/ac";
const cache = new AnotherCache({
ttl: 10 * 60 * 1000, // 10 minutes
maxSize: 100,
cleanupInterval: 5 * 60 * 1000, // 5 minutes
});
The cache can persist data to disk or localStorage using the HardStore interface.
interface HardStore {
save(data: Object): void;
read(): Object;
}
import { AnotherCache, FSStore } from "@wxn0brp/ac";
const cache = new AnotherCache({
store: new FSStore("./cache.json"),
});
import { AnotherCache, LocalStorageHardStore } from "@wxn0brp/ac";
const cache = new AnotherCache({
store: new LocalStorageHardStore("my-cache"),
});
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request.