class CMSScanner::Cache::FileStore

Cache Implementation using files

Attributes

serializer[R]
storage_path[R]

Public Class Methods

new(storage_path, serializer = Marshal) click to toggle source

The serializer must have the 2 methods load and dump

(Marshal and YAML have them)

YAML is Human Readable, contrary to Marshal which store in a binary format Marshal does not need any “require”

@param [ String ] storage_path @param [ Constant ] serializer

# File lib/cms_scanner/cache/file_store.rb, line 16
def initialize(storage_path, serializer = Marshal)
  @storage_path = File.expand_path(storage_path)
  @serializer   = serializer

  FileUtils.mkdir_p(@storage_path) unless Dir.exist?(@storage_path)
end

Public Instance Methods

clean() click to toggle source

TODO: rename this to clear ?

# File lib/cms_scanner/cache/file_store.rb, line 24
def clean
  Dir[File.join(storage_path, '*')].each do |f|
    File.delete(f) unless File.symlink?(f)
  end
end
entry_expiration_path(key) click to toggle source

@param [ String ] key

@return [ String ] The expiration file path associated to the key

# File lib/cms_scanner/cache/file_store.rb, line 61
def entry_expiration_path(key)
  "#{entry_path(key)}.expiration"
end
entry_path(key) click to toggle source

@param [ String ] key

@return [ String ] The file path associated to the key

# File lib/cms_scanner/cache/file_store.rb, line 54
def entry_path(key)
  File.join(storage_path, key)
end
read_entry(key) click to toggle source

@param [ String ] key

@return [ Mixed ]

# File lib/cms_scanner/cache/file_store.rb, line 33
def read_entry(key)
  return if expired_entry?(key)

  serializer.load(File.read(entry_path(key)))
rescue StandardError
  nil
end
write_entry(key, data_to_store, cache_ttl) click to toggle source

@param [ String ] key @param [ Mixed ] data_to_store @param [ Integer ] cache_ttl

# File lib/cms_scanner/cache/file_store.rb, line 44
def write_entry(key, data_to_store, cache_ttl)
  return unless cache_ttl.to_i.positive?

  File.write(entry_path(key), serializer.dump(data_to_store))
  File.write(entry_expiration_path(key), Time.now.to_i + cache_ttl)
end

Private Instance Methods

expired_entry?(key) click to toggle source

@param [ String ] key

@return [ Boolean ]

# File lib/cms_scanner/cache/file_store.rb, line 70
def expired_entry?(key)
  File.read(entry_expiration_path(key)).to_i <= Time.now.to_i
rescue StandardError
  true
end