Class: Cachetastic::Adapters::File

Inherits:
FileBase show all
Defined in:
lib/gems/cachetastic-2.1.2/lib/cachetastic/adapters/file.rb

Overview

This adapter uses the file system as it’s backing. The configuration for this should look something like this:

my_awesome_cache_options:
  debug: false
  adapter: file
  marshall_method: none
  default_expiry: <%= 24.hours %>
  store_options:  
    dir: /usr/local/caches/
  logging:
    logger_1:
      type: file
      file: log/file_store_cache.log

Instance Attribute Summary

Attributes inherited from FileBase

#directory, #hashed_keys

Attributes inherited from Base

#logger, #name

Instance Method Summary collapse

Methods inherited from FileBase

#delete, #expire_all, #setup, #stats, #valid?

Methods inherited from Base

configuration, #configuration, #debug?, #initialize, #stats

Constructor Details

This class inherits a constructor from Cachetastic::Adapters::Base

Instance Method Details

#get(key) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/adapters/file.rb', line 16

def get(key)
  full_path = full_path_from_dir(get_key_directoy(key, false))
  return nil unless File.exists?(full_path)
  so = YAML::load(File.open(full_path).read)
  if so
    if so.invalid?
      self.delete(key)
      return nil
    end
    if so.value.is_a?(YAML::Object)
      require so.value.class.underscore
      so = YAML::load(File.open(full_path).read)
    end
    return so.value
  end
  return nil
end

#set(key, value, expiry = 0) ⇒ Object



34
35
36
37
38
39
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/adapters/file.rb', line 34

def set(key, value, expiry = 0)
  so = Cachetastic::Adapters::StoreObject.new(key.to_s, value, expiry)
  File.open(full_path_from_key(key), "w") do |f|
    f.puts YAML.dump(so)
  end
end