Class: Amazon::AWS::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon/aws/cache.rb

Overview

This class provides a simple results caching system for operations performed by AWS.

To use it, set cache to true in either /etc/amazonrc or ~/.amazonrc.

By default, the cache directory used is /tmp/amazon, but this can be changed by defining cache_dir in either /etc/amazonrc or ~/.amazonrc.

When a cache is used, Ruby/AWS will check the cache directory for a recent copy of a response to the exact operation that you are performing. If found, the cached response will be returned instead of the request being forwarded to the AWS servers for processing. If no (recent) copy is found, the request will be forwarded to the AWS servers as usual. Recency is defined here as less than 24 hours old.

Defined Under Namespace

Classes: PathError

Constant Summary collapse

ONE_DAY =

Length of one day in seconds

86400
MAX_AGE =

Age in days below which to consider cache files valid.

1.0
DEFAULT_CACHE_DIR =

Default cache location.

'/tmp/amazon'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = DEFAULT_CACHE_DIR) ⇒ Cache

Returns a new instance of Cache.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/amazon/aws/cache.rb', line 54

def initialize(path=DEFAULT_CACHE_DIR)
  path ||= DEFAULT_CACHE_DIR

  ::FileUtils::mkdir_p( path ) unless File.exists? path

  unless File.directory? path 
    raise PathError, "cache path #{path} is not a directory"
  end

  unless File.readable? path 
    raise PathError, "cache path #{path} is not readable"
  end

  unless File.writable? path
    raise PathError, "cache path #{path} is not writable"
  end

  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



52
53
54
# File 'lib/amazon/aws/cache.rb', line 52

def path
  @path
end

Instance Method Details

#cached?(url) ⇒ Boolean

Determine whether or not the the response to a given URL is cached. Returns true or false.

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/amazon/aws/cache.rb', line 78

def cached?(url)
  digest = Digest::MD5.hexdigest( url )

  cache_files = Dir.glob( File.join( @path, '*' ) ).map do |d|
    File.basename( d )
  end

  return cache_files.include?( digest ) &&
    ( Time.now - File.mtime( File.join( @path, digest ) ) ) /
    ONE_DAY <= MAX_AGE
end

#fetch(url) ⇒ Object

Retrieve the cached response associated with url.



93
94
95
96
97
98
99
100
101
102
# File 'lib/amazon/aws/cache.rb', line 93

def fetch(url)
  digest = Digest::MD5.hexdigest( url )
  cache_file = File.join( @path, digest )

  return nil unless File.exist? cache_file

  Amazon.dprintf( 'Fetching %s from cache...', digest )
  #File.open( File.join( cache_file ) ).readlines.to_s
  File.read(cache_file)
end

#flush_allObject

This method flushes all files from the cache directory specified in the object’s @path variable.



122
123
124
# File 'lib/amazon/aws/cache.rb', line 122

def flush_all
  FileUtils.rm Dir.glob( File.join( @path, '*' ) )
end

#flush_expiredObject

This method flushes expired files from the cache directory specified in the object’s @path variable.



130
131
132
133
134
135
136
137
138
# File 'lib/amazon/aws/cache.rb', line 130

def flush_expired
  now = Time.now

  expired_files = Dir.glob( File.join( @path, '*' ) ).find_all do |f|
    ( now - File.mtime( f ) ) / ONE_DAY > MAX_AGE
  end

  FileUtils.rm expired_files
end

#store(url, contents) ⇒ Object

Cache the data from contents and associate it with url.



107
108
109
110
111
112
113
114
115
116
# File 'lib/amazon/aws/cache.rb', line 107

def store(url, contents)
  digest = Digest::MD5.hexdigest( url )
  cache_file = File.join( @path, digest )

  Amazon.dprintf( 'Caching %s...', digest )
  contents.force_encoding("UTF-8")
  File.open(cache_file, "w"){ |f|
    f.write contents
  }
end