Class: Amazon::AWS::Cache
- Inherits:
-
Object
- Object
- Amazon::AWS::Cache
- Defined in:
- lib/ruby-paa/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
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#cached?(url) ⇒ Boolean
Determine whether or not the the response to a given URL is cached.
-
#fetch(url) ⇒ Object
Retrieve the cached response associated with url.
-
#flush_all ⇒ Object
This method flushes all files from the cache directory specified in the object’s @path variable.
-
#flush_expired ⇒ Object
This method flushes expired files from the cache directory specified in the object’s @path variable.
-
#initialize(path = DEFAULT_CACHE_DIR) ⇒ Cache
constructor
A new instance of Cache.
-
#store(url, contents) ⇒ Object
Cache the data from contents and associate it with url.
Constructor Details
#initialize(path = DEFAULT_CACHE_DIR) ⇒ Cache
Returns a new instance of Cache.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ruby-paa/aws/cache.rb', line 55 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
#path ⇒ Object (readonly)
Returns the value of attribute path.
53 54 55 |
# File 'lib/ruby-paa/aws/cache.rb', line 53 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.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ruby-paa/aws/cache.rb', line 79 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.
94 95 96 97 98 99 100 101 102 |
# File 'lib/ruby-paa/aws/cache.rb', line 94 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 end |
#flush_all ⇒ Object
This method flushes all files from the cache directory specified in the object’s @path variable.
119 120 121 |
# File 'lib/ruby-paa/aws/cache.rb', line 119 def flush_all FileUtils.rm Dir.glob( File.join( @path, '*' ) ) end |
#flush_expired ⇒ Object
This method flushes expired files from the cache directory specified in the object’s @path variable.
127 128 129 130 131 132 133 134 135 |
# File 'lib/ruby-paa/aws/cache.rb', line 127 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 |
# File 'lib/ruby-paa/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 ) File.open( cache_file, 'w' ) { |f| f.puts contents } end |