class Alephant::Cache

Constants

VERSION

Attributes

bucket[R]
id[R]
path[R]

Public Class Methods

new(id, path) click to toggle source
# File lib/alephant/cache.rb, line 10
def initialize(id, path)
  @id = id
  @path = path
  @bucket = AWS::S3.new.buckets[id]

  logger.info(
    "event"  => "CacheInitialized",
    "id"     => id,
    "path"   => path,
    "method" => "#{self.class}#initialize",
  )
end

Public Instance Methods

clear() click to toggle source
# File lib/alephant/cache.rb, line 23
def clear
  bucket.objects.with_prefix(path).delete_all
  logger.info(
    "event"  => "CacheCleared",
    "path"   => path,
    "method" => "#{self.class}#clear"
  )
end
get(id) click to toggle source
# File lib/alephant/cache.rb, line 50
def get(id)
  object       = bucket.objects["#{path}/#{id}"]
  content      = object.read
  content_type = object.content_type
  meta_data    = object.metadata.to_h

  logger.metric "CacheGets"
  logger.info(
    "event"       => "CacheObjectRetrieved",
    "path"        => path,
    "id"          => id,
    "content"     => content,
    "contentType" => content_type,
    "metadata"    => meta_data,
    "method"      => "#{self.class}#get"
  )

  {
    :content      => content,
    :content_type => content_type,
    :meta         => meta_data
  }
end
put(id, data, content_type = 'text/plain', meta = {}) click to toggle source
# File lib/alephant/cache.rb, line 32
def put(id, data, content_type = 'text/plain', meta = {})
  bucket.objects["#{path}/#{id}"].write(
    data,
    {
      :content_type => content_type,
      :metadata     => meta
    }
  )

  logger.metric "CachePuts"
  logger.info(
    "event"  => "CacheObjectStored",
    "path"   => path,
    "id"     => id,
    "method" => "#{self.class}#put"
  )
end