javascript - How can i view all cache in Http in angular js -
i know can use this
var cache = $cachefactory.get('$http'); cache.get('http://example.com/api/users.json'); but want know other data cache has.
i tried
console.log(cache) gives function names not actual data in cache
i tried page
https://docs.angularjs.org/api/ng/service/$cachefactory
in example use keys data
<div ng-repeat="key in keys"> <span ng-bind="key"></span> <span>: </span> <b ng-bind="cache.get(key)"></b> </div> but want keys reside in cache. mean like
cache.getkeys()
if @ internal implementation of $cachefactoryprovider you'll pretty find data variable never exposed in raw state.
as such, real way of getting said data modify angular js source - or decorate provider.
now, i've attempted - "works™", note just proof of concept. take further, and/or implement in fork of angular (better yet, send pr) , run spec suite figure out if , how managed break stuff.
anyway, here - i've tried document steps i've taken best can.
app.config(function ($provide) { /** * store reference original return value * of $cachefactory(cacheid, options). * * used later in referencing 'native' methods. */ var originalcachefactory; $provide.decorator('$cachefactory', function ($delegate) { /** * setup caches holder. */ var caches = {}; /** * dont overwrite original */ if (!originalcachefactory) { originalcachefactory = $delegate.apply(null, arguments); } /** * setup new cachefactory method. * take original return value of $delegate(cacheid, options) * , extend overriden methods. * * `put` should push our data local `data` variable. * `getall()` returns entire data pool given cache. * */ function cachefactory (cacheid, options) { var data = {}; return caches[cacheid] = angular.extend($delegate.apply(null, arguments), { put: function (k, v) { /** * store away values in * our own `data` variable, presented * .getall. */ if (!data[cacheid]) { data[cacheid] = {}; } data[cacheid][k] = v; /** * run original .put method. */ originalcachefactory.put.apply(null, arguments); }, getall: function () { return data[cacheid]; } }); } /** * re-implement $cachefactory.get(id) * method. */ cachefactory.get = function (id) { return caches[id]; }; /** * re-implement $cachefactory.info() * method. */ cachefactory.info = function () { var info = {}; foreach(caches, function(cache, cacheid) { info[cacheid] = cache.info(); }); return info; }; /** * return our new cachefactory function. */ return cachefactory; }); }); ////////////////////////////////////////////////////////////////// /** * log contents of $templatecache. */ app.run(function ($templatecache, $cacheprovider, $timeout) { $timeout(function () { console.log($templatecache.getall()); }); // or $timeout(function () { console.log($cacheprovider.get('templates').getall()); }); }); here's jsbin showcasing end result. pop open console , see console output of 3 stored templates in $templatecache.
with of said , done;
- an issue has been posted @ angular: angular.js#3797
- angular-cache @jmdobry has invented wheel , replaced $cachefactory.
Comments
Post a Comment