Production Cache

1. View files are cached in production mode
2. Fixed test/all_tests.dart path issue
This commit is contained in:
Cory Forward 2017-04-04 17:05:16 -06:00
parent 8a1b2c0dac
commit 3fdb43ec50
3 changed files with 83 additions and 17 deletions

View file

@ -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);
};
};
}

71
lib/src/cache.dart Normal file
View file

@ -0,0 +1,71 @@
import 'dart:io';
import 'dart:collection';
import 'package:angel_framework/angel_framework.dart';
class MustacheCacheController {
HashMap<String, String> viewCache = new HashMap();
HashMap<String, String> 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);
}
}

View file

@ -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'});