Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

feat(cache): Move cache out of core, add a CacheRegister #1165

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions lib/application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import 'package:intl/date_symbol_data_local.dart';
import 'package:di/di.dart';
import 'package:angular/angular.dart';
import 'package:angular/perf/module.dart';
import 'package:angular/cache/module.dart';
import 'package:angular/core/module_internal.dart';
import 'package:angular/core/registry.dart';
import 'package:angular/core_dom/module_internal.dart';
Expand All @@ -94,6 +95,7 @@ import 'package:angular/core_dom/static_keys.dart';
*/
class AngularModule extends Module {
AngularModule() {
install(new CacheModule());
install(new CoreModule());
install(new CoreDomModule());
install(new DirectiveModule());
Expand Down
7 changes: 6 additions & 1 deletion lib/core/cache.dart → lib/cache/cache.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of angular.core_internal;
part of angular.cache;

class CacheStats {
final int capacity;
Expand Down Expand Up @@ -32,10 +32,15 @@ abstract class Cache<K, V> {
/**
* Removes all entries from the cache.
*/
@Deprecated('Use clear() instead')
void removeAll();
int get capacity;
@Deprecated('Use length instead')
int get size;
CacheStats stats();

void clear() => removeAll();
int get length => size;
}


Expand Down
64 changes: 64 additions & 0 deletions lib/cache/cache_register.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
part of angular.cache;

class CacheRegisterStats {
final String name;
int length;

CacheRegisterStats(this.name);
}

@Injectable()
class CacheRegister {
Map<String, dynamic> _caches = {};
List<CacheRegisterStats> _stats = null;

/**
* Registers a cache with the CacheRegister. The [name] is used for in the stats as
* well as a key for [clear].
*/
void registerCache(String name, cache) {
if (_caches.containsKey(name)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Map -> Cache on l. 19 - GH don't let me comment there :(

Copy link
Contributor

Choose a reason for hiding this comment

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

Apparently it should be Map (judging by Interpolate code) - what is the purpose of Cache then ?

throw "Cache [$name] already registered";
}
_caches[name] = cache;

// The stats object needs to be updated.
_stats = null;

}

/**
* A list of caches and their sizes.
*/
List<CacheRegisterStats> get stats {
if (_stats == null) {
_stats = [];
_caches.forEach((k, v) {
_stats.add(new CacheRegisterStats(k));
Copy link
Contributor

Choose a reason for hiding this comment

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

_caches.keys.foreach() ?

});
}

_stats.forEach((CacheRegisterStats stat) {
stat.length = _caches[stat.name].length;
});
return _stats;
}

/**
* Clears one or all the caches. If [name] is omitted, all caches will be cleared.
* Otherwise, only the cache named [name] will be cleared.
*/
void clear([String name]) {
if (name == null) {
_caches.forEach((k, Map v) {
Copy link
Contributor

Choose a reason for hiding this comment

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

_caches.values.forEach ?

v.clear();
});
return;
}
var cache = _caches[name];
if (cache == null) {
return;
}
_caches[name].clear();
Copy link
Contributor

Choose a reason for hiding this comment

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

cache.clear()

}
}
16 changes: 16 additions & 0 deletions lib/cache/module.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
library angular.cache;

import 'dart:collection';
import 'dart:async';

import 'package:di/di.dart';
import 'package:angular/core/annotation_src.dart';

part "cache.dart";
part "cache_register.dart";

class CacheModule extends Module {
CacheModule() {
bind(CacheRegister);
}
}
4 changes: 4 additions & 0 deletions lib/core/interpolate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ part of angular.core_internal;
@Injectable()
class Interpolate implements Function {
var _cache = new HashMap();

Interpolate(CacheRegister cacheRegister) {
cacheRegister.registerCache("Interpolate", _cache);
}
/**
* Compiles markup text into expression.
*
Expand Down
6 changes: 5 additions & 1 deletion lib/core/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ export "package:angular/change_detection/change_detection.dart" show
AvgStopwatch,
FieldGetterFactory;

export "package:angular/cache/module.dart" show
Cache,
CacheRegister,
CacheRegisterStats;

export "package:angular/core_dom/module_internal.dart" show
Animation,
AnimationResult,
BrowserCookies,
Cache,
Compiler,
CompilerConfig,
Cookies,
Expand Down
3 changes: 1 addition & 2 deletions lib/core/module_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:angular/utils.dart';

import 'package:angular/core/annotation_src.dart';

import 'package:angular/cache/module.dart';
import 'package:angular/change_detection/watch_group.dart';
export 'package:angular/change_detection/watch_group.dart';
import 'package:angular/change_detection/ast_parser.dart';
Expand All @@ -24,7 +25,6 @@ import 'package:angular/core/parser/utils.dart';
import 'package:angular/core/registry.dart';
import 'package:angular/core/static_keys.dart';

part "cache.dart";
part "exception_handler.dart";
part "interpolate.dart";
part "scope.dart";
Expand All @@ -36,7 +36,6 @@ class CoreModule extends Module {
bind(ScopeDigestTTL);

bind(MetadataExtractor);
bind(Cache);
bind(ExceptionHandler);
bind(FormatterMap);
bind(Interpolate);
Expand Down
5 changes: 4 additions & 1 deletion lib/core/parser/dynamic_parser.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library angular.core.parser.dynamic_parser;

import 'package:angular/cache/module.dart';
import 'package:angular/core/annotation_src.dart' hide Formatter;
import 'package:angular/core/module_internal.dart' show FormatterMap;

Expand All @@ -24,7 +25,9 @@ class DynamicParser implements Parser<Expression> {
final Lexer _lexer;
final ParserBackend _backend;
final Map<String, Expression> _cache = {};
Copy link
Contributor

Choose a reason for hiding this comment

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

Not directly related to this PR but could this be an HashMap ?

DynamicParser(this._lexer, this._backend);
DynamicParser(this._lexer, this._backend, CacheRegister cacheRegister) {
cacheRegister.registerCache("DynamicParser", _cache);
}

Expression call(String input) {
if (input == null) input = '';
Expand Down
5 changes: 4 additions & 1 deletion lib/core/parser/static_parser.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library angular.core.parser.static_parser;

import 'package:angular/cache/module.dart' show CacheRegister;
import 'package:angular/core/annotation_src.dart' show Injectable;
import 'package:angular/core/module_internal.dart' show FormatterMap;
import 'package:angular/core/parser/parser.dart';
Expand All @@ -17,7 +18,9 @@ class StaticParser implements Parser<Expression> {
final StaticParserFunctions _functions;
final DynamicParser _fallbackParser;
final _cache = new HashMap<String, Expression>();
StaticParser(this._functions, this._fallbackParser);
StaticParser(this._functions, this._fallbackParser, CacheRegister cacheRegister) {
cacheRegister.registerCache("StaticParser", _cache);
}

Expression call(String input) {
if (input == null) input = '';
Expand Down
10 changes: 8 additions & 2 deletions lib/core_dom/module_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'dart:js' as js;
import 'package:di/di.dart';
import 'package:perf_api/perf_api.dart';

import 'package:angular/cache/module.dart';

import 'package:angular/core/annotation.dart';
import 'package:angular/core/annotation_src.dart' show SHADOW_DOM_INJECTOR_NAME;
import 'package:angular/core/module_internal.dart';
Expand Down Expand Up @@ -55,7 +57,11 @@ class CoreDomModule extends Module {
bind(ElementProbe, toValue: null);

// Default to a unlimited-sized TemplateCache
bind(TemplateCache, toFactory: (_) => new TemplateCache());
bind(TemplateCache, toFactory: (i) {
var templateCache = new TemplateCache();
i.getByKey(CACHE_REGISTER_KEY).registerCache("TemplateCache", templateCache);
return templateCache;
});
bind(dom.NodeTreeSanitizer, toImplementation: NullTreeSanitizer);

bind(TextMustache);
Expand All @@ -64,7 +70,7 @@ class CoreDomModule extends Module {
bind(Compiler, toImplementation: TaggingCompiler);
bind(CompilerConfig);

bind(ComponentFactory, toImplementation: ShadowDomComponentFactory);
bind(ComponentFactory, toFactory: (i) => i.getByKey(SHADOW_DOM_COMPONENT_FACTORY_KEY));
bind(ShadowDomComponentFactory);
bind(TranscludingComponentFactory);
bind(Content);
Expand Down
6 changes: 5 additions & 1 deletion lib/core_dom/shadow_dom_component_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ class ShadowDomComponentFactory implements ComponentFactory {

final Map<_ComponentAssetKey, async.Future<dom.StyleElement>> styleElementCache = {};

ShadowDomComponentFactory(this.viewCache, this.http, this.templateCache, this.platform, this.componentCssRewriter, this.treeSanitizer, this.expando, this.config);
ShadowDomComponentFactory(this.viewCache, this.http, this.templateCache, this.platform,
this.componentCssRewriter, this.treeSanitizer, this.expando,
this.config, CacheRegister cacheRegister) {
cacheRegister.registerCache("ShadowDomComponentFactoryStyles", styleElementCache);
}

bind(DirectiveRef ref, directives) =>
new BoundShadowDomComponentFactory(this, ref, directives);
Expand Down
3 changes: 3 additions & 0 deletions lib/core_dom/static_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library angular.core_dom.static_keys;

import 'dart:html' as dom;
import 'package:di/di.dart';
import 'package:angular/cache/module.dart';
import 'package:angular/core/static_keys.dart';
import 'package:angular/core_dom/module_internal.dart';

Expand All @@ -12,6 +13,7 @@ export 'package:angular/core/static_keys.dart';

Key ANIMATE_KEY = new Key(Animate);
Key BOUND_VIEW_FACTORY_KEY = new Key(BoundViewFactory);
Key CACHE_REGISTER_KEY = new Key(CacheRegister);
Key COMPILER_KEY = new Key(Compiler);
Key COMPONENT_CSS_REWRITER_KEY = new Key(ComponentCssRewriter);
Key DIRECTIVE_MAP_KEY = new Key(DirectiveMap);
Expand All @@ -23,6 +25,7 @@ Key NG_ELEMENT_KEY = new Key(NgElement);
Key NODE_ATTRS_KEY = new Key(NodeAttrs);
Key NODE_KEY = new Key(dom.Node);
Key NODE_TREE_SANITIZER_KEY = new Key(dom.NodeTreeSanitizer);
Key SHADOW_DOM_COMPONENT_FACTORY_KEY = new Key(ShadowDomComponentFactory);
Key SHADOW_ROOT_KEY = new Key(dom.ShadowRoot);
Key TEMPLATE_CACHE_KEY = new Key(TemplateCache);
Key TEMPLATE_LOADER_KEY = new Key(TemplateLoader);
Expand Down
4 changes: 3 additions & 1 deletion lib/core_dom/view_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ class ViewCache {
final Compiler compiler;
final dom.NodeTreeSanitizer treeSanitizer;

ViewCache(this.http, this.templateCache, this.compiler, this.treeSanitizer);
ViewCache(this.http, this.templateCache, this.compiler, this.treeSanitizer, CacheRegister cacheRegister) {
cacheRegister.registerCache('viewCache', viewFactoryCache);
}

ViewFactory fromHtml(String html, DirectiveMap directives) {
ViewFactory viewFactory = viewFactoryCache.get(html);
Expand Down
1 change: 1 addition & 0 deletions test/_specs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export 'package:di/dynamic_injector.dart';
export 'package:angular/angular.dart';
export 'package:angular/application.dart';
export 'package:angular/introspection.dart';
export 'package:angular/cache/module.dart';
export 'package:angular/core/annotation.dart';
export 'package:angular/core/registry.dart';
export 'package:angular/core/module_internal.dart';
Expand Down
4 changes: 3 additions & 1 deletion test/angular_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ main() {
var ALLOWED_NAMES = [
"angular.app.AngularModule",
"angular.app.Application",
"angular.cache.Cache",
"angular.cache.CacheRegister",
"angular.cache.CacheRegisterStats",
"angular.core.annotation.ShadowRootAware",
"angular.core.annotation_src.AttachAware",
"angular.core.annotation_src.Component",
Expand Down Expand Up @@ -142,7 +145,6 @@ main() {
"angular.core.dom_internal.ViewCache",
"angular.core.dom_internal.ViewFactory",
"angular.core.dom_internal.ViewPort",
"angular.core_internal.CacheStats",
"angular.core_internal.ExceptionHandler",
"angular.core_internal.Interpolate",
"angular.core_internal.RootScope",
Expand Down
49 changes: 49 additions & 0 deletions test/cache/cache_register_spec.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
library cache_register_spec;

import '../_specs.dart';

main() => describe('CacheRegister', () {
it('should clear caches', (CacheRegister register) {
var map = {'a': 2};
var map2 = {'b': 3};
expect(map.length).toEqual(1);
expect(map2.length).toEqual(1);

register.registerCache('a', map);
register.registerCache('b', map2);
register.clear('a');
expect(map.length).toEqual(0);
expect(map2.length).toEqual(1);

map['a'] = 2;
register.clear();
expect(map.length).toEqual(0);
expect(map2.length).toEqual(0);


});

it('should return stats when empty', (CacheRegister register) {
expect(register.stats).toEqual([]);
});

it('should return correct stats', (CacheRegister register) {
var map = {'a': 2};
var map2 = {'b': 3, 'c': 4};
register.registerCache('a', map);
register.registerCache('b', map2);

expect(register.stats.length).toEqual(2);
if (register.stats[0].name == 'a') {
expect(register.stats[0].length).toEqual(1);
expect(register.stats[1].name).toEqual('b');
expect(register.stats[1].length).toEqual(2);
} else {
expect(register.stats[0].name).toEqual('b');
expect(register.stats[0].length).toEqual(2);
expect(register.stats[1].name).toEqual('a');
expect(register.stats[1].length).toEqual(1);
}

});
});
File renamed without changes.