Class: Resque::Reports::CacheFile
Overview
Class describes how to storage and access cache file NOTE: Every time any cache file is opening,
cache is cleared from old files.
Constant Summary
collapse
- DEFAULT_EXPIRE_TIME =
86_400
- DEFAULT_CODING =
UTF8
Extensions::Encodings::CP1251, Extensions::Encodings::UTF8
Instance Method Summary
collapse
Constructor Details
#initialize(dir, filename, options = {}) ⇒ CacheFile
Returns a new instance of CacheFile.
14
15
16
17
18
19
20
21
22
|
# File 'lib/resque/reports/cache_file.rb', line 14
def initialize(dir, filename, options = {})
@dir = dir
@filename = File.join(dir, filename)
@ext = File.extname(filename)
@coding = options[:coding] || DEFAULT_CODING
@expiration_time = options[:expire_in] || DEFAULT_EXPIRE_TIME
end
|
Instance Method Details
#clear ⇒ Object
48
49
50
|
# File 'lib/resque/reports/cache_file.rb', line 48
def clear
FileUtils.rm_f(@filename)
end
|
#exists? ⇒ Boolean
Also known as:
ready?
24
25
26
|
# File 'lib/resque/reports/cache_file.rb', line 24
def exists?
!expired?(@filename)
end
|
#filename ⇒ Object
29
30
31
32
|
# File 'lib/resque/reports/cache_file.rb', line 29
def filename
fail 'File doesn\'t exists, check exists? before' unless exists?
@filename
end
|
#open(force = false) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/resque/reports/cache_file.rb', line 34
def open(force = false)
prepare_cache_dir
(force ? clear : return) if File.exists?(@filename)
with_tempfile do |tempfile|
yield tempfile
tempfile.close
FileUtils.cp(tempfile.path, @filename)
FileUtils.chmod(0644, @filename)
end
end
|