diff --git a/lib/angel_cache.dart b/lib/angel_cache.dart index faba4152..a0c15fc6 100644 --- a/lib/angel_cache.dart +++ b/lib/angel_cache.dart @@ -1,2 +1,3 @@ export 'src/cache.dart'; -export 'src/cache_service.dart'; \ No newline at end of file +export 'src/cache_service.dart'; +export 'src/serializer.dart'; \ No newline at end of file diff --git a/lib/src/cache.dart b/lib/src/cache.dart index fe4622fb..09f102c5 100644 --- a/lib/src/cache.dart +++ b/lib/src/cache.dart @@ -14,18 +14,25 @@ class ResponseCache { ResponseCache({this.timeout}); - Future handleRequest(RequestContext req, ResponseContext res) async { - var now = new DateTime.now().toUtc(); + /// A middleware that handles requests with an `If-Modified-Since` header. + /// + /// This prevents the server from even having to access the cache, and plays very well with static assets. + Future ifModifiedSince(RequestContext req, ResponseContext res) async { + if (req.headers.value('if-modified-since') != null) { + var modifiedSince = _fmt.parse(req.headers.value('if-modified-since')); - // Check if there is a cache entry. - for (var pattern in patterns) { - if (pattern.allMatches(req.uri.path).isNotEmpty && _cache.containsKey(req.uri.path)) { - var response = _cache[req.uri.path]; + // Check if there is a cache entry. + for (var pattern in patterns) { + if (pattern.allMatches(req.uri.path).isNotEmpty && + _cache.containsKey(req.uri.path)) { + var response = _cache[req.uri.path]; - // Only send a cached response if it is valid. - if (timeout == null || now.difference(response.timestamp) >= timeout) { - // TODO: If-Last-Modified - break; + // Only send a cached response if it is valid. + if (timeout == null || + modifiedSince.difference(response.timestamp) >= timeout) { + res.statusCode = 304; + return false; + } } } } @@ -33,7 +40,10 @@ class ResponseCache { return true; } - Future responseFinalizer(RequestContext req, ResponseContext res) async { + Future responseFinalizer( + RequestContext req, ResponseContext res) async { + if (res.statusCode == 304) return true; + var now = new DateTime.now().toUtc(); } @@ -58,4 +68,4 @@ class _CachedResponse { final DateTime timestamp; _CachedResponse({this.headers, this.body, this.timestamp}); -} \ No newline at end of file +}