All Projects → mkozjak → koa-cache-lite

mkozjak / koa-cache-lite

Licence: MIT license
Zero-dependency koa router cache

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to koa-cache-lite

Cash
HTTP response caching for Koa. Supports Redis, in-memory store, and more!
Stars: ✭ 122 (+351.85%)
Mutual labels:  caching, koa
cachegrand
cachegrand is an open-source fast, scalable and secure Key-Value store, also fully compatible with Redis protocol, designed from the ground up to take advantage of modern hardware vertical scalability, able to provide better performance and a larger cache at lower cost, without losing focus on distributed systems.
Stars: ✭ 87 (+222.22%)
Mutual labels:  caching
objection-authorize
isomorphic, "magical" authorization integration with Objection.js 🎉
Stars: ✭ 71 (+162.96%)
Mutual labels:  koa
GithubProfile
Visualizing GitHub profile
Stars: ✭ 45 (+66.67%)
Mutual labels:  koa
blog
✍️无他术,唯勤读书而多为之,自工。
Stars: ✭ 62 (+129.63%)
Mutual labels:  koa
EFCache
Second Level Cache for Entity Framework 6.1
Stars: ✭ 97 (+259.26%)
Mutual labels:  caching
wagtail-cache
A simple page cache for Wagtail based on the Django cache middleware.
Stars: ✭ 63 (+133.33%)
Mutual labels:  caching
django-cached-modelforms
🌟 ModelChoiceField implementation that can accept lists of objects, not just querysets
Stars: ✭ 14 (-48.15%)
Mutual labels:  caching
Rephic
💼 Rephic 是一个基于 Koa 的 React 服务端渲染工程脚手架。支持 React-Router, Redux 以及 Less, Sass。
Stars: ✭ 23 (-14.81%)
Mutual labels:  koa
koa-simple-ratelimit
Simple rate limiter for Koa.js v2 web framework
Stars: ✭ 17 (-37.04%)
Mutual labels:  koa
trickster
Open Source HTTP Reverse Proxy Cache and Time Series Dashboard Accelerator
Stars: ✭ 1,753 (+6392.59%)
Mutual labels:  caching
nodejs-api-kickstarter
A batteries included NodeJS API Kickstater focused on extensability and developer productivity.
Stars: ✭ 20 (-25.93%)
Mutual labels:  koa
stash
Key-value store abstraction with plain and cache driven semantics and a pluggable backend architecture.
Stars: ✭ 71 (+162.96%)
Mutual labels:  caching
node-typescript-starter
REST API using Node with typescript, KOA framework. TypeORM for SQL. Middlewares JWT (auth), CORS, Winston Logger, Error, Response
Stars: ✭ 19 (-29.63%)
Mutual labels:  koa
endorphin
Key-Value based in-memory cache library which supports Custom Expiration Policies
Stars: ✭ 14 (-48.15%)
Mutual labels:  caching
powered-cache
The most powerful caching and performance suite for WordPress.
Stars: ✭ 31 (+14.81%)
Mutual labels:  caching
koa
A golang framework like koa.js and has the best performance with net/http.
Stars: ✭ 30 (+11.11%)
Mutual labels:  koa
DailyBugle
📰Modern MVVM Android application following single activity architecture which fetches news from 🕷️ news API. this repository contains some best practices ⚡ of android development
Stars: ✭ 17 (-37.04%)
Mutual labels:  caching
campus-leaflets
校园传单是一个使用 Node.js、MongoDB、Koa、EJS、MDL、Less、RequireJS 和 Gulp 开发的 Node 应用程序。
Stars: ✭ 16 (-40.74%)
Mutual labels:  koa
java-core
Collections of solutions for micro-tasks created while building modules as part of project. Also has very fun stuffs :)
Stars: ✭ 35 (+29.63%)
Mutual labels:  caching

GitHub license npm Donate Bitcoin/Altcoins
NPM

koa-cache-lite

Zero-dependency koa router cache

News

important Breaking API changes were made since version 3.x.

Once included, the library returns the 'configure', 'init', 'middleware', 'exists', 'clear' and 'currentCacheType' methods.

'configure' is used to configure caching, and 'middleware' to get the generator which can be used with koa.use().

'init' is an optional method for the initialization of the caching process. This is a convenience method used, for example, in a 'cluster' environment, where you would send a wrapped 'middleware' function to be able to, in example, clear cache from the worker, even though the cache was initialized in the master process.

'clear' can be used to clear the cache and 'currentCacheType' to get the currently used caching system.

See below for API details and examples.

Installation

Locally in your project or globally:

npm install koa-cache-lite
npm install -g koa-cache-lite

Quick start

'use strict'

var cache = require('koa-cache-lite')
var koa = require('koa')()

// use in-memory cache
cache.configure({
  '/api/v1/test': 3000
}, {
  debug: true
})

// or redis (install ioredis manually with npm install ioredis)
cache.configure({
  '/api/v1/test': 3000
}, {
  external: {
    type: 'redis',
    host: '127.0.0.1',
    port: 6379
  },
  debug: true
})

// use increasing cache timeout and customize cache key
cache.configure({
    '/api/v1/test': 'increasing'
  }, {
    increasing: {
      1: 1000,  // key = route calls per minute, value = cache timeout
      5: 2000,
      25: 5000,
      100: 10000
    },
    keyFragment: 'cache', // header field containing cache key appendix
    debug: true
})

// start caching
koa.use(cache.middleware())

koa.listen(1337)

API

koa-cache-lite accepts two arguments.

First one is an object consisting of routes and the second one is an options object, having an 'external' key where a type of cache and its location are defined,
'increasing' key consisting of cache timeouts per step
(Number representing route calls per minute needed to increase cache timeout is the key and Number is a value representing cache timeout per step)
and 'cacheKeyArgs' where additional internal labeling values can be defined to customize the cache key.
The debug mode can be enabled (console is used for debugging).

cache.configure(routes[, options]) -> Class

Sets various caching options regarding urls, internal cache keys, debug mode and more.

routes {Object}:

  • key: {String} A string value consisting of optional parameter placeholders used to match the route for caching. For example, one can have multiple colon placeholders throughout the url to flag the dynamic parts of it and one asterisk to match everything with and after the given string.
cache.configure({
  ...
  routes: {
    {String}: {String|Number|Boolean}
  }
})

// or
cache.configure({
  ...
  routes: {
    {String}: {
      timeout: {String|Number}
      cacheKeyArgs: {
        headers: {String|Array},
        query: {String|Array}
      }
    }
  }
})

// in example
cache.configure({
  ...
  routes: {

    // will cache based on parameter 'id'
    '/api/client/:id/tickets': 30000,

    // will cache based on parameter 'uid' and an http header field 'x-auth-custom'
    '/api/admin/register/:uid': {
      timeout: 10000,
      cacheKeyArgs: {
        headers: 'x-auth-custom'
      }
    }
  }
})

options {Object}:

  • external: {Object} External storage database configuration.
    • type: {String} Type of caching system used. Can be 'memory' or 'redis' (in which case you need to install ioredis manually).
    • host: {String} If type is 'redis', the address used to connect to. Defaults to '127.0.0.1'.
    • port: {Number} If type is 'redis', the port used to connect to. Defaults to 6379.
  • increasing: {Object} An object consisting of api calls per minute cache timeout increase.
  • cacheKeyArgs: {String} Default cache key to be used internally by the library.
  • cacheKeyPrefix: {String} Cache key prefix for storing.
  • minimumSize: {Number} Minimum http body size needed to cache the response.
  • vary: {Array} List of HTTP request headers used for Vary. Defaults to '["Accept-Encoding"]'.
  • ignoreNoCache: {Boolean} Flag for ignoring 'Cache-Control: no-cache' headers.
  • debug: {Boolean} Flag showing whether to debug the middleware or not.
  • logging: {Function} External logging function to be used for debugging.

cache.init() -> Class

Start the caching process. This method should only be used when different contexts throughout your application are used. In example, when using the 'cluster' module, you should use cache.init() in master and send a wrapped cache.clear() function to worker processes so you can use the Cache instance.

cache.init()

cache.middleware() -> Generator

Starts the caching process and returns a generator which can be used in conjunction with koa.use().

koa.use(cache.middleware())

cache.exists(key) -> Promise

Check if cache for the given key exists.

cache.clear([keys])

Clear cache entries individually or all of them. If 'keys' is not provided it clears all the keys based on cacheKeyPrefix if defined. It also supports glob-style patterns:

  • h?llo matches hello, hallo and hxllo
  • h*llo matches hllo and heeeello
  • h[ae]llo matches hello and hallo, but not hillo
  • h[^e]llo matches hallo, hbllo, ... but not hello
  • h[a-b]llo matches hallo and hbllo
// clear all keys (based on cacheKeyPrefix)
cache.clear()

// or only /api/v1/users
cache.clear('/api/v1/users')

// or anything under '/api/v2'
cache.clear('/api/v2/*')

cache.currentCacheType() -> String

Returns a type of caching system currently used.

Professional support

I offer professional support for koa-cache-lite and beyond. I have many years of expertise on building robust, scalable Node.js applications and can help you overcome issues and challenges preventing you to ship your great products. I also excel in software architecture and implementation, being able to provide you with development, planning, consulting, training and customization services. Feel free to contact me so we can discuss how to help you finish your products!

Sponsors

Become a sponsor and get your logo on project's README on GitHub with a link to your site. Feel free to contact me for the arrangement!

Donate

If you love this project, consider donating!

License

MIT

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].