Skip to content

dancastillo/cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cache

In-Memory cache for node.js

Install

To install cache in an existing project as a dependency:

Install with npm:

npm install @dancastillo/cache

Install with yarn:

yarn add @dancastillo/cache

Install with pnpm:

pnpm install @dancastillo/cache

Example

// ESM
import Cache from '@dancastillo/cache'
const cache = new Cache()
cache.set('foo', 'bar')
// CommonJs
const Cache = require('@dancastillo/cache')
const cache = Cache()

cache.set('foo', 'bar')

Configuration

scache(<options>) accepts an options object.

Options

DurationOptions

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

const value = cache.get('foo') // bar
  • get(key): any: Used to get a value stored in cache
    • key: string

put

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

del

cache.del('foo')
  • del(key): void: Used to delete a value stored in cache
    • key: string

clear

cache.clear()
  • clear(): void: Used to delete all values stored in cache

keys

cache.keys()
  • keys(): string[]: Used to retrieve all the keys in cache.

License

Licensed under MIT.