{"id":18561251,"url":"https://github.com/apostrophecms/cache-on-demand","last_synced_at":"2025-06-14T06:34:01.389Z","repository":{"id":22047984,"uuid":"25376643","full_name":"apostrophecms/cache-on-demand","owner":"apostrophecms","description":"\"On demand\" caching that kicks in only when requests arrive simultaneously.","archived":false,"fork":false,"pushed_at":"2022-02-18T15:56:10.000Z","size":36,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-06-14T05:37:34.384Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/apostrophecms.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-10-17T20:51:23.000Z","updated_at":"2022-02-16T15:23:12.000Z","dependencies_parsed_at":"2022-08-20T19:10:34.226Z","dependency_job_id":null,"html_url":"https://github.com/apostrophecms/cache-on-demand","commit_stats":null,"previous_names":["punkave/cache-on-demand"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/apostrophecms/cache-on-demand","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Fcache-on-demand","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Fcache-on-demand/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Fcache-on-demand/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Fcache-on-demand/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apostrophecms","download_url":"https://codeload.github.com/apostrophecms/cache-on-demand/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Fcache-on-demand/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259774597,"owners_count":22909163,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T22:06:20.889Z","updated_at":"2025-06-14T06:34:01.372Z","avatar_url":"https://github.com/apostrophecms.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cache-on-demand\n\n\u003ca href=\"http://apostrophenow.org/\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/punkave/cache-on-demand/master/logos/logo-box-madefor.png\" align=\"right\" /\u003e\u003c/a\u003e\n\n\"On demand\" caching that kicks in only when requests arrive simultaneously.\n\n```javascript\nvar cacheOnDemand = require('cache-on-demand');\n\n// Find all the things related to a URL.\n// Let's just say this is a slow operation.\n\nfunction getTheStuff(url, callback) {\n  return myDatabase.find({ url: url }).toArray(callback);\n}\n\n// Create a new function with on-demand caching.\n\nvar cachedGetTheStuff = cacheOnDemand(getTheStuff, function(url) {\n  // A URL makes a great hash key\n  return url;\n});\n\n// Call cachedGetTheStuff just like we'd call the original.\n\ngetTheStuff('/welcome', function(err, stuff) {\n  // Hooray, let's loop over the stuff\n});\n```\n\nUnder light load, with calls to `cachedGetTheStuff` taking place separated in time, every request for a given URL will get an individually generated response, which gives them the newest content. Just like calling `getTheStuff` directly.\n\nBut under heavy load, with new requests arriving while the first request is still being processed, the additional requests are queued up. When the first response is ready, it is simply sent to all of them. And then the response is discarded, so that the next request to arrive will generate a new response with the latest content.\n\nThis gives us \"on demand\" caching. The server is still allowed to generate new responses often, just not many of them simultaneously. It is the shortest practical lifetime for cache requests and largely eliminates concerns about users seeing old content, as well as concerns about cache memory management. In fact most users will get *fresher content than they would without the caching*, because the server is not overwhelmed and generating responses slowly.\n\n## Writing your hash function\n\nThe second argument to `cache-on-demand` is a hash function. It receives the same arguments as your original function, except for the callback.\n\nIf this call *should not be cached*, just return `false`.\n\nIf this call *should* be cached, return a string for use as a hash key. All calls with the same hash key that arrive while your worker function is running will get the same response, without calling the function again.\n\n## What about errors?\n\nIf your main function reports an error to its callback, it is reported to the original caller and all of the pending callers as well. In fact, we simply deliver *all of the same arguments* that your worker function passed to its callback.\n\n## Express middleware\n\nThere's an easy way to use this module with web apps powered by Express. Check out [express-cache-on-demand](https://github.com/punkave/express-cache-on-demand).\n\n## About P'unk Avenue and Apostrophe\n\n`cache-on-demand` was created at [P'unk Avenue](http://punkave.com) for use in many projects built with Apostrophe, an open-source content management system built on node.js. If you like `cache-on-demand` you should definitely [check out apostrophenow.org](http://apostrophenow.org).\n\n## Support\n\nFeel free to open issues on [github](http://github.com/punkave/cache-on-demand).\n\n\u003ca href=\"http://punkave.com/\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/punkave/cache-on-demand/master/logos/logo-box-builtby.png\" /\u003e\u003c/a\u003e\n\n## Changelog\n\n### CHANGES IN 1.0.1\n\nEliminated use of `var` and introduced use of `...` to make the code more maintainable. No functional changes.\n\n### CHANGES IN 1.0.0\n\nThis has been in production use for many moons, so we're declaring 1.0.0 stable. Also bumped the dependencies up to modern releases.\n\n### CHANGES IN 0.2.0\n\nMoved all the expressly Express-related stuff to the [express-cache-on-demand](https://github.com/punkave/express-cache-on-demand) module.\n\n### CHANGES IN 0.1.0\n\nInitial release. With shiny unit tests, of course.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapostrophecms%2Fcache-on-demand","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapostrophecms%2Fcache-on-demand","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapostrophecms%2Fcache-on-demand/lists"}