All Projects → fmiras → micro-cacheable

fmiras / micro-cacheable

Licence: MIT license
A micro utility for data caching

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to micro-cacheable

Gretchen
Making fetch happen in TypeScript.
Stars: ✭ 301 (+760%)
Mutual labels:  fetch, micro
micro-query
Simple query string parser for Vercel's Micro
Stars: ✭ 23 (-34.29%)
Mutual labels:  micro
hent
A small utility to fetch remote files into buffers
Stars: ✭ 23 (-34.29%)
Mutual labels:  fetch
Mvvmicro
Minimalist MVVM framework for .NET.
Stars: ✭ 22 (-37.14%)
Mutual labels:  micro
react-use-downloader
Creates a download handler function and gives progress information
Stars: ✭ 65 (+85.71%)
Mutual labels:  fetch
cache
Laravel & Lumen Cache Service | File and Redis cache system
Stars: ✭ 19 (-45.71%)
Mutual labels:  cache-storage
s3-example
Simple example using micro for uploading stuff to AWS S3.
Stars: ✭ 45 (+28.57%)
Mutual labels:  micro
dockerpkg-example
Example app running in small docker container using dockerpkg
Stars: ✭ 54 (+54.29%)
Mutual labels:  micro
react-native-fetch
🌍Fetch API wrapped as a component with support for retries & timeouts
Stars: ✭ 20 (-42.86%)
Mutual labels:  fetch
simple-cache
⚡ Simple Cache Abstraction Layer for PHP
Stars: ✭ 28 (-20%)
Mutual labels:  cache-storage
yafetch
Yet another fetch ...
Stars: ✭ 27 (-22.86%)
Mutual labels:  fetch
micro
abp+micro+typescript+vue+element
Stars: ✭ 92 (+162.86%)
Mutual labels:  micro
affilicats
🐈 Progressive Web App demo that showcases flaky network resilience measures (📶 or 🚫📶).
Stars: ✭ 65 (+85.71%)
Mutual labels:  cache-storage
tcl-modules
A collection of pure Tcl, production-ready micro packages
Stars: ✭ 25 (-28.57%)
Mutual labels:  micro
fetch-reject
Simple wrapper for fetch which rejects on HTTP error
Stars: ✭ 70 (+100%)
Mutual labels:  fetch
m.test
m(icro)test is a lightweight test runner for node.js written in es6+
Stars: ✭ 17 (-51.43%)
Mutual labels:  micro
fennch
Modern fetch-based axios-like HTTP client for the browser and node.js
Stars: ✭ 12 (-65.71%)
Mutual labels:  fetch
Fetch
Asynchronous HTTP client with promises.
Stars: ✭ 29 (-17.14%)
Mutual labels:  fetch
comlink-fetch
⚙️ A Web worker fetch wrapper using comlink
Stars: ✭ 43 (+22.86%)
Mutual labels:  fetch
vue2-element
基于vue2 + vue-router2 + element-ui + vuex2 + fetch + webpack2 企业级后台管理系统最佳实践
Stars: ✭ 115 (+228.57%)
Mutual labels:  fetch

micro-cacheable

NPM version Build Status code style: prettier XO code style Join the community on Spectrum Greenkeeper badge

micro-cacheable is an utility for data caching focused on micro framework. The problem this package solves is to save the already requested data in-memory to have it available for a configurated time without processing it again.

Usage

cd my-micro-project/
npm install --save micro-cacheable

and add use the package like this:

// index.js
const cache = require('micro-cacheable')

const microFn = (req, res) => {
  return new Date()
}

module.exports = cache(60 * 60 * 1000, microFn)

then just run your microservice normally and it will return the same result for an hour (first param as miliseconds) unless that you change the request url.

A more useful example:

Let's say that we need a microservice that receives a name (string) and search data of a person on 3 or 4 APIs:

const { parse } = require('url')
const fetch = require('node-fetch')

module.exports = async (req, res) => {
  const { searchName } = parse(req.url, true).query
  const facebookData = await fetch('https://someapi1.com')
  const githubData = await fetch('https://someapi2.com')
  const financialData = await fetch('https://someapi3.com')
  return { facebookData, githubData, financialData }
}

This microservice would fetch 3 APIs every time it receives a request. Probably, in some cases, if the microservice receive the same name it will return the same data, at least for the same day, so you can just add micro-cacheable like this:

const { parse } = require('url')
const fetch = require('node-fetch')
const cache = require('micro-cacheable')

const microFn = async (req, res) => {
  const { searchName } = parse(req.url, true).query
  const facebookData = await fetch('https://someapi1.com')
  const githubData = await fetch('https://someapi2.com')
  const financialData = await fetch('https://someapi3.com')
  return { facebookData, githubData, financialData }
}

module.exports = cache(24 * 60 * 60 * 1000, microFn) // One day data caching

Mongo Support

micro-cacheable supports Mongo (and hopefully Redis in the future) because in-memory data cache can't scale horizontally and if you work with microservices you will loose that advantage. So you can avoid that problem setting the MONGO_URL and MONGO_DB enviroment variables so all your microservice's instances use the same cache.

Why?

I worked on a project with micro using it for making web-scrapping workers that take too long the first time to get the data, and users requested often the same data so with this I can save a lot of requests, processing and time making requests of +5000ms only take 50ms.

Contributing

  1. Fork this repository to your own GitHub account and then clone it to your local device
  2. Link the package to the global module directory: npm link
  3. Within the module you want to test your local development instance of micro-cacheable, just link it to the dependencies: npm link micro-cacheable. Instead of the default one from npm, node will now use your clone of micro-cacheable!

Credits

Thanks to ZEIT Team for giving us micro to make our life easier!

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