Socket
Socket
Sign inDemoInstall

midd-cache

Package Overview
Dependencies
2
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    midd-cache

http cache middleware for midd


Version published
Weekly downloads
10
Maintainers
2
Install size
25.0 kB
Created
Weekly downloads
 

Readme

Source

HTTP cache middleware for midd

Usage

const middCache = require('midd-cache');
app.use(middCache())
Manually check cache fresh when request coming

After cache expired in the client(browser), client will send a revalidate request with If-Modified-Since or If-None-Match headers. Use req.fresh(status) to check that, and if cache is fresh, it will response 304 and return true, otherwise it will return false. The status is current resource state about Last-Modified or ETag. It could have two keys(keys name is case insensitive):

  • Last-Modified or LastModified: number(ms) or string(utc) or Date. The string format is like date.toUTCString()
  • ETag: the etag string

Example:

app.use((req, resp, next)=>{
  let lastMod = getRecordLastModified(req.params.id);
  if(req.fresh({'Last-Modified': lastMod})) return;
  resp.send(updatedRecord)
})
Auto check cache fresh when using resp.send

This mechanism must be used with midd-send middleware and only effective on resp.send method. You should attch midd-send first. When you call resp.send(body) method, it will generate ETag from the body, and get Last-Modified from the headers. Then use them to compare with the request's If-Modified-Since or If-None-Match. If the cache is fresh, respond 304, otherwise respond the body with ETag and Last-Modified headers.

Set cache headers for response

resp.setCache(options) Options have:

  • maxAge: number, milliseconds. Used in Cache-Control header
  • private: boolean. Used in Cache-Control header
  • Last-Modified or LastModified: number(ms) or string(utc) or Date. The key name is case insensitive.
  • ETag: the etag string. The key name is case insensitive.

License

Licensed under MIT

Copyright (c) 2017 Tian Jian

Keywords

FAQs

Last updated on 22 Feb 2017

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc