Upon examining the internal workings of $cacheFactoryProvider
, it becomes evident that the data
variable remains inaccessible in its original form.
Therefore, the primary method to access this data involves altering the Angular JS source code or enhancing the provider through decoration.
I have made an attempt at implementing this modification, and while it does "work™", I must emphasize that it is merely a Proof of Concept. It could potentially be extended further or integrated into a fork of Angular (even better, submit a PR) to undergo testing with their specification suite to identify any potential issues.
Below are the documented steps outlining my approach:
app.config(function ($provide) {
// Store the original return value of $cacheFactory(cacheId, options).
var originalCacheFactory;
$provide.decorator('$cacheFactory', function ($delegate) {
// Initialize caches holder.
var caches = {};
// Prevent overwriting the original implementation.
if (!originalCacheFactory) {
originalCacheFactory = $delegate.apply(null, arguments);
}
// Define a new cacheFactory method.
function cacheFactory (cacheId, options) {
var data = {};
return caches[cacheId] = angular.extend($delegate.apply(null, arguments), {
put: function (k, v) {
// Store values in our local 'data' variable accessible via getAll.
if (!data[cacheId]) {
data[cacheId] = {};
}
data[cacheId][k] = v;
// Invoke the original .put method.
originalCacheFactory.put.apply(null, arguments);
},
getAll: function () {
return data[cacheId];
}
});
}
// Re-implement the $cacheFactory.get(id).
cacheFactory.get = function (id) {
return caches[id];
};
// Re-implement the $cacheFactory.info().
cacheFactory.info = function () {
var info = {};
forEach(caches, function(cache, cacheId) {
info[cacheId] = cache.info();
});
return info;
};
// Return the updated cacheFactory function.
return cacheFactory;
});
});
//////////////////////////////////////////////////////////////////
// Log the contents of the $templateCache.
app.run(function ($templateCache, $cacheProvider, $timeout) {
$timeout(function () {
console.log($templateCache.getAll());
});
// Or
$timeout(function () {
console.log($cacheProvider.get('templates').getAll());
});
});
Check out this jsBin showcasing the final outcome. Open the console to view the output displaying three stored templates in the $templateCache.
In conclusion: