All Projects → bahung1221 → cache-all

bahung1221 / cache-all

Licence: MIT license
Fast, efficient cache engines for both express routes & native nodeJs (redis, in-memory & file cache)

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to cache-all

Cachemanager
CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.
Stars: ✭ 2,049 (+9213.64%)
Mutual labels:  memcached, cachemanager
incache
Powerful key/value in-memory storage or on disk to persist data
Stars: ✭ 16 (-27.27%)
Mutual labels:  singleton, cache-storage
BowieCode
Personal Code/Snippet Library for Unity 3D
Stars: ✭ 23 (+4.55%)
Mutual labels:  singleton
fortune-commons
Fortune Commons is an project focused on all aspects of reusable Java components.
Stars: ✭ 17 (-22.73%)
Mutual labels:  memcached
libmemcached
Resurrection of libmemcached
Stars: ✭ 30 (+36.36%)
Mutual labels:  memcached
micro-cacheable
A micro utility for data caching
Stars: ✭ 35 (+59.09%)
Mutual labels:  cache-storage
hibernate-l2-memcached
Hibernate second level cache implementation over memcached
Stars: ✭ 34 (+54.55%)
Mutual labels:  memcached
lambda-mailer
Simple module for receiving an email from a contact form on your website.
Stars: ✭ 83 (+277.27%)
Mutual labels:  node-module
SwiftlyCache
SwiftlyCache is a thread safe IOS general cache Library
Stars: ✭ 84 (+281.82%)
Mutual labels:  cache-storage
ioBroker.epson stylus px830
Zustand Druckerpatronen im EPSON Stylus PX830 für ioBroker auslesen
Stars: ✭ 18 (-18.18%)
Mutual labels:  node-module
session
Aplus Framework Session Library
Stars: ✭ 170 (+672.73%)
Mutual labels:  memcached
sparql-proxy
SPARQL-proxy: provides cache, job control, and logging for any SPARQL endpoint
Stars: ✭ 26 (+18.18%)
Mutual labels:  memcached
ember-cli-es6-transform
Import ES6 modules from npm, bower or anywhere else in your app.
Stars: ✭ 13 (-40.91%)
Mutual labels:  node-module
node-ts-dedent
TypeScript package which smartly trims and strips indentation from multi-line strings
Stars: ✭ 119 (+440.91%)
Mutual labels:  node-module
docker-lemp
A single container LEMP complete fullstack with latest release of PHP7.4.33, 8.0.26 & 8.1.13/8.2RC and MySQL, nginx, PostgreSQL, phalcon, swoole, mailcatcher, beanstalkd, elasticsearch, memcached, redis, adminer and all you ever need; on top alpine3.15
Stars: ✭ 106 (+381.82%)
Mutual labels:  memcached
dxram
A distributed in-memory key-value storage for billions of small objects.
Stars: ✭ 25 (+13.64%)
Mutual labels:  memcached
node-cli-boilerplate
🪓 Create node cli with this user friendly boilerplate
Stars: ✭ 17 (-22.73%)
Mutual labels:  node-module
originator
🌱 es6 starter - babel, tape, zuul, npm scripts
Stars: ✭ 12 (-45.45%)
Mutual labels:  node-module
aiomcache
Minimal asyncio memcached client
Stars: ✭ 129 (+486.36%)
Mutual labels:  memcached
xlsx-extract
nodejs lib for extracting data from XLSX files
Stars: ✭ 33 (+50%)
Mutual labels:  node-module

npm version Dependency Status Build Status Coverage Status Codacy Badge

NPM info

cache-all

🚀 Fast, simple cache engines for expressJS & native nodeJS (redis, in-memory & file caching).

  • Multi cache engines, each engine has one singleton instance and independent with other engine.
  • Include express middleware, which can be use for cache response on specific routes.
  • Init once and then use anywhere for caching anything in your application.

Install

npm install --save cache-all

or

yarn add cache-all

Usages (single cache engine):

Init

Init cache engine once and then you can use it anywhere, recommend init when booting your application

Example init in your server.js:

const express = require('express')
const cache = require('cache-all') // default is in-memory engine
// or
const cache = require('cache-all/memory') // explicit in-memory engine
// or
const cache = require('cache-all/file') // file engine
// or
const cache = require('cache-all/redis') // redis engine
const app = express()

// if you need to use cache methods immediately after init method, you must await it `await cache.init({...})`
cache.init({
  ttl: 90,
})
// ...

app.listen(...)

Default config: Just config for engine that will be use

  • in-memory
{
  ttl: 90,
  isEnable: true, // Flag for enable/disable cache, useful for development
}
  • file
{
  ttl: 90,
  isEnable: true,
  file: {
    path: path.join(process.cwd(), 'storage', 'cache') // Storage path for file cache engine
  }
}
  • redis
{
  ttl: 90,
  isEnable: true,
  redis: {
    port: 6379,
    host: '127.0.0.1',
    // password: String,
    // database: String,
    // prefix: String, // default is `cacheall:`
    // setex: Function
  }
}

set(key, value, [expireIn])

Set cache:

const cache = require('cache-all')

cache
  .set('foo', 'bar')
  .then(result => console.log(result))

Set cache with specific expire time (second):

const cache = require('cache-all')

cache
  .set('foo', 'bar', 90)
  .then(result => console.log(result)) // {status: 1}

get(key)

Get cache (if key doesn't exist, null will be return):

const cache = require('cache-all')

cache
  .get('foo')
  .then(result => console.log(result)) // 'bar'

getAll()

Get all cached entries as array:

const cache = require('cache-all')

cache
  .getAll()
  .then(result => console.log(result)) // [ { key: 'foo', value: 'bar'},... ]

has(key)

Deprecated: should use cache.get and then check returned value instead use this function because costs of these functions is same.

Check if given key exist:

const cache = require('cache-all')

cache
  .has('foo')
  .then(result => console.log(result)) // true

remove(key)

Remove given cache key:

const cache = require('cache-all')

cache
  .remove('foo')
  .then(result => console.log(result)) // {status: 1}

removeByPattern(pattern)

Remove all cached data base on pattern/text:

const cache = require('cache-all')

await cache.set('user_foo', { name: 'foo' })
await cache.set('user_bar', { name: 'bar' })

await cache.removeByPattern('user') // or removeByPattern(/user/)

middleware([expireIn], [prefix]) (Cache on express route)

This package provide a middleware which will cache your response data base on request fullpath, request method and prefix (optinal).

NOTE: using prefix if you want manual clear data that was cached by middleware (using removeByPattern(prefix) method)

const express = require('express')
const router = express.Router()
const cache = require('cache-all')

router.get('/api/user', cache.middleware(86400, 'user'), function(req, res, next) {
  res.json({foo: 'bar'})
})
// First time request '/foo' will cache response data before send back to client (non-blocking)
// Next time requests '/foo' will be response cached data immediately

Usages (multi engine)

You can use many cache engine together in your application, each engine still has singleton instance of it, that work independent with other

Just require specific engine you need instead require root

  • init
const fileCache = require('cache-all/file')
const memoryCache = require('cache-all/memory')

// ...
fileCache.init({
  ttl: 60,
  file: {
    path: path.join(process.cwd(), 'storage', 'cache')
  }
})
memoryCache.init({
  ttl: 60,
})
// ...

app.listen(...)
  • set/get/has/remove/middleware
const fileCache = require('cache-all/file')
const memoryCache = require('cache-all/memory')

fileCache
  .set('foo', 'bar', 90)
  .then(result => console.log(result)) // {status: 1}
  
memoryCache
  .set('foo', 'bar', 90)
  .then(result => console.log(result)) // {status: 1}

Typescript

Add @types/express and @type/redis as devDependencies:

yarn add -D @types/express @types/redis

That's it!

Test

npm run test

TODO

  • Mongo cache engines
  • Reduce number of dependencies
  • Update Code coverage
  • Event

Contributes

You are welcome <3

Release Note

Version Date Description
1.0.0 2019-01-14 First version, contain basic functions
1.1.0 2019-08-19 Add removeByPattern function & update dependencies
2.0.0 2019-09-05 Re-structure (DRY) & remove mkdirp dependency
2.0.1 2019-09-08 Refactor FileStore - use ES6 class instead prototype
2.0.2 2019-09-21 Add getAll method & integrate travis-ci & code coverage
2.0.6 2019-10-24 Allow redis empty prefix PR#15
2.0.8 2019-10-28 FS async implementation PR#19
2.1.0 2019-10-28 Add type definition (typescript) PR#22
2.1.1 2020-08-05 Upgrade node-redis to v3 (Removed hiredis completely)

License

This project is licensed under the terms of the MIT license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].