class S3Cache
Constants
- VERSION
Public Class Methods
new(**params)
click to toggle source
# File lib/s3cache.rb, line 10 def initialize(**params) @logger = Rails.logger ? Rails.logger : Logger.new(STDOUT) if not params.to_h[:bucket_name] @logger.info{ "requires {:bucket_name => String, (optional) :expires => Integer}" } end @bucket_name = params.to_h[:bucket_name] @expires = params.to_h[:expires] ? params.to_h[:expires] : 32.days if ENV['AWS_ACCESS_KEY_ID'].nil? || ENV['AWS_SECRET_ACCESS_KEY'].nil? puts 'Enviroment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY must be defined. Learn more http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#config-settings-and-precedence' else @s3 = Aws::S3::Client.new if not bucket_exist? @logger.debug( "creating bucket #{@bucket_name}" ) bucket_create end end end
Public Instance Methods
clear()
click to toggle source
# File lib/s3cache.rb, line 79 def clear cache_keys.each do |key| @logger.debug( "deleting key #{key}" ) @s3.delete_object({:bucket => @bucket_name, :key => key}) end end
exist?(key)
click to toggle source
# File lib/s3cache.rb, line 67 def exist?(key) key_name = cache_key( key ) begin response = @s3.get_object({:bucket => @bucket_name, :key => key_name}) rescue response = nil end @logger.debug( "exists? #{!response.nil?} #{key_name}" ) return !response.nil? end
fetch(key, **params) { || ... }
click to toggle source
# File lib/s3cache.rb, line 53 def fetch(key, **params) key_name = cache_key( key ) if(exist?(key_name) ) @logger.debug( "fetch->read #{key_name}" ) read (key_name) else @logger.debug( "fetch->write #{key_name}" ) value = yield write(key_name, value) read (key_name) end end
read(key, **params)
click to toggle source
# File lib/s3cache.rb, line 32 def read(key, **params) key_name = cache_key( key ) @logger.debug( "read #{key_name}" ) if exist?(key_name) Marshal.load(@s3.get_object({ bucket: @bucket_name, key: key_name }).body.read) else return false end end
write(key, contents, **params)
click to toggle source
# File lib/s3cache.rb, line 42 def write(key, contents, **params) key_name = cache_key( key ) @logger.debug( "write #{key_name}" ) @s3.put_object({ bucket: @bucket_name, key: key_name, expires: Time.now + @expires.days, body: Marshal.dump(contents), }) end
Private Instance Methods
bucket_create()
click to toggle source
# File lib/s3cache.rb, line 105 def bucket_create @s3.create_bucket({ acl: "private", # accepts private, public-read, public-read-write, authenticated-read bucket: @bucket_name, # required }) end
bucket_exist?()
click to toggle source
# File lib/s3cache.rb, line 112 def bucket_exist? begin response = @s3.head_bucket({ bucket: @bucket_name }) rescue response = nil end return !response.nil? end
cache_key(key)
click to toggle source
# File lib/s3cache.rb, line 97 def cache_key(key) if(key.is_a?(String) && key.downcase.match(/^[a-f0-9]{64}$/) ) key else Digest::SHA256.hexdigest(key.to_s).to_s end end
cache_keys()
click to toggle source
——— private ———
# File lib/s3cache.rb, line 89 def cache_keys @s3.list_objects({:bucket => @bucket_name}).first[:contents].collect{|e|e.key} end
cache_valid(key)
click to toggle source
# File lib/s3cache.rb, line 93 def cache_valid(key) exist?(key) end