class Jerakia::Cache
Very primitive form of cache - but we'll make this smarter
Attributes
bucket[R]
Public Class Methods
add(index, data, metadata={})
click to toggle source
# File lib/jerakia/cache.rb, line 19 def self.add(index, data, metadata={}) @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 # set_metadata(index, metadata) @bucket[index][:content] = Marshal.load(Marshal.dump(data)) end
get(index)
click to toggle source
# File lib/jerakia/cache.rb, line 65 def self.get(index) data = @bucket[index][:content] Marshal.load(Marshal.dump(data)) end
in_bucket?(index)
click to toggle source
# File lib/jerakia/cache.rb, line 44 def self.in_bucket?(index) bucket.has_key?(index) end
metadata(index)
click to toggle source
# File lib/jerakia/cache.rb, line 33 def self.metadata(index) if in_bucket?(index) Marshal.load(Marshal.dump(@bucket[index][:metadata])) end end
new()
click to toggle source
# File lib/jerakia/cache.rb, line 12 def initialize end
purge(index)
click to toggle source
# File lib/jerakia/cache.rb, line 57 def self.purge(index) @bucket.delete(index) end
set_metadata(index, metadata)
click to toggle source
# File lib/jerakia/cache.rb, line 29 def self.set_metadata(index, metadata) @bucket[index][:metadata] = Marshal.load(Marshal.dump(metadata)) end
Public Instance Methods
add(index, data, metadata={})
click to toggle source
# File lib/jerakia/cache.rb, line 15 def add(index, data, metadata={}) self.class.add(index,data, metadata) end
bucket()
click to toggle source
# File lib/jerakia/cache.rb, line 70 def bucket self.class.bucket end
get(index)
click to toggle source
# File lib/jerakia/cache.rb, line 61 def get(index) self.class.get(index) end
in_bucket?(index)
click to toggle source
# File lib/jerakia/cache.rb, line 40 def in_bucket?(index) self.class.in_bucket?(index) end
purge(index)
click to toggle source
# File lib/jerakia/cache.rb, line 53 def purge(index) self.class.purge(index) end
valid?(index)
click to toggle source
default behaviour is always validate if exists.
# File lib/jerakia/cache.rb, line 49 def valid?(index) in_bucket?(index) end