Giter Site home page Giter Site logo

kevduc / read-cached-file Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 421 KB

Cache the result of a usually slow and/or resource/time consuming operation into a local file.

Home Page: https://kevduc.github.io/read-cached-file/

License: MIT License

JavaScript 100.00%
read cache file cached network-requests slow speed-up resource-consumption time-consuming save

read-cached-file's Introduction

readCachedFile

Cache the result of a usually slow and/or resource/time consuming operation (e.g. response from a server request, heavy computation) into a local file.

๐Ÿ“– Example

import readCachedFile from 'read-cached-file'

// Callback to retrieve the data
const getPhotoData = async () => await (await fetch('https://jsonplaceholder.typicode.com/photos')).text()

// Run script once: calls getPhotoData and creates photos-data.json
// Run a second time: no network request, reads from photos-data.json
const data = await readCachedFile("./photos-data.json", getPhotoData)

// Do something with the data
const photos = JSON.parse(data)

๐Ÿ’ก Tips

  • To force retrieving/re-generating the data, set forceUpdate to true:
data = await readCachedFile("./photos-data.json", getPhotoData, true)

read-cached-file's People

Contributors

kevduc avatar

Watchers

 avatar

read-cached-file's Issues

JSON support

Add an option to read/write cached file as JSON.
To avoid stingifying an object to return from the callback and then parsing the output of the function => unecessary conversion from/to JSON string

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.