Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/devvali/cache-keeper
A simple and lightweight caching solution for your Discord bot.
https://github.com/devvali/cache-keeper
cache cache-storage caching discord-bot discord-js
Last synced: 4 months ago
JSON representation
A simple and lightweight caching solution for your Discord bot.
- Host: GitHub
- URL: https://github.com/devvali/cache-keeper
- Owner: DevVali
- License: mit
- Created: 2024-08-10T21:07:04.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-08-12T11:20:41.000Z (6 months ago)
- Last Synced: 2024-10-12T03:20:21.937Z (4 months ago)
- Topics: cache, cache-storage, caching, discord-bot, discord-js
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/cache-keeper
- Size: 23.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
[![GitHub release](https://img.shields.io/github/release/DevVali/cache-keeper.svg)](https://github.com/DevVali/cache-keeper/releases/latest)
[![npm](https://img.shields.io/npm/dt/cache-keeper.svg)](https://www.npmjs.com/package/cache-keeper)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/DevVali/cache-keeper?tab=MIT-1-ov-file#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):
```bash
npm install cache-keeper
```## Usage
> The term TTL refers to Time to Live.
### Creating a cache instance
```js
// 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```js
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```js
console.log(cache.get('userId')); // '12345'
console.log(cache.get('userNickname', 'DefaultNick')); // 'DefaultNick'
```### Other useful methods
- `has`: Checks if a key is cached
- `delete`: Removes an entry from the cache
- `clear`: Removes all entries from the cache
- `size`: Returns the current number of entries in the cache### Advanced usage
- `getExpirationTime`: Returns the expiration time of an entry
- `clearExpired`: Removes all expired entries from the cache### TypeScript support
`cache-keeper` provides robust TypeScript support for type safety and code maintainability.
```ts
// 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();// 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
- [`@discordjs/collection`](https://www.npmjs.com/package/@discordjs/collection) (Dependency)
- [Repo template](https://github.com/bit-js/library)
- [Discord bot template](https://github.com/devvali/djs-template)## Issues
If you encounter any issues or have suggestions, please [open an issue](https://github.com/DevVali/cache-keeper/issues).