Module: Amazon::Associates::FilesystemCache

Defined in:
lib/amazon-associates/caching/filesystem_cache.rb

Constant Summary collapse

DEFAULT_DISK_QUOTA =

disk quota in megabytes

200
DEFAULT_SWEEP_FREQUENCY =

frequency of sweeping in hours

2

Class Method Summary collapse

Class Method Details

.cache(request_url, response) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/amazon-associates/caching/filesystem_cache.rb', line 19

def self.cache(request_url, response)
  path = self.cache_path
  cached_filename = Digest::SHA1.hexdigest(request_url)
  cached_folder = File.join(path, cached_filename[0..2])

  FileUtils.mkdir_p(cached_folder)

  destination = File.join(cached_folder, cached_filename)
  open(destination, "w") do |cached_file|
    Marshal.dump(response, cached_file)
  end
end

.cache_pathObject



75
76
77
# File 'lib/amazon-associates/caching/filesystem_cache.rb', line 75

def self.cache_path
  @@cache_path
end

.disk_quotaObject



67
68
69
# File 'lib/amazon-associates/caching/filesystem_cache.rb', line 67

def self.disk_quota
  @@disk_quota
end

.get(request) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/amazon-associates/caching/filesystem_cache.rb', line 32

def self.get(request)
  path = self.cache_path
  cached_filename = Digest::SHA1.hexdigest(request)
  file_path = File.join(path, cached_filename[0..2], cached_filename)
  if FileTest.exists?(file_path)
    open(file_path) {|f| Marshal.load(f) }
  end
end

.initialize_options(options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/amazon-associates/caching/filesystem_cache.rb', line 41

def self.initialize_options(options)
  #check for required options
  unless cache_options = options[:caching_options]
    raise ConfigurationError, "You must specify caching options for filesystem caching: :cache_path is required"
  end

  #default disk quota to 200MB
  @@disk_quota = cache_options[:disk_quota] || DEFAULT_DISK_QUOTA

  @@sweep_frequency = cache_options[:sweep_frequency] || DEFAULT_SWEEP_FREQUENCY

  @@cache_path = cache_options[:cache_path]

  if @@cache_path.nil?
    raise ConfigurationError, "You must specify a cache path for filesystem caching"
  end

  if !File.directory?(@@cache_path)
    raise ConfigurationError, "You must specify a valid cache path for filesystem caching"
  end
end

.sweepObject



63
64
65
# File 'lib/amazon-associates/caching/filesystem_cache.rb', line 63

def self.sweep
  self.perform_sweep if must_sweep?
end

.sweep_frequencyObject



71
72
73
# File 'lib/amazon-associates/caching/filesystem_cache.rb', line 71

def self.sweep_frequency
  @@sweep_frequency
end