Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I cache a third party json? #87

Open
ChristianBorresen opened this issue May 22, 2017 · 1 comment
Open

How can I cache a third party json? #87

ChristianBorresen opened this issue May 22, 2017 · 1 comment

Comments

@ChristianBorresen
Copy link

Hi,

I've created an API that is served through a static json on a separate domain. I would like to cache this json on my server and pass it through my app so that I don't have to hit my API with every visitor. I'm not very experienced, so I'm not sure how I would achieve this.

Currently, my routes are being cached, but they are still sending the http request to my third party json. Let's assume that my api url is https://exampleapi.com/item.json

This is what I currently have (none of my attempts at json caching has worked):

var cache = (duration) => {
  return (req, res, next) => {
    let key = '__express__' + req.originalUrl || req.url
    let cachedBody = mcache.get(key)
    if (cachedBody) {
      console.log('There is a cachedBody')
      res.send(cachedBody)
      return
    } else {
      console.log('There is not a cachedBody')
      res.sendResponse = res.send
      res.send = (body) => {
        mcache.put(key, body, duration *1000);
        res.sendResponse(body)
      }
      next()
    }
  }
}

app.get('/', cache(60), function(req, res) {
  setTimeout(() => {
    res.render('../dist/index', {
      req: req,
      res: res
    });
    console.timeEnd(`GET: ${req.originalUrl}`);
  }, 5000);
});
@slidenerd
Copy link

JSON objects are storable in this lib, check the code below for example

'use strict';
const cache = require('memory-cache')

cache.put('ETH-USD', { "Response": "Success", "Message": "This api will soon move to mi-api path.", "Data": { "Algorithm": "Ethash", "ProofType": "PoW", "BlockNumber": 4239827, "NetHashesPerSecond": 96366939315478.141, "TotalCoinsMined": 94435901.9053, "BlockReward": 5.0, "AggregatedData": { "TYPE": "5", "MARKET": "CCCAGG", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "1", "PRICE": "284.9", "LASTUPDATE": "1504585778", "LASTVOLUME": "0.223", "LASTVOLUMETO": "65.337439", "LASTTRADEID": "1504585778.7719", "VOLUME24HOUR": "1925119.1785345688", "VOLUME24HOURTO": "581958947.8059875", "OPEN24HOUR": "345.53", "HIGH24HOUR": "346.44", "LOW24HOUR": "275.05", "LASTMARKET": "Kraken" }, "Exchanges": [{ "TYPE": "2", "MARKET": "BTCE", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "1", "PRICE": "205.50099", "LASTUPDATE": "1500979696", "LASTVOLUME": "0.93965638", "LASTVOLUMETO": "193.10031634981618", "LASTTRADEID": "113648081", "VOLUME24HOUR": "0", "VOLUME24HOURTO": "0", "OPEN24HOUR": "205.50099", "HIGH24HOUR": "205.50099", "LOW24HOUR": "205.50099" }, { "TYPE": "2", "MARKET": "OKCoin", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "2", "PRICE": "284.2", "LASTUPDATE": "1504585230", "LASTVOLUME": "0.579", "LASTVOLUMETO": "164.5518", "LASTTRADEID": "18448444", "VOLUME24HOUR": "8767.919", "VOLUME24HOURTO": "2655671.7731800014", "OPEN24HOUR": "355", "HIGH24HOUR": "355", "LOW24HOUR": "261" }, { "TYPE": "2", "MARKET": "Kraken", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "1", "PRICE": "292.993", "LASTUPDATE": "1504585778", "LASTVOLUME": "0.223", "LASTVOLUMETO": "65.337439", "LASTTRADEID": "1504585778.7719", "VOLUME24HOUR": "208028.28756465", "VOLUME24HOURTO": "63724039.95610457", "OPEN24HOUR": "348.49", "HIGH24HOUR": "349", "LOW24HOUR": "281.301" }, { "TYPE": "2", "MARKET": "Cexio", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "2", "PRICE": "307.57722229", "LASTUPDATE": "1504585731", "LASTVOLUME": "0.995", "LASTVOLUMETO": "306.03933617855", "LASTTRADEID": "791220", "VOLUME24HOUR": "35567.683433", "VOLUME24HOURTO": "11300633.931886906", "OPEN24HOUR": "359.80558409", "HIGH24HOUR": "361.99999494", "LOW24HOUR": "296.54426928" }, { "TYPE": "2", "MARKET": "Exmo", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "2", "PRICE": "288", "LASTUPDATE": "1504585716", "LASTVOLUME": "0.01916638", "LASTVOLUMETO": "5.51991744", "LASTTRADEID": "19078579", "VOLUME24HOUR": "3236.417659919998", "VOLUME24HOURTO": "1002991.9135371699", "OPEN24HOUR": "349", "HIGH24HOUR": "352.29085857", "LOW24HOUR": "280.00000001" }, { "TYPE": "2", "MARKET": "Coinbase", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "4", "PRICE": "296.61", "LASTUPDATE": "1504585765", "LASTVOLUME": "0.00023529", "LASTVOLUMETO": "0.06978936690000001", "LASTTRADEID": "10323690", "VOLUME24HOUR": "521959.42118457897", "VOLUME24HOURTO": "161553551.0031986", "OPEN24HOUR": "347.72", "HIGH24HOUR": "348.52", "LOW24HOUR": "285" }, { "TYPE": "2", "MARKET": "WavesDEX", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "2", "PRICE": "297.14", "LASTUPDATE": "1502447804", "LASTVOLUME": "1.2", "LASTVOLUMETO": "356.568", "LASTTRADEID": "7", "VOLUME24HOUR": "2.4", "VOLUME24HOURTO": "734.644", "OPEN24HOUR": "315", "HIGH24HOUR": "315", "LOW24HOUR": "297.14" }, { "TYPE": "2", "MARKET": "BitTrex", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "2", "PRICE": "277.29999978", "LASTUPDATE": "1504585744", "LASTVOLUME": "0.405", "LASTVOLUMETO": "112.30649991", "LASTTRADEID": "7429751", "VOLUME24HOUR": "77882.07716049001", "VOLUME24HOURTO": "22848548.07120349", "OPEN24HOUR": "344", "HIGH24HOUR": "344", "LOW24HOUR": "265.5" }, { "TYPE": "2", "MARKET": "Poloniex", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "2", "PRICE": "274.99972021", "LASTUPDATE": "1504585709", "LASTVOLUME": "6.60045348", "LASTVOLUMETO": "1815.12286025", "LASTTRADEID": "3855562", "VOLUME24HOUR": "196341.07716732976", "VOLUME24HOURTO": "58177469.1726234", "OPEN24HOUR": "342.10963005", "HIGH24HOUR": "344.10984123", "LOW24HOUR": "267" }, { "TYPE": "2", "MARKET": "Gemini", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "2", "PRICE": "262.88", "LASTUPDATE": "1504576699", "LASTVOLUME": "2", "LASTVOLUMETO": "525.76", "LASTTRADEID": "1571120198", "VOLUME24HOUR": "216904.2207455004", "VOLUME24HOURTO": "65026987.65402278", "OPEN24HOUR": "343.89", "HIGH24HOUR": "344.87", "LOW24HOUR": "262.88" }, { "TYPE": "2", "MARKET": "Bitfinex", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "2", "PRICE": "275.71", "LASTUPDATE": "1504585690", "LASTVOLUME": "1.62376255", "LASTVOLUMETO": "447.68757266049994", "LASTTRADEID": "62385134", "VOLUME24HOUR": "644798.5421963095", "VOLUME24HOURTO": "192084082.9579", "OPEN24HOUR": "343.16", "HIGH24HOUR": "344.01", "LOW24HOUR": "267.64" }, { "TYPE": "2", "MARKET": "Cryptsy", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "4", "PRICE": "1.85", "LASTUPDATE": "1452789914", "LASTVOLUME": "1.23007147", "LASTVOLUMETO": "2.27563222", "LASTTRADEID": "110935442", "VOLUME24HOUR": "0", "VOLUME24HOURTO": "0", "OPEN24HOUR": "1.85", "HIGH24HOUR": "1.85", "LOW24HOUR": "1.85" }, { "TYPE": "2", "MARKET": "HitBTC", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "1", "PRICE": "275.31", "LASTUPDATE": "1504585763", "LASTVOLUME": "0.052", "LASTVOLUMETO": "14.31612", "LASTTRADEID": "24783526", "VOLUME24HOUR": "9882.125999999998", "VOLUME24HOURTO": "3036674.5280299988", "OPEN24HOUR": "343.06", "HIGH24HOUR": "343.36", "LOW24HOUR": "270.67" }, { "TYPE": "2", "MARKET": "Tidex", "FROMSYMBOL": "ETH", "TOSYMBOL": "WUSD", "FLAGS": "4", "PRICE": "311.94902967", "LASTUPDATE": "1504584562", "LASTVOLUME": "0.80913154", "LASTVOLUMETO": "252.4077987783928", "LASTTRADEID": "1090807", "VOLUME24HOUR": "78.18033999999997", "VOLUME24HOURTO": "24806.22865919628", "OPEN24HOUR": "336.83947443", "HIGH24HOUR": "338.7302818", "LOW24HOUR": "303.943277" }, { "TYPE": "2", "MARKET": "LiveCoin", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "4", "PRICE": "320", "LASTUPDATE": "1504526194", "LASTVOLUME": "0.004", "LASTVOLUMETO": "1.28", "LASTTRADEID": "157477489", "VOLUME24HOUR": "83.27876504", "VOLUME24HOURTO": "27965.89069719821", "OPEN24HOUR": "354.9", "HIGH24HOUR": "356.9997", "LOW24HOUR": "320" }, { "TYPE": "2", "MARKET": "BitBay", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "2", "PRICE": "303.99", "LASTUPDATE": "1504578523", "LASTVOLUME": "0.14476664", "LASTVOLUMETO": "44.0076108936", "LASTTRADEID": "2490275", "VOLUME24HOUR": "57.29266397000001", "VOLUME24HOURTO": "18637.0921285355", "OPEN24HOUR": "379.47", "HIGH24HOUR": "389.57", "LOW24HOUR": "270" }, { "TYPE": "2", "MARKET": "Yobit", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "1", "PRICE": "295.86577392", "LASTUPDATE": "1504585550", "LASTVOLUME": "0.00035522", "LASTVOLUMETO": "0.10509744021186239", "LASTTRADEID": "133304745", "VOLUME24HOUR": "890.36434062", "VOLUME24HOURTO": "276210.2367078599", "OPEN24HOUR": "354.79518826", "HIGH24HOUR": "354.81", "LOW24HOUR": "289.5003864" }, { "TYPE": "2", "MARKET": "CCEX", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "2", "PRICE": "287.888", "LASTUPDATE": "1504583813", "LASTVOLUME": "0.1467777", "LASTVOLUMETO": "42.2555384976", "LASTTRADEID": "1668309", "VOLUME24HOUR": "112.96267008000002", "VOLUME24HOURTO": "36018.73519999543", "OPEN24HOUR": "355", "HIGH24HOUR": "355", "LOW24HOUR": "287.888" }, { "TYPE": "2", "MARKET": "Gatecoin", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "4", "PRICE": "276.001", "LASTUPDATE": "1504584202", "LASTVOLUME": "2.5563", "LASTVOLUMETO": "705.5413562999998", "LASTTRADEID": "44359310", "VOLUME24HOUR": "1059.0328284700001", "VOLUME24HOURTO": "320734.07302390534", "OPEN24HOUR": "350", "HIGH24HOUR": "374.992", "LOW24HOUR": "275.001" }, { "TYPE": "2", "MARKET": "Quoine", "FROMSYMBOL": "ETH", "TOSYMBOL": "USD", "FLAGS": "2", "PRICE": "298", "LASTUPDATE": "1504548341", "LASTVOLUME": "9.26", "LASTVOLUMETO": "2759.48", "LASTTRADEID": "10156637", "VOLUME24HOUR": "105.25999999999999", "VOLUME24HOURTO": "34724.48", "OPEN24HOUR": "380", "HIGH24HOUR": "380", "LOW24HOUR": "298" }] }, "Type": 100 }, 60000, (key, value) => {
    console.log(key + ' was timed out')
})

console.log(cache.get('ETH-USD'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants