Class: EOAT::Cache::FileCache

Inherits:
Object
  • Object
show all
Defined in:
lib/eoat/cache/file_cache.rb

Overview

Store cache to files on disk. Default use ~/.eoat/cache as destination directory.

Examples:

Set FileCache as a cache storage, with default destination directory

EOAT.cache = EOAT::Cache::FileCache.new

Set FileCache as a cache storage, with custom destination directory

EOAT.cache = EOAT::Cache::FileCache.new('path/to/cache/dir')

Author:

Instance Method Summary collapse

Constructor Details

#initialize(path = "#{ENV['HOME']}/.eoat/cache") ⇒ FileCache

Returns a new instance of FileCache.

Parameters:

  • path (String) (defaults to: "#{ENV['HOME']}/.eoat/cache")

    destination path



15
16
17
18
19
# File 'lib/eoat/cache/file_cache.rb', line 15

def initialize(path = "#{ENV['HOME']}/.eoat/cache")
  @path = path.chomp('/')
  FileUtils.mkpath(@path) unless File.exists?(@path)
  perform_cache_dir
end

Instance Method Details

#get(host, uri) ⇒ Object, NilClass

Get object from cache

Parameters:

  • host (String)

    the request host string

  • uri (String)

    the query string

Returns:

  • (Object, NilClass)

    the instance of result class or nil if key not does not exist



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/eoat/cache/file_cache.rb', line 26

def get(host, uri)
  perform_cache_dir
  # Perform host string to md5 string
  host = EOAT::Cache.md5hash(host)
  # Perform uri string to md5 string
  uri = EOAT::Cache.md5hash(uri)
  # Combine parameters to path
  request_path = "#{@path}/#{host}/#{uri}"
  if File.exists?(request_path)
    # Set now in UTC timezone to timestamp
    now = Time.now.utc.to_i
    # Get all directories in request_path
    Dir["#{request_path}/*"].each do |dir|
      # Check timestamp
      if dir.split('/').last.to_i > now
        yaml = File.read("#{dir}/result.yml")
        yaml_hash = File.read("#{dir}/result.md5")
        if EOAT::Cache.md5hash(yaml) == yaml_hash
          return YAML::load(yaml)
        else
          FileUtils.rmtree("#{dir}")
        end
      else
        FileUtils.rmtree("#{dir}")
      end
    end
  end
  # Return false if data not found
  false
end

#save(host, uri, content) ⇒ Object

Save instance of result class.

Parameters:

  • host (String)

    the request host string

  • uri (String)

    the query string

  • content (Object)

    the result class instance



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/eoat/cache/file_cache.rb', line 61

def save(host, uri, content)
  # Check expired
  perform_cache_dir
  # Calculate TTL in seconds
  expire = (content.cached_until - content.request_time).to_i
  # If TTL > EOAT.max_ttl set EOAT.max_tt as expire
  expire = expire > EOAT.max_ttl ? EOAT.max_ttl : expire
  # If 0 or a negative value, it does not save
  if expire > 0
    # Set expired as timestamp of date
    expired = (content.request_time + expire).to_i
    # Perform host string to md5 string
    host = EOAT::Cache.md5hash(host)
    # Perform uri string to md5 string
    uri = EOAT::Cache.md5hash(uri)
    # Combine parameters to path
    save_path = "#{@path}/#{host}/#{uri}/#{expired}"
    # This principle is not possible, but should be checked
    if File.exists?(save_path)
      raise EOAT::Exception::CacheSaveError.new("Save path: #{save_path} already exists!")
    end
    # Create directory
    FileUtils.mkpath(save_path)
    # Save instance to result.yml
    content_yaml = content.to_yaml
    File.write("#{save_path}/result.md5", EOAT::Cache.md5hash(content_yaml))
    File.write("#{save_path}/result.yml", content_yaml)
  end
end