All Projects β†’ koajs β†’ Cash

koajs / Cash

Licence: mit
HTTP response caching for Koa. Supports Redis, in-memory store, and more!

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Cash

Koa Redis
Redis storage for Koa session middleware/cache with Sentinel and Cluster support
Stars: ✭ 324 (+165.57%)
Mutual labels:  redis, cache, storage, koa, session
Lad
πŸ‘¦ Lad is the best Node.js framework. Made by a former Express TC and Koa team member.
Stars: ✭ 2,112 (+1631.15%)
Mutual labels:  aws, s3, redis, koa
Memorystore
express-session full featured MemoryStore layer without leaks!
Stars: ✭ 79 (-35.25%)
Mutual labels:  cache, memory, session, sessions
Bojack
🐴 The unreliable key-value store
Stars: ✭ 101 (-17.21%)
Mutual labels:  redis, cache, storage, store
punic
Punic is a remote cache CLI built for Carthage and Apple .xcframework
Stars: ✭ 25 (-79.51%)
Mutual labels:  caching, amazon, cache, s3
Foundatio
Pluggable foundation blocks for building distributed apps.
Stars: ✭ 1,365 (+1018.85%)
Mutual labels:  aws, s3, redis, storage
Aws Toolkit Vscode
AWS Toolkit for Visual Studio Code, an extension for working with AWS services including AWS Lambda.
Stars: ✭ 823 (+574.59%)
Mutual labels:  aws, s3, amazon
Django S3 Like Storage
Your Own Amazon S3 Django Storage
Stars: ✭ 28 (-77.05%)
Mutual labels:  aws, amazon, static
Synchrotron
Caching layer load balancer.
Stars: ✭ 42 (-65.57%)
Mutual labels:  redis, cache, caching
Terraform Aws S3 Log Storage
This module creates an S3 bucket suitable for receiving logs from other AWS services such as S3, CloudFront, and CloudTrail
Stars: ✭ 65 (-46.72%)
Mutual labels:  aws, s3, storage
S5cmd
Parallel S3 and local filesystem execution tool.
Stars: ✭ 565 (+363.11%)
Mutual labels:  aws, s3, storage
Easycaching
πŸ’₯ EasyCaching is an open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier!
Stars: ✭ 1,047 (+758.2%)
Mutual labels:  redis, cache, caching
Terraform Aws Elasticache Redis
Terraform module to provision an ElastiCache Redis Cluster
Stars: ✭ 73 (-40.16%)
Mutual labels:  aws, redis, cache
Java Knowledge Mind Map
γ€πŸŒ±πŸŒ±Java服劑端ηŸ₯θ―†ζŠ€θƒ½ε›Ύθ°±γ€‘η”¨ζ€η»΄θ„‘ε›Ύζ’³η†ζ±‡ζ€»Java服劑端ηŸ₯θ―†ζŠ€θƒ½
Stars: ✭ 787 (+545.08%)
Mutual labels:  redis, cache, network
Gocache
β˜”οΈ A complete Go cache library that brings you multiple ways of managing your caches
Stars: ✭ 775 (+535.25%)
Mutual labels:  redis, cache, memory
Apicache
Simple API-caching middleware for Express/Node.
Stars: ✭ 957 (+684.43%)
Mutual labels:  redis, cache, memory
Rome
Carthage cache for S3, Minio, Ceph, Google Storage, Artifactory and many others
Stars: ✭ 724 (+493.44%)
Mutual labels:  aws, s3, cache
Django Qsessions
Extended session backends for Django (Sessions store IP, User Agent, and foreign key to User)
Stars: ✭ 64 (-47.54%)
Mutual labels:  cache, session, sessions
Aws Workflows On Github
Workflows for automation of AWS services setup from Github CI/CD
Stars: ✭ 95 (-22.13%)
Mutual labels:  aws, s3, amazon
Lada Cache
A Redis based, fully automated and scalable database cache layer for Laravel
Stars: ✭ 424 (+247.54%)
Mutual labels:  redis, cache, caching

koa-cash

build status code coverage code style styled with prettier made with lass license npm downloads

HTTP response caching for Koa. Supports Redis, in-memory store, and more!

Table of Contents

Features

Caches the response based on any arbitrary store you'd like.

  • Handles JSON and stream bodies
  • Handles gzip compression negotiation (if options.compression is set to true as of v4.0.0)
  • Handles 304 responses

πŸŽ‰ Pairs great with @ladjs/koa-cache-responses πŸŽ‰

Install

npm:

npm install koa-cash

yarn:

yarn add koa-cash

Usage

const koaCash = require('koa-cash');

// ...

app.use(koaCash())

app.use(async ctx => {
  // this response is already cashed if `true` is returned,
  // so this middleware will automatically serve this response from cache
  if (await ctx.cashed()) return;

  // set the response body here,
  // and the upstream middleware will automatically cache it
  ctx.body = 'hello world!';
});

API

app.use(koaCash(options))

Options are:

maxAge

Default max age (in milliseconds) for the cache if not set via await ctx.cashed(maxAge).

threshold

Minimum byte size to compress response bodies. Default 1kb.

compression

If a truthy value is passed, then compression will be enabled. This value is false by default.

setCachedHeader

If a truthy value is passed, then X-Cached-Response header will be set as HIT when response is served from the cache. This value is false by default.

hash()

A hashing function. By default, it's:

function hash(ctx) {
 return ctx.response.url; // same as ctx.url
}

ctx is the Koa context and is also passed as an argument. By default, it caches based on the URL.

get()

Get a value from a store. Must return a Promise, which returns the cache's value, if any.

function get(key, maxAge) {
  return Promise;
}

Note that all the maxAge stuff must be handled by you. This module makes no opinion about it.

set()

Set a value to a store. Must return a Promise.

function set(key, value, maxAge) {
  return Promise;
}

Note: maxAge is set by .cash = { maxAge }. If it's not set, then maxAge will be 0, which you should then ignore.

Example

Using a library like lru-cache, though this would not quite work since it doesn't allow per-key expiration times.

const koaCash = require('koa-cash');
const LRU = require('lru-cache');

const cache = new LRU({
  maxAge: 30000 // global max age
})

app.use(koaCash({
  get (key, maxAge) {
    return cache.get(key)
  },
  set (key, value) {
    cache.set(key, value)
  }
}))

See @ladjs/koa-cache-responses test folder more examples (e.g. Redis with ioredis).

const cached = await ctx.cashed([maxAge])

This is how you enable a route to be cached. If you don't call await ctx.cashed(), then this route will not be cached nor will it attempt to serve the request from the cache.

maxAge is the max age passed to get().

If cached is true, then the current request has been served from cache and you should early return. Otherwise, continue setting ctx.body= and this will cache the response.

Notes

  • Only GET and HEAD requests are cached.
  • Only 200 responses are cached. Don't set 304 status codes on these routes - this middleware will handle it for you
  • The underlying store should be able to handle Date objects as well as Buffer objects. Otherwise, you may have to serialize/deserialize yourself.

Usage

Contributors

Name Website
Jonathan Ong http://jongleberry.com
Nick Baugh http://niftylettuce.com

License

MIT Β© Jonathan Ong

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