Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

refactor($cacheFactory): cache inefficient when no capacity #6226

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 29 additions & 22 deletions src/ng/cacheFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*
* @description
* Factory that constructs cache objects and gives access to them.
*
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's with the whitespace changes? I'm not sure they're bad changes, but they might not belong in the diff

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. It was my fault. The editor settings of my WebStorm were like this: "strip trailing spaces on save: all". I have reverted the whitespace changes.

* <pre>
*
*
* var cache = $cacheFactory('cacheId');
* expect($cacheFactory.get('cacheId')).toBe(cache);
* expect($cacheFactory.get('noSuchCacheId')).not.toBeDefined();
Expand All @@ -17,8 +17,8 @@
* cache.put("another key", "another value");
*
* // We've specified no options on creation
* expect(cache.info()).toEqual({id: 'cacheId', size: 2});
*
* expect(cache.info()).toEqual({id: 'cacheId', size: 2});
*
* </pre>
*
*
Expand Down Expand Up @@ -59,9 +59,11 @@ function $CacheFactoryProvider() {
return caches[cacheId] = {

put: function(key, value) {
var lruEntry = lruHash[key] || (lruHash[key] = {key: key});
if (capacity < Number.MAX_VALUE) {
var lruEntry = lruHash[key] || (lruHash[key] = {key: key});

refresh(lruEntry);
refresh(lruEntry);
}

if (isUndefined(value)) return;
if (!(key in data)) size++;
Expand All @@ -76,26 +78,31 @@ function $CacheFactoryProvider() {


get: function(key) {
var lruEntry = lruHash[key];
if (capacity < Number.MAX_VALUE) {
var lruEntry = lruHash[key];

if (!lruEntry) return;
if (!lruEntry) return;

refresh(lruEntry);
refresh(lruEntry);
}

return data[key];
},


remove: function(key) {
var lruEntry = lruHash[key];
if (capacity < Number.MAX_VALUE) {
var lruEntry = lruHash[key];

if (!lruEntry) return;

if (!lruEntry) return;
if (lruEntry == freshEnd) freshEnd = lruEntry.p;
if (lruEntry == staleEnd) staleEnd = lruEntry.n;
link(lruEntry.n,lruEntry.p);

if (lruEntry == freshEnd) freshEnd = lruEntry.p;
if (lruEntry == staleEnd) staleEnd = lruEntry.n;
link(lruEntry.n,lruEntry.p);
delete lruHash[key];
}

delete lruHash[key];
delete data[key];
size--;
},
Expand Down Expand Up @@ -201,7 +208,7 @@ function $CacheFactoryProvider() {
* The first time a template is used, it is loaded in the template cache for quick retrieval. You
* can load templates directly into the cache in a `script` tag, or by consuming the
* `$templateCache` service directly.
*
*
* Adding via the `script` tag:
* <pre>
* <html ng-app>
Expand All @@ -213,29 +220,29 @@ function $CacheFactoryProvider() {
* ...
* </html>
* </pre>
*
*
* **Note:** the `script` tag containing the template does not need to be included in the `head` of
* the document, but it must be below the `ng-app` definition.
*
*
* Adding via the $templateCache service:
*
*
* <pre>
* var myApp = angular.module('myApp', []);
* myApp.run(function($templateCache) {
* $templateCache.put('templateId.html', 'This is the content of the template');
* });
* </pre>
*
*
* To retrieve the template later, simply use it in your HTML:
* <pre>
* <div ng-include=" 'templateId.html' "></div>
* </pre>
*
*
* or get it via Javascript:
* <pre>
* $templateCache.get('templateId.html')
* </pre>
*
*
* See {@link ng.$cacheFactory $cacheFactory}.
*
*/
Expand Down