chrisronline / angular-promise-cache Goto Github PK
View Code? Open in Web Editor NEWAngularJS service that provides a generic way to cache promises and ensure all cached promises are resolved correctly
License: MIT License
AngularJS service that provides a generic way to cache promises and ensure all cached promises are resolved correctly
License: MIT License
My problem is when I call function wrapped in promiseCache with different arguments I expect it to be executed once for every unique argument. I'm not if it is a bug or I don't use it incorrectly
Here's example: http://jsfiddle.net/apuchkov/7kcs10ef/
var myApp = angular.module('myApp', ['angular-promise-cache']);
myApp
.factory('myAwesomeModel', function ($http, promiseCache) {
return {
getData: function (id) {
return promiseCache({
args: [id],
promise: function (id) {
$('#log').append('<p>Making HTTP call for id: ' + id + '</p>')
return $http.get('/echo/json/');
}
});
}
};
})
.controller('myAwesomeCtrl', function ($scope, myAwesomeModel) {
myAwesomeModel.getData(1);
myAwesomeModel.getData(1);
myAwesomeModel.getData(2); // I expect a second http call here
myAwesomeModel.getData(2);
});
Hi there, thanks for great plugin.
I'm using promise cache to cache 'currentuser' for instance.
First time I call it, it goes to the server, caches the promise and brings the data. The trouble is, it's going to the server each and every time I call it. It should be caching the resulting data I would've thought. What am I doing wrong?
app.factory('PromiseCacheFactory', function(Resource, promiseCache) {
return {
/* User */
getUser: function(userId) {
return promiseCache({
key: 'user_' + userId,
promise: function() {
return Resource.getUser(userId); // here goes $http call
}
});
},
Would there be a way to delete a cache through promiseCache?
Hi,
Thanks for your work it is really usefull !
However today we noticed a problem with Chrome / Opera, in settings the user may disable the use of local data (avoid site from creating data), in the case this is totally crashing.
On line 36 :
ls = window.localStorage || {
setItem: function() { },
removeItem: function() { },
getItem: function() { }
},
The DOMException should only be caught to avoid troubles (exception is "VM1577:1 Uncaught DOMException: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.").
Hi, first of all great job and thanks for this library!
I wonder if there is a standard way to clear all keys in the cache.
This is useful when some cached data is related to one user, and if the user closes the session you may need to empty the cache instead of going one by one.
For now I just forked the repo and added a dirty hack:
promiseCacheFunction.removeAll = function() {
var keys = Object.keys(memos);
for (var i = keys.length - 1; i >= 0; i--) {
this.remove(keys[i]);
};
};
It looks like the memoize function checks to see if underscore is installed and uses that. Unfortunately, lodash while faster in most other functions actually runs the promise. I was able to get around the issue by saving your default memoize function and based on an opts property I added it will revert the memoize function back to the one you have in the code. So I know that is the issue, I am just going to shut off local storage for my use case for now and if I get more time I will try and do a pull request that fixed the issue.
In the current implementation, the hasOwnProperty is used. Do you see any other way of doing this as to still support IE8?
It's really a regular dependency
https://github.com/chrisronline/angular-promise-cache/blob/master/bower.json#L5
Needs a pass
one thing that probably should be good to add is ability to remove from cache by key mask like
return promiseCache({
promise: function () {... },
key: "srv.getBy:" + args.toString(),
});
....
promiseCache.removeByMask(REGEX);
This assignment statement expires every cache item when one thing expires.
https://github.com/chrisronline/angular-promise-cache/blob/master/angular-promise-cache.js#L150
Hi,
Loving this module, saved me a lot of work.
I was thinking if you would care to improve it with a bit more options that would be extremely helpful.
For example, I am in process of writing app that loads all items from API once you open it, then you click for item detail and after reading it go back to the list. Your module works perfect not to do make excessive calls, but once the ttl is up and I go back to list, it is empty and I have to wait a second to fill up again. That's why I think it would be cool to still display the "old" data and refresh them once the new request is done. Just to assure that the list is never empty.
The local storage is basically for the same reason and mainly because I am wrapping my app in phonegap and publishing it on devices. Local storage would be helpful for offline access and, again, non-empty screens on re-opening the app.
I dont have really much time to spare so Im asking if you would be up for it, otherwise Im gonna go for it once I have more time and create pull request after.
Thanks and keep up great work :D
If request fail, library cache fail callback.
Is any possible way to not cache callback from failed request?
We can add expireOnFailure, but I don't want to remove previous cached data.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.