cache-keeper
v1.1.0
Published
A simple and lightweight caching solution for your Discord bot.
Downloads
13
Maintainers
Readme
Cache Keeper
A simple and lightweight caching solution for your Discord bot.
Installation
To get started, install the library using npm (or your other favorite package manager):
npm install cache-keeper
Usage
The term TTL refers to Time to Live.
Creating a cache instance
// Create a cache instance with a default TTL of 10 minutes
const cache = new Keeper();
// Create a cache instance with a custom default TTL of 30 minutes
const longCache = new Keeper(1800000);
Setting values
- Use the
set
method to store a key-value pair in the cache - You can optionally specify a custom TTL for an entry that overrides the default
cache.set('userId', '12345');
// Set with a custom TTL of 5 minutes
cache.set('username', 'AwesomeUser', 300000);
Retrieving values
- Use the
get
method to retrieve a value from the cache - You can optionally specify a default value to return if the entry is not set
console.log(cache.get('userId')); // '12345'
console.log(cache.get('userNickname', 'DefaultNick')); // 'DefaultNick'
Other useful methods
has
: Checks if a key is cacheddelete
: Removes an entry from the cacheclear
: Removes all entries from the cachesize
: Returns the current number of entries in the cache
Advanced usage
getExpirationTime
: Returns the expiration time of an entryclearExpired
: Removes all expired entries from the cache
TypeScript support
cache-keeper
provides robust TypeScript support for type safety and code maintainability.
// Define a type for the cache key
type UserId = string;
// Define an interface for the cache value
interface UserData {
username: string;
userNickname: string;
createdTimestamp: number;
}
// Create a cache instance using generic types for key and value types
const userCache = new Keeper<UserId, UserData>();
// Store user data in the cache
userCache.set('12345', {
username: 'AwesomeUser',
userNickname: 'DefaultNick',
createdTimestamp: 1723320168561,
});
// Retrieve user data from the cache
if (userCache.has('12345')) {
const userData = userCache.get('12345')!;
console.log(userData.username); // TypeScript knows the shape of userData
}
Resources
Issues
If you encounter any issues or have suggestions, please open an issue.