Class: Itrigga::Cache::Filecache
- Inherits:
-
Object
- Object
- Itrigga::Cache::Filecache
- Includes:
- Singleton
- Defined in:
- lib/itrigga/cache/filecache.rb
Instance Attribute Summary collapse
-
#cache_dir ⇒ Object
Returns the value of attribute cache_dir.
-
#enabled ⇒ Object
Returns the value of attribute enabled.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
- #delete(key, opts = {}) ⇒ Object
- #expired?(key, opts = {}) ⇒ Boolean
- #file_path(key, opts = {}) ⇒ Object
- #get(key, opts = {}) ⇒ Object
- #is_in_cache?(key, opts = {}) ⇒ Boolean
- #set(key, content, opts = {}) ⇒ Object
Instance Attribute Details
#cache_dir ⇒ Object
Returns the value of attribute cache_dir.
20 21 22 |
# File 'lib/itrigga/cache/filecache.rb', line 20 def cache_dir @cache_dir end |
#enabled ⇒ Object
Returns the value of attribute enabled.
20 21 22 |
# File 'lib/itrigga/cache/filecache.rb', line 20 def enabled @enabled end |
#timeout ⇒ Object
Returns the value of attribute timeout.
20 21 22 |
# File 'lib/itrigga/cache/filecache.rb', line 20 def timeout @timeout end |
Class Method Details
.setup!(opts = {}) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/itrigga/cache/filecache.rb', line 22 def self.setup!(opts = {}) instance.cache_dir = opts[:cache_dir] || "#{defined?(RAILS_ROOT) ? RAILS_ROOT : ''}/tmp/cache" instance.timeout = opts[:timeout] || 3600 instance.enabled = true instance end |
Instance Method Details
#delete(key, opts = {}) ⇒ Object
42 43 44 |
# File 'lib/itrigga/cache/filecache.rb', line 42 def delete(key, opts = {}) File.delete( file_path(key, opts)) if is_in_cache?(key, opts) end |
#expired?(key, opts = {}) ⇒ Boolean
54 55 56 57 58 |
# File 'lib/itrigga/cache/filecache.rb', line 54 def expired?(key, opts={} ) return true unless is_in_cache?(key, opts) opts[:timeout] ||= timeout Time.now - File.mtime(file_path(key, opts)) > opts[:timeout].to_i end |
#file_path(key, opts = {}) ⇒ Object
46 47 48 |
# File 'lib/itrigga/cache/filecache.rb', line 46 def file_path(key, opts = {}) File.(File.join(opts[:cache_dir] || cache_dir, key )) end |
#get(key, opts = {}) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/itrigga/cache/filecache.rb', line 29 def get(key, opts = {}) if expired?(key, opts) delete(key, opts) nil else File.open( file_path(key, opts), 'r' ){ |f| f.read } if is_in_cache?(key, opts) end end |
#is_in_cache?(key, opts = {}) ⇒ Boolean
50 51 52 |
# File 'lib/itrigga/cache/filecache.rb', line 50 def is_in_cache?(key, opts={} ) File.exists?( file_path(key, opts) ) end |
#set(key, content, opts = {}) ⇒ Object
38 39 40 |
# File 'lib/itrigga/cache/filecache.rb', line 38 def set(key, content, opts = {}) File.open( file_path(key, opts), 'w' ){ |f| f.write(content) } end |