Class: CacheLocal::File
- Inherits:
-
Object
- Object
- CacheLocal::File
- Defined in:
- lib/cache_local/file.rb
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #get(key) ⇒ Object
-
#initialize(path, namespace = nil) ⇒ File
constructor
A new instance of File.
- #set(key, value, expiration_time = 0) ⇒ Object
Constructor Details
#initialize(path, namespace = nil) ⇒ File
Returns a new instance of File.
5 6 7 8 9 10 11 |
# File 'lib/cache_local/file.rb', line 5 def initialize(path, namespace = nil) @path = path @namespace = namespace if namespace FileUtils.mkdir_p(::File.join(path, namespace)) unless FileTest.exist?(::File.join(path, namespace)) end end |
Instance Method Details
#delete(key) ⇒ Object
35 36 37 |
# File 'lib/cache_local/file.rb', line 35 def delete(key) ::File.delete(filename(key)) if ::File.exists?(filename(key)) end |
#get(key) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/cache_local/file.rb', line 19 def get(key) return nil unless ::File.exists?(filename(key)) file = ::File.open(filename(key), 'r') cache = Marshal.load(file) is_expired = cache[:expiration_time] != 0 && Time.now - file.mtime > cache[:expiration_time] file.close if is_expired delete(key) return nil end cache[:value] end |
#set(key, value, expiration_time = 0) ⇒ Object
13 14 15 16 17 |
# File 'lib/cache_local/file.rb', line 13 def set(key, value, expiration_time = 0) file = ::File.open(filename(key), 'w') Marshal.dump({value: value, expiration_time: expiration_time}, file) file.close end |