Class: Jerakia::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/jerakia/cache.rb

Overview

Very primitive form of cache - but we'll make this smarter

Defined Under Namespace

Classes: Entry, File

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache


12
13
# File 'lib/jerakia/cache.rb', line 12

def initialize
end

Class Attribute Details

.bucketObject (readonly)

Returns the value of attribute bucket


9
10
11
# File 'lib/jerakia/cache.rb', line 9

def bucket
  @bucket
end

Class Method Details

.add(index, data, metadata = {}) ⇒ Object


19
20
21
22
23
24
25
26
27
# File 'lib/jerakia/cache.rb', line 19

def self.add(index, data, ={})
  @bucket[index] ||= {}
  ## The cache bucket is a global class object, therefore we should
  ## always store a copy of the data object, not the actual object
  ## to ensure that it is not referenced and tainted by the lookup
  #
  (index, )
  @bucket[index][:content] = Marshal.load(Marshal.dump(data))
end

.get(index) ⇒ Object


65
66
67
68
# File 'lib/jerakia/cache.rb', line 65

def self.get(index)
  data = @bucket[index][:content]
  Marshal.load(Marshal.dump(data))
end

.in_bucket?(index) ⇒ Boolean

Returns:

  • (Boolean)

44
45
46
# File 'lib/jerakia/cache.rb', line 44

def self.in_bucket?(index)
  bucket.has_key?(index)
end

.metadata(index) ⇒ Object


33
34
35
36
37
# File 'lib/jerakia/cache.rb', line 33

def self.(index)
  if in_bucket?(index)
    Marshal.load(Marshal.dump(@bucket[index][:metadata]))
  end
end

.purge(index) ⇒ Object


57
58
59
# File 'lib/jerakia/cache.rb', line 57

def self.purge(index)
  @bucket.delete(index)
end

.set_metadata(index, metadata) ⇒ Object


29
30
31
# File 'lib/jerakia/cache.rb', line 29

def self.(index, )
  @bucket[index][:metadata] = Marshal.load(Marshal.dump())
end

Instance Method Details

#add(index, data, metadata = {}) ⇒ Object


15
16
17
# File 'lib/jerakia/cache.rb', line 15

def add(index, data, ={})
  self.class.add(index,data, )
end

#bucketObject


70
71
72
# File 'lib/jerakia/cache.rb', line 70

def bucket
  self.class.bucket
end

#get(index) ⇒ Object


61
62
63
# File 'lib/jerakia/cache.rb', line 61

def get(index)
  self.class.get(index)
end

#in_bucket?(index) ⇒ Boolean

Returns:

  • (Boolean)

40
41
42
# File 'lib/jerakia/cache.rb', line 40

def in_bucket?(index)
  self.class.in_bucket?(index)
end

#purge(index) ⇒ Object


53
54
55
# File 'lib/jerakia/cache.rb', line 53

def purge(index)
  self.class.purge(index)
end

#valid?(index) ⇒ Boolean

default behaviour is always validate if exists.

Returns:

  • (Boolean)

49
50
51
# File 'lib/jerakia/cache.rb', line 49

def valid?(index)
  in_bucket?(index)
end