https://github.com/thevxn/node-cache
Simple in-memory caching for Node.js apps.
https://github.com/thevxn/node-cache
cache cache-storage caching node node-js nodejs typescript
Last synced: 10 months ago
JSON representation
Simple in-memory caching for Node.js apps.
- Host: GitHub
- URL: https://github.com/thevxn/node-cache
- Owner: thevxn
- License: mit
- Created: 2024-06-11T18:07:02.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-06-11T20:42:43.000Z (over 1 year ago)
- Last Synced: 2025-02-12T17:47:00.988Z (11 months ago)
- Topics: cache, cache-storage, caching, node, node-js, nodejs, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@savla-dev/node-cache
- Size: 18.6 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# node-cache
Simple in-memory caching for Node.js apps.
## Getting Started
Create a new instance of the cache, passing in the TTL for its items in seconds:
```ts
const cache = new Cache(900)
```
The following methods are exposed:
```ts
set(key: string, value: T): void
get(key: string): T | null
delete(key: string): void
// Clears all items from the cache
clear(): void
```
## Example usage
```ts
import { Cache } from '@savla-dev/node-cache'
const cache = new Cache(900) // 15 minutes TTL
async function fetchWithCache(url: string): Promise {
const cachedData = cache.get(url)
if (cachedData) {
console.log('Returning cached data')
return cachedData
}
console.log('Fetching new data')
let response: Response
try {
response = await fetch(url)
} catch (e) {
throw new Error(`Error during fetch: ${e}`)
}
try {
const data = await response.json()
cache.set(url, data)
return data
} catch (e) {
console.log(`Error during response parsing: ${e}`)
}
}
```