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 =
"2.0.1"

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
17
18
19
20
21
# 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

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



23
24
25
26
27
28
29
30
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/alephant/cache.rb', line 50

def get(id)
  object       = bucket.objects["#{path}/#{id}"]
  content      = object.read
  content_type = object.content_type
      = object..to_h

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

  {
    :content      => content,
    :content_type => content_type,
    :meta         => 
  }
end

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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