Sha256: cb472eabe74cc0e0b357b2d5686c235b54928e4411c68f6408022b77c2c7596e

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 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("Cache.initialize: end with id #{id} and path #{path}")
    end

    def clear
      bucket.objects.with_prefix(path).delete_all
      logger.info("Cache.clear: #{path}")
    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",
        metric_opts[:dimensions].merge(:function => "put")
      )
      logger.info("Cache.put: #{path}/#{id}")
    end

    def get(id)
      logger.metric(
        "CacheGets",
        metric_opts[:dimensions].merge(:function => "get")
      )
      logger.info("Cache.get: #{path}/#{id}")
      object = bucket.objects["#{path}/#{id}"]

      {
        :content      => object.read,
        :content_type => object.content_type,
        :meta         => object.metadata.to_h
      }
    end

    def metric_opts
      {
        :dimensions => {
          :module   => "Alephant",
          :class    => "Cache"
      }
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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