New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

koa-incache

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-incache

Koa cache middleware

  • 0.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-60%
Maintainers
1
Weekly downloads
 
Created
Source

koa-incache

Koa cache middleware

Installation

Koa-incache requires koa2 and node v7.6.0 or higher

npm install koa-incache --save

Example

Basic usage
const cache = require('koa-incache');
const koa = require('koa');
const app = new koa();

app.use(cache());

app.use(ctx=>{
    const result = 'hello world';
    ctx.cached(result);
    ctx.body = result;
});

app.listen(3000);
Routing

The cache is made using ctx.originalUrl as key

const fs = require('fs');
const cache = require('koa-incache');
const koa = require('koa');
const Router = require('koa-router');
const app = new koa();
const router = new Router();

app.use(cache({
    maxAge: 60000 // 1 minute max age
}));

router.get('/this/is/cached', function (ctx, next) {
    const content = fs.readFileSync('myFile');
    ctx.cached(content);
    ctx.body = content;
});

router.get('/this/is/not/cached', function (ctx, next) {
    const content = fs.readFileSync('myFile');
    ctx.body = content;
});

router.get('/uncache', function (ctx, next) {
    const content = fs.readFileSync('myFile');
    ctx.cached(content);
    
    if(foo)
        ctx.uncache();
    
    ctx.body = content;
});

app
    .use(router.routes())
    .use(router.allowedMethods());

app.listen(3000);
Access to InCache instance

From koa context it's possible get InCache by ctx.cache

router.get('/my/root', function (ctx, next) {
    ctx.cache.set('my key', 'my value');
    //...
});

API context

  • cached accept an argument that is the content to be stored
  • uncache remove cache for context root

For more info about InCache click here

Changelog

You can view the changelog here

License

koa-incache is open-sourced software licensed under the MIT license

Authors

Fabio Ricali

Davide Polano

Keywords

FAQs

Package last updated on 02 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc