Coder Social home page Coder Social logo

bahung1221 / cache-all Goto Github PK

View Code? Open in Web Editor NEW
22.0 4.0 2.0 229 KB

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

License: MIT License

JavaScript 97.09% TypeScript 2.91%
node node-module cache cache-storage cachemanager singleton filecache rediscache memcached nodejs

cache-all's Introduction

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

cache-all's People

Contributors

bahung1221 avatar bpodwinski avatar ffkl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cache-all's Issues

Flutter: cache-sksl not working on iOS (TestFlight)

image

Sorry to disturb you, I saw your flutter issues

I used flutter build ios to generate .ipa

flutter build ios --release --bundle-sksl-path flutter_01.sksl.json

I can’t upload to TestFlight, he reported an error, I uploaded it directly using Xcode, and it succeeded

Error: code 1176 (App Store operation failed. Unable to process application at this time due to the following error: Invalid Provisioning Profile. The provisioning profile included in the bundle com.mhh.xxx [Payload/Runner.app] is invalid. [Missing code-signing certificate]. A Distribution Provisioning profile should be used when submitting apps to the App Store. For more information, visit the iOS Developer Portal..)

image

How did you upload it successfully?

thanks

Can't resolve 'hiredis'

Getting a compilation error, using "cache-all": "^2.0.7":

ERROR in ./node_modules/redis-parser/lib/hiredis.js
Module not found: Error: Can't resolve 'hiredis' in '.../node_modules/redis-parser/lib'
 @ ./node_modules/redis-parser/lib/hiredis.js 3:14-32
 @ ./node_modules/redis-parser/lib/parser.js
 @ ./node_modules/redis-parser/index.js
 @ ./node_modules/redis/index.js
 @ ./node_modules/cache-all/src/core/redis.js
 @ ./node_modules/cache-all/src/cache.js
 @ ./node_modules/cache-all/memory.js
 @ ./src/containers/Workbench.jsx
 @ ./src/containers/AppContainer.jsx
 @ ./src/renderer/index.js
 @ multi react-hot-loader/patch ./src/renderer/index.js
Child html-webpack-plugin for "index.html":
     1 asset
    Entrypoint undefined = ./index.html
       1 module

How to update the cache when edit data?

I ran into a problem that when editing data, old data that is stored in the cache does not update the changes until the cache expires. How to do caching when changing data?

routing:

const express = require("express");
const router = express.Router();
const controller = require("../controllers/users");
const cache = require('cache-all')

router.get("/get_all", cache.middleware(86400), controller.getAll);
router.patch("/patch/:USER_ID", controller.update);

module.exports = router;

controller:

const cache = require('cache-all')

module.exports.update = async function (req, res) {
    try {
        let users = await User.findOne({ USER_ID: req.params.USER_ID });
        await users.set({
            LOGIN: req.body.LOGIN
        })
        users.save().then(function () {
            return cache.set('patch:' + req.params.USER_ID, users);
        }).then(() => {
            return res.status(200).json(users);
        })
    } catch (e) {
        errorHandler(res, e);
    }
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.