summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jow@openwrt.org>2008-06-26 20:25:02 +0000
committerJo-Philipp Wich <jow@openwrt.org>2008-06-26 20:25:02 +0000
commit96e11423b32302287a90123b8ead227b80c4bb68 (patch)
tree05af646e40820459acd33c9866afd9344c755cab
parent378ef2da569fc684e98f3d4cd9e4c160286ba423 (diff)
* libs/http: implement caching in http.protocol.date
-rw-r--r--libs/http/luasrc/http/protocol/date.lua62
1 files changed, 36 insertions, 26 deletions
diff --git a/libs/http/luasrc/http/protocol/date.lua b/libs/http/luasrc/http/protocol/date.lua
index d403604945..a7edcfd025 100644
--- a/libs/http/luasrc/http/protocol/date.lua
+++ b/libs/http/luasrc/http/protocol/date.lua
@@ -15,6 +15,10 @@ $Id$
module("luci.http.protocol.date", package.seeall)
+local ucache = { }
+local hcache = { }
+
+
MONTHS = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"
@@ -151,40 +155,46 @@ end
-- Convert a HTTP date to unixtime
function to_unix(date)
- local wd, day, mon, yr, hr, min, sec, tz = date:match(
- "([A-Z][a-z][a-z]), ([0-9]+) " ..
- "([A-Z][a-z][a-z]) ([0-9]+) " ..
- "([0-9]+):([0-9]+):([0-9]+) " ..
- "([A-Z0-9%+%-]+)"
- )
-
- if day and mon and yr and hr and min and sec then
- -- find month
- local month = 1
- for i = 1, 12 do
- if MONTHS[i] == mon then
- month = i
- break
+ if not ucache[date] then
+ local wd, day, mon, yr, hr, min, sec, tz = date:match(
+ "([A-Z][a-z][a-z]), ([0-9]+) " ..
+ "([A-Z][a-z][a-z]) ([0-9]+) " ..
+ "([0-9]+):([0-9]+):([0-9]+) " ..
+ "([A-Z0-9%+%-]+)"
+ )
+
+ if day and mon and yr and hr and min and sec then
+ -- find month
+ local month = 1
+ for i = 1, 12 do
+ if MONTHS[i] == mon then
+ month = i
+ break
+ end
end
- end
- -- convert to epoch time
- return tz_offset(tz) + os.time( {
- year = yr,
- month = month,
- day = day,
- hour = hr,
- min = min,
- sec = sec
- } )
+ -- convert to epoch time
+ ucache[date] = tz_offset(tz) + os.time( {
+ year = yr,
+ month = month,
+ day = day,
+ hour = hr,
+ min = min,
+ sec = sec
+ } )
+ end
end
- return 0
+ return ucache[date] or 0
end
-- Convert a unixtime to HTTP date
function to_http(time)
- return os.date( "%a, %d %b %Y %H:%M:%S GMT", time )
+ if not hcache[time] then
+ hcache[time] = os.date( "%a, %d %b %Y %H:%M:%S GMT", time )
+ end
+
+ return hcache[time]
end
-- Compare two dates