Class: Picombo::Cache_File
- Inherits:
-
Object
- Object
- Picombo::Cache_File
- Defined in:
- lib/classes/cache/file.rb
Overview
Cache_File Class
Handles driver level logic for file based caching.
Not used by end users. Should only be called from main cache class
Constant Summary collapse
- @@config =
nil
Instance Method Summary collapse
-
#delete(keys) ⇒ Object
Deletes a cache entry.
-
#delete_all ⇒ Object
Deletes all cache entries.
-
#exists(keys) ⇒ Object
Finds an array of files that exist for a specified key.
-
#expired(file) ⇒ Object
Determines if a cache entry is expired or not.
-
#get(keys, single = false) ⇒ Object
Retreives a cached item or items.
-
#initialize(config) ⇒ Cache_File
constructor
Constructor.
-
#set(items, lifetime) ⇒ Object
Writes a config value to a file.
Constructor Details
#initialize(config) ⇒ Cache_File
Constructor.
11 12 13 14 15 |
# File 'lib/classes/cache/file.rb', line 11 def initialize(config) @@config = config raise "Cache directory does not exist: "+::APPPATH+'cache' unless File.directory?(::APPPATH+'cache/') raise "Cache directory not writable: "+::APPPATH+'cache' unless File.writable?(::APPPATH+'cache/') end |
Instance Method Details
#delete(keys) ⇒ Object
Deletes a cache entry
73 74 75 76 77 |
# File 'lib/classes/cache/file.rb', line 73 def delete(keys) exists(keys).each do |file| File.unlink(file) end end |
#delete_all ⇒ Object
Deletes all cache entries
80 81 82 83 84 |
# File 'lib/classes/cache/file.rb', line 80 def delete_all exists(true).each do |file| File.unlink(file) end end |
#exists(keys) ⇒ Object
Finds an array of files that exist for a specified key
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/classes/cache/file.rb', line 18 def exists(keys) return Dir.glob(::APPPATH+'cache/*~*') if keys == true paths = [] [*keys].each do |key| Dir.glob(::APPPATH+'cache/'+key.to_s+'~*').each do |path| paths.push(path) end end paths.uniq end |
#expired(file) ⇒ Object
Determines if a cache entry is expired or not
87 88 89 90 |
# File 'lib/classes/cache/file.rb', line 87 def expired(file) expires = file[file.index('~') + 1, file.length] return (expires != 0 and expires.to_i <= Time.now.to_i); end |
#get(keys, single = false) ⇒ Object
Retreives a cached item or items
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/classes/cache/file.rb', line 47 def get(keys, single = false) items = [] exists(keys).each do |file| if expired(file) next end contents = '' File.open(file, "r") do |infile| while (line = infile.gets) contents+=line+"\n" end end items.push(Marshal.load(contents)) end items.uniq! if single return items.length > 0 ? items.shift : nil else return items end end |
#set(items, lifetime) ⇒ Object
Writes a config value to a file
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/classes/cache/file.rb', line 32 def set(items, lifetime) lifetime+=Time.now.to_i success = true items.each do |key, value| delete(key) File.open(::APPPATH+'cache/'+key.to_s+'~'+lifetime.to_s, 'w') {|f| f.write(Marshal.dump(value)) } end return true end |