From 3fdb43ec50f6d491df9c76cea30679334553538c Mon Sep 17 00:00:00 2001 From: Cory Forward Date: Tue, 4 Apr 2017 17:05:16 -0600 Subject: [PATCH] Production Cache 1. View files are cached in production mode 2. Fixed test/all_tests.dart path issue --- lib/angel_mustache.dart | 27 +++++++--------- lib/src/cache.dart | 71 +++++++++++++++++++++++++++++++++++++++++ test/all_tests.dart | 2 +- 3 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 lib/src/cache.dart diff --git a/lib/angel_mustache.dart b/lib/angel_mustache.dart index 6688a2cd..d26d8db1 100644 --- a/lib/angel_mustache.dart +++ b/lib/angel_mustache.dart @@ -3,33 +3,28 @@ library angel_mustache; import 'dart:io'; import 'package:angel_framework/angel_framework.dart'; import 'package:mustache4dart/mustache4dart.dart' show render; +import 'package:angel_mustache/src/cache.dart'; mustache(Directory viewsDirectory, {String fileExtension: '.mustache', String partialsPath: './partials'}) { + Directory partialsDirectory = new Directory.fromUri( - viewsDirectory.uri.resolve(partialsPath)); + viewsDirectory.uri.resolve(partialsPath)); + + MustacheCacheController cache = new MustacheCacheController( + viewsDirectory, partialsDirectory, fileExtension); + return (Angel app) async { app.viewGenerator = (String name, [Map data]) async { var partialsProvider; partialsProvider = (String name) { - String viewPath = name + fileExtension; - File viewFile = new File.fromUri( - partialsDirectory.absolute.uri.resolve(viewPath)); - if (viewFile.existsSync()) { - return render(viewFile.readAsStringSync(), data ?? {}, + String template = cache.get_partial(name, app); + return render(template, data ?? {}, partial: partialsProvider); - } else throw new FileSystemException( - 'View "$name" was not found.', viewPath); }; - String viewPath = name + fileExtension; - File viewFile = new File.fromUri( - viewsDirectory.absolute.uri.resolve(viewPath)); - if (await viewFile.exists()) { - return render(await viewFile.readAsString(), data ?? {}, - partial: partialsProvider); - } else throw new FileSystemException( - 'View "$name" was not found.', viewPath); + String viewTemplate = await cache.get_view(name, app); + return render(viewTemplate, data ?? {}, partial: partialsProvider); }; }; } \ No newline at end of file diff --git a/lib/src/cache.dart b/lib/src/cache.dart new file mode 100644 index 00000000..7d58336d --- /dev/null +++ b/lib/src/cache.dart @@ -0,0 +1,71 @@ +import 'dart:io'; +import 'dart:collection'; + +import 'package:angel_framework/angel_framework.dart'; + +class MustacheCacheController { + HashMap viewCache = new HashMap(); + HashMap partialCache = new HashMap(); + + /** + * The directory of the mustache views + */ + Directory viewDirectory; + + /** + * The directory of mustache partials + */ + Directory partialsDirectory; + + /** + * Default file extension associated with a view file + */ + String fileExtension; + + MustacheCacheController( + [this.viewDirectory, this.partialsDirectory, this.fileExtension]); + + get_view(String viewName, Angel app) async { + if (app.isProduction) { + if (viewCache.containsKey(viewName)) { + return viewCache[viewName]; + } + } + + String viewPath = viewName + this.fileExtension; + File viewFile = + new File.fromUri(this.viewDirectory.absolute.uri.resolve(viewPath)); + + if (await viewFile.exists()) { + String viewTemplate = await viewFile.readAsString(); + if (app.isProduction) { + this.viewCache[viewName] = viewTemplate; + } + return viewTemplate; + } else + throw new FileSystemException( + 'View "$viewName" was not found.', viewPath); + } + + get_partial(String partialName, Angel app) { + if (app.isProduction) { + if (partialCache.containsKey(partialName)) { + return partialCache[partialName]; + } + } + + String viewPath = partialName + this.fileExtension; + File viewFile = + new File.fromUri(partialsDirectory.absolute.uri.resolve(viewPath)); + + if (viewFile.existsSync()) { + String partialTemplate = viewFile.readAsStringSync(); + if (app.isProduction) { + this.partialCache[partialName] = partialTemplate; + } + return partialTemplate; + } else + throw new FileSystemException( + 'View "$partialName" was not found.', viewPath); + } +} diff --git a/test/all_tests.dart b/test/all_tests.dart index 96619326..f5f77a35 100644 --- a/test/all_tests.dart +++ b/test/all_tests.dart @@ -6,7 +6,7 @@ import 'package:test/test.dart'; main() async { Angel angel = new Angel(); - await angel.configure(mustache(new Directory('/test'))); + await angel.configure(mustache(new Directory('./test'))); test('can render templates', () async { var hello = await angel.viewGenerator('hello', {'name': 'world'});