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

Direct Known Subclasses

File

Defined Under Namespace

Classes: Entry, File

Constant Summary collapse

@@bucket =
{}

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



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

def initialize
end

Instance Method Details

#add(index, data) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/jerakia/cache.rb', line 12

def 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
  #
  @@bucket[index][:content] = Marshal::load(Marshal.dump(data))
  data
end

#bucketObject



36
37
38
# File 'lib/jerakia/cache.rb', line 36

def bucket
  @@bucket
end

#get(index) ⇒ Object



31
32
33
34
# File 'lib/jerakia/cache.rb', line 31

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

#in_bucket?(index) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/jerakia/cache.rb', line 22

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

#valid?(index) ⇒ Boolean

default behaviour is always validate if exists.

Returns:

  • (Boolean)


27
28
29
# File 'lib/jerakia/cache.rb', line 27

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