Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nsisodiya/api-cacher
Most simple way to add caching in Promise based API in JavaScript
https://github.com/nsisodiya/api-cacher
Last synced: 28 days ago
JSON representation
Most simple way to add caching in Promise based API in JavaScript
- Host: GitHub
- URL: https://github.com/nsisodiya/api-cacher
- Owner: nsisodiya
- License: mit
- Created: 2016-06-09T11:08:14.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-10-28T13:42:25.000Z (about 8 years ago)
- Last Synced: 2024-11-20T16:11:49.288Z (about 1 month ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# api-cacher
Most simple way to add caching in Promise based API in JavaScript# What is this ?
We normally query server data using `XHR requests`, and we use `Promises` for making `XHR requests`!This tiny library add caching layer for `Promises`.
# Example```js
function printUsers(){
User.findAll().then(function(data){
console.log("Users are ", data);
})
}
```
Above function will print users users !But, What if your code will call this `printUsers` method again and again ?
```js
printUsers();
printUsers();
printUsers();
//3 XHR Requests !
```Solution is apiCacher library !
```js
User.findAllCached = apiCacher({
cache: true,
cacheHash: function() {
return "all";
},
request: function() {
return User.findAll();
}
});
function printUsers(){
User.findAllCached().then(function(data){
console.log("Users are ", data);
})
}
printUsers();
printUsers();
printUsers();
//just 1 XHR Requests !
```
# Bonus Examples
using cacheHash you can set cache key.```js
User.findByIdCached = apiCacher({
cache: true,
cacheHash: function(reqObj) {
return reqObj.id;
},
request: function(reqObj) {
return User.findById(reqObj);
}
});
```#License
MIT