Permalink
Browse files

#10 : using 'X-RedisCache-expireat' header with a timestamp as value,…

… it's now possible to set an expiration datetime to a cached data.
  • Loading branch information...
1 parent c6e1364 commit 066463c5608d11f23fe083c456a32e08d52698aa @desbouis committed Feb 26, 2012
Showing with 53 additions and 1 deletion.
  1. +18 −1 cachemanager.lua
  2. +35 −0 doc_root/test_expireat.php
View
@@ -1,3 +1,9 @@
+---------------------
+-- --
+-- Cache Manager --
+-- --
+---------------------
+
-- if the method is not GET or HEAD, do not read in redis
ngx.log(ngx.NOTICE, "[LUA] Is the request cacheable ?")
if (ngx.var.request_method ~= "GET" and ngx.var.request_method ~= "HEAD") then
@@ -10,6 +16,7 @@ end
-- parser object that will receive redis responses and parse them into lua objects
local parser = require "redis.parser"
+-- read the response in redis
ngx.log(ngx.NOTICE, "[LUA] Is the response in the redis cache ?")
local redis_read_response = ngx.location.capture("/redis_read", {args = {k = ngx.var.key}})
local res, typ = parser.parse_reply(redis_read_response.body)
@@ -34,18 +41,28 @@ else
-- write the response in redis
if fallback_response.status == ngx.HTTP_OK then
+ -- our header names declaration
+ local ttl_header, expireat_header = "X-RedisCache-ttl", "X-RedisCache-expireat"
+ -- by default, the data is persistent
+ local expire_query = {"PERSIST", ngx.var.key}
ngx.req.set_header("X-RedisCache-time", ngx.http_time(ngx.time()))
ngx.log(ngx.NOTICE, "[LUA] got response from /fallback, uri ",ngx.var.key," must be cached")
-- get the fallback headers
local headers = {}
for k, v in pairs(fallback_response.header) do
table.insert(headers, k..": "..v)
end
+ -- ttl or expiration date
+ if fallback_response.header[ttl_header] ~= nil then
+ expire_query = {"EXPIRE", ngx.var.key, fallback_response.header[ttl_header]}
+ elseif fallback_response.header[expireat_header] ~= nil then
+ expire_query = {"EXPIREAT", ngx.var.key, fallback_response.header[expireat_header]}
+ end
-- redis transaction
local queries = {
{"MULTI"},
{"HMSET", ngx.var.key, "headers", table.concat(headers, "\\n"), "body", fallback_response.body},
- {"EXPIRE", ngx.var.key, fallback_response.header["X-RedisCache-ttl"]},
+ expire_query,
{"EXEC"}
}
-- because of carriage returns in HTML code, we use the redis parser to build the queries
View
@@ -0,0 +1,35 @@
+<?php
+
+$expire_date = '2012-12-31 23:59:59';
+$timestamp = strtotime($expire_date);
+//$timestamp = strtotime('+2 min');
+header("X-RedisCache-expireat: $timestamp");
+
+function setInCache($buffer)
+{
+ $buffer = str_replace('{{{uri}}}', $_SERVER['REQUEST_URI'], $buffer);
+ return $buffer;
+}
+
+ob_start("setInCache");
+
+$server = "nginx server";
+$date = date('Y/m/d H:i:s');
+
+?>
+<html>
+<head>
+<title>Welcome to <?php echo $server; ?>!</title>
+</head>
+<body bgcolor="white" text="black">
+<h1>Welcome to <?php echo $server; ?>!</h1>
+<div>Page url : {{{uri}}}</div>
+<div>This page was generated at <?php echo $date; ?></div>
+<div>This page will expire at <?php echo date("Y-m-d H:i:s", $timestamp); ?> (<?php echo $timestamp; ?>)</div>
+</body>
+</html>
+
+<?php
+$output = ob_get_contents();
+ob_end_flush();
+//var_dump($output);

0 comments on commit 066463c

Please sign in to comment.