Merge pull request #2 from c4wrd/master

Production Cache Implementation
This commit is contained in:
Tobe O 2017-04-04 23:09:26 -04:00 committed by GitHub
commit a1243aeac7
3 changed files with 83 additions and 17 deletions

View file

@ -3,33 +3,28 @@ library angel_mustache;
import 'dart:io'; import 'dart:io';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:mustache4dart/mustache4dart.dart' show render; import 'package:mustache4dart/mustache4dart.dart' show render;
import 'package:angel_mustache/src/cache.dart';
mustache(Directory viewsDirectory, mustache(Directory viewsDirectory,
{String fileExtension: '.mustache', String partialsPath: './partials'}) { {String fileExtension: '.mustache', String partialsPath: './partials'}) {
Directory partialsDirectory = new Directory.fromUri( Directory partialsDirectory = new Directory.fromUri(
viewsDirectory.uri.resolve(partialsPath)); viewsDirectory.uri.resolve(partialsPath));
MustacheCacheController cache = new MustacheCacheController(
viewsDirectory, partialsDirectory, fileExtension);
return (Angel app) async { return (Angel app) async {
app.viewGenerator = (String name, [Map data]) async { app.viewGenerator = (String name, [Map data]) async {
var partialsProvider; var partialsProvider;
partialsProvider = (String name) { partialsProvider = (String name) {
String viewPath = name + fileExtension; String template = cache.get_partial(name, app);
File viewFile = new File.fromUri( return render(template, data ?? {},
partialsDirectory.absolute.uri.resolve(viewPath));
if (viewFile.existsSync()) {
return render(viewFile.readAsStringSync(), data ?? {},
partial: partialsProvider); partial: partialsProvider);
} else throw new FileSystemException(
'View "$name" was not found.', viewPath);
}; };
String viewPath = name + fileExtension; String viewTemplate = await cache.get_view(name, app);
File viewFile = new File.fromUri( return render(viewTemplate, data ?? {}, partial: partialsProvider);
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);
}; };
}; };
} }

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 { main() async {
Angel angel = new Angel(); Angel angel = new Angel();
await angel.configure(mustache(new Directory('/test'))); await angel.configure(mustache(new Directory('./test')));
test('can render templates', () async { test('can render templates', () async {
var hello = await angel.viewGenerator('hello', {'name': 'world'}); var hello = await angel.viewGenerator('hello', {'name': 'world'});