Skip to content

Instantly share code, notes, and snippets.

@rista404
Created August 8, 2018 16:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rista404/fd7a391728ba07f6125c59eb7877c3f4 to your computer and use it in GitHub Desktop.
// @flow
import cache from 'memory-cache'
import fetch from 'isomorphic-fetch'
const TTL_MILISECONDS: number = 10 * 60 * 1000 // ten minutes
export default async function<T>(url: string, options?: Object): Promise<T> {
const cachedResponse = cache.get(url)
if (cachedResponse) return cachedResponse
// If there is no cached response,
// do the actual call and store the response
return fetch(url, options)
.then(response => response.json())
.then(response => {
cache.put(url, response, TTL_MILISECONDS)
return response
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment