root/HTTP/Cache.lua

Revision 1368 (checked in by rsz, 6 months ago)

cleanup

Line 
1 --------------------------------------------------------------------------------
2 -- Title:               Cache.lua
3 -- Description:         Like a square peg in a round hole
4 -- Author:              Raphaël Szwarc http://alt.textdrive.com/lua/
5 -- Creation Date:       January 30, 2007
6 -- Legal:               Copyright (C) 2007 Raphaël Szwarc
7 --                      Under the terms of the MIT License
8 --                      http://www.opensource.org/licenses/mit-license.html
9 --------------------------------------------------------------------------------
10
11 -- import dependencies
12 local io = require( 'io' )
13 local lfs = require( 'lfs' )
14
15 local assert = assert
16 local getmetatable = getmetatable
17 local setmetatable = setmetatable
18
19 --------------------------------------------------------------------------------
20 -- Cache
21 --------------------------------------------------------------------------------
22
23 module( 'Cache' )
24 _VERSION = '1.0'
25
26 local self = setmetatable( _M, {} )
27 local meta = getmetatable( self )
28
29 --------------------------------------------------------------------------------
30 -- Utilities
31 --------------------------------------------------------------------------------
32
33 local function Read( aPath, aContent, aTime )
34     local aReader = assert( io.open( aPath, 'rb' ) )
35    
36     if lfs.lock( aReader, 'r' ) then
37         local aContent = aReader:read( '*a' )
38        
39         lfs.unlock( aReader )
40         aReader:close()
41        
42         return aContent
43     end
44    
45     aReader:close()
46    
47     return self( aPath, aContent, aTime )
48 end
49
50 local function Write( aPath, aContent, aTime )
51     local aWriter = assert( io.open( aPath, 'wb' ) )
52    
53     if lfs.lock( aWriter, 'w' ) then
54         local aContent = aContent()
55        
56         aWriter:write( aContent )
57         aWriter:flush()
58         lfs.unlock( aWriter )
59         aWriter:close()
60        
61         return aContent
62     end
63    
64     aWriter:close()
65    
66     return self( aPath, aContent, aTime )
67 end
68
69 --------------------------------------------------------------------------------
70 -- Metamethods
71 --------------------------------------------------------------------------------
72
73 function meta:__call( aPath, aContent, aTime )
74     local aModification = lfs.attributes( aPath, 'modification' )
75     local aSize = lfs.attributes( aPath, 'size' )
76    
77     if aModification and aModification > aTime
78     and aSize and aSize > 0 then
79         return Read( aPath, aContent, aTime )
80     end
81    
82     return Write( aPath, aContent, aTime )
83 end
84
85 function meta:__concat( aValue )
86     return tostring( self ) .. tostring( aValue )
87 end
88
89 function meta:__tostring()
90     return ( '%s/%s' ):format( self._NAME, self._VERSION )
91 end
Note: See TracBrowser for help on using the browser.