Class Amazon::AWS::Cache
In: lib/amazon/aws/cache.rb
Parent: Object

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.

Methods

cached?   fetch   flush_all   flush_expired   new   store  

Classes and Modules

Class Amazon::AWS::Cache::PathError

Constants

MAX_AGE = 1.0   Age in days below which to consider cache files valid.
DEFAULT_CACHE_DIR = '/tmp/amazon'   Default cache location.

Attributes

path  [R] 

Public Class methods

[Source]

# File lib/amazon/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

Public Instance methods

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

[Source]

# File lib/amazon/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

Retrieve the cached response associated with url.

[Source]

# File lib/amazon/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

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

[Source]

# File lib/amazon/aws/cache.rb, line 119
      def flush_all
        FileUtils.rm Dir.glob( File.join( @path, '*' ) )
      end

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

[Source]

# File lib/amazon/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

Cache the data from contents and associate it with url.

[Source]

# 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 )
        File.open( cache_file, 'w' ) { |f| f.puts contents }
      end

[Validate]