Class: Alephant::Cache

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/alephant/cache.rb,
lib/alephant/cache/version.rb

Constant Summary collapse

VERSION =
"1.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, path) ⇒ Cache

Returns a new instance of Cache.



10
11
12
13
14
15
16
# File 'lib/alephant/cache.rb', line 10

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

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



8
9
10
# File 'lib/alephant/cache.rb', line 8

def bucket
  @bucket
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/alephant/cache.rb', line 8

def id
  @id
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/alephant/cache.rb', line 8

def path
  @path
end

Instance Method Details

#clearObject



18
19
20
21
# File 'lib/alephant/cache.rb', line 18

def clear
  bucket.objects.with_prefix(path).delete_all
  logger.info("Cache.clear: #{path}")
end

#get(id) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/alephant/cache.rb', line 35

def get(id)
  logger.info("Cache.get: #{path}/#{id}")
  object = bucket.objects["#{path}/#{id}"]

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

#put(id, data, content_type = 'text/plain', meta = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/alephant/cache.rb', line 23

def put(id, data, content_type = 'text/plain', meta = {})
  bucket.objects["#{path}/#{id}"].write(
    data,
    {
      :content_type => content_type,
      :metadata     => meta
    }
  )

  logger.info("Cache.put: #{path}/#{id}")
end