Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ksamirdev/bun-cache
Simple and fast bun caching with Bun's SQLite
https://github.com/ksamirdev/bun-cache
Last synced: 3 months ago
JSON representation
Simple and fast bun caching with Bun's SQLite
- Host: GitHub
- URL: https://github.com/ksamirdev/bun-cache
- Owner: ksamirdev
- Created: 2023-10-14T16:56:08.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-14T14:51:48.000Z (3 months ago)
- Last Synced: 2024-08-14T16:25:12.429Z (3 months ago)
- Language: TypeScript
- Homepage:
- Size: 24.4 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Bun Cache
Bun Cache is a caching library for Bun apps that harnesses the power of the Bun's SQLite to offer a straightforward and efficient caching solution.
## Installation 📦
To get Bun Cache up and running, you can easily install it using bun cli:
```bash
bun add @samocodes/bun-cache
```## Usage 🚀
To leverage Bun Cache, simply create a new instance of the `BunCache` class and start using its methods:
```typescript
import { BunCache } from "@samocodes/bun-cache";const cache = new BunCache(); // new BunCache(true) for persistance
cache.put("my-key", "my-value", 1000); // Store a value with a 1-second TTL
const value = cache.get("my-key");console.log(value); // 🌟 "my-value"
```## API 🧰
### `BunCache` Class
- `persistance`: The persistance mode for the cache. If set to `true`, the cache will be stored in the database and will persist across app restarts. If set to `false`, the cache will be stored in memory and will be lost when the app is restarted.
#### `put(key: string, value: string | object, ttl?: number): boolean`
Adds a value to the cache.
- `key`: The key under which to store the value.
- `value`: The value to be stored.
- `ttl`: The time-to-live for the value, in milliseconds.
- Returns: `true` if the value was successfully stored, `false` otherwise.#### `get(key: string): string | object | null`
Retrieves the value associated with a key from the cache.
- `key`: The key for which to fetch the value.
- Returns: The value if the key exists and hasn't expired, `null` otherwise.#### `delete(key: string): boolean`
Removes a key from the cache.
- `key`: The key to be deleted.
- Returns: `true` if the key was successfully deleted, `false` otherwise.#### `hasKey(key: string): boolean`
Checks if a key exists in the cache.
- `key`: The key to be checked
- Returns: `true` if the key exists, `false` otherwise## License 📜
Bun Cache is distributed under the MIT License.