Class: AmazonAssociate::CachingStrategy::Filesystem

Inherits:
Base
  • Object
show all
Defined in:
lib/amazon_associate/caching_strategy/filesystem.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, response) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/amazon_associate/caching_strategy/filesystem.rb', line 13

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

  FileUtils.mkdir_p(File.join(path, cached_folder, cached_folder))

  cached_file = File.open(File.join(path, cached_folder, cached_filename), "w")
  cached_file.puts response.doc.to_s
  cached_file.close
end

.cache_pathObject



68
69
70
# File 'lib/amazon_associate/caching_strategy/filesystem.rb', line 68

def self.cache_path
  @@cache_path
end

.disk_quotaObject



60
61
62
# File 'lib/amazon_associate/caching_strategy/filesystem.rb', line 60

def self.disk_quota
  @@disk_quota
end

.get(request) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/amazon_associate/caching_strategy/filesystem.rb', line 25

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)
    File.read(file_path).chomp
  else
    nil
  end
end

.initialize_options(options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/amazon_associate/caching_strategy/filesystem.rb', line 36

def self.initialize_options(options)      
  #check for required options
  if options[:caching_options].nil?
    raise AmazonAssociate::ConfigurationError, "You must specify caching options for filesystem caching: :cache_path is required"
  end

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

  @@sweep_frequency = options[:caching_options][:sweep_frequency] || DEFAULT_SWEEP_FREQUENCY

  @@cache_path = options[:caching_options][:cache_path]

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

  return options
end

.sweepObject



56
57
58
# File 'lib/amazon_associate/caching_strategy/filesystem.rb', line 56

def self.sweep
  self.perform_sweep if must_sweep?
end

.sweep_frequencyObject



64
65
66
# File 'lib/amazon_associate/caching_strategy/filesystem.rb', line 64

def self.sweep_frequency
  @@sweep_frequency
end