Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dancastillo/cache
Simple cache (in memory) for node.js
https://github.com/dancastillo/cache
Last synced: about 1 month ago
JSON representation
Simple cache (in memory) for node.js
- Host: GitHub
- URL: https://github.com/dancastillo/cache
- Owner: dancastillo
- License: mit
- Created: 2023-01-13T01:18:20.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-19T03:28:24.000Z (about 1 month ago)
- Last Synced: 2024-11-19T04:25:01.200Z (about 1 month ago)
- Language: JavaScript
- Size: 55.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cache
In-Memory cache for node.js
### Install
To install cache in an existing project as a dependency:
Install with npm:
```sh
npm install @dancastillo/cache
```
Install with yarn:
```sh
yarn add @dancastillo/cache
```
Install with pnpm:
```sh
pnpm install @dancastillo/cache
```### Example
```js
// ESM
import cache from '@dancastillo/cache'
const c = cache()
c.set('foo', 'bar')
``````js
// CommonJs
const cache = require('@dancastillo/cache')
const c = cache()
c.set('foo', 'bar')
```## Configuration
`scache()` accepts an options object.
### Options
#### DurationOptions
```js
const duration = {
days: 1,
hours: 12,
minutes: 10,
seconds: 30
}const cache = cache({ duration })
```- `duration`: This is used to add a time to live for the cache data. This option is optional.
- `duration.days`: Number of days to hold the data in cache. This option is optional.
- `duration.hours`: Number of hours to hold the data in cache. This option is optional.
- `duration.minutes`: Number of minutes to hold the data in cache. This option is optional.
- `duration.seconds`: Number of seconds to hold the data in cache. This option is optional.### Methods
get
```js
const value = cache.get('foo') // bar
```- `get(key): any`: Used to get a value stored in cache
- `key`: stringput
```js
cache.put('foo', 'bar')// with duration
const duration = { hours: 12 }
cache.put('foo', 'bar', duration)
```- `put(key, value, DurationOptions): void`: Used to get a value stored in cache.
- `key`: string
- `value`: any
- `DurationOptions`: [here](#DurationOptions)del
```js
cache.del('foo')
```- `del(key): void`: Used to delete a value stored in cache
- `key`: stringclear
```js
cache.clear()
```- `clear(): void`: Used to delete all values stored in cache
keys
```js
cache.keys()
```- `keys(): string[]`: Used to retrieve all the keys in cache.
## License
Licensed under [MIT](./LICENSE).