Sha256: b0afc3fffa46ca8a68c3c24d8e610eefadf78e7099fa795ad9f8cad338086d5d

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

require 'alephant/cache/version'
require 'alephant/logger'
require 'aws-sdk'

module Alephant
  class Cache
    include Logger
    attr_reader :id, :bucket, :path

    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

    def clear
      bucket.objects.with_prefix(path).delete_all
      logger.info(
        "event"  => "CacheCleared",
        "path"   => path,
        "method" => "#{self.class}#clear"
      )
    end

    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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alephant-cache-2.0.1 lib/alephant/cache.rb