Class: Jerakia::Cache
- Inherits:
-
Object
- Object
- Jerakia::Cache
- Defined in:
- lib/jerakia/cache.rb
Overview
Very primitive form of cache - but we’ll make this smarter
Direct Known Subclasses
Defined Under Namespace
Constant Summary collapse
- @@bucket =
{}
Instance Method Summary collapse
- #add(index, data) ⇒ Object
- #bucket ⇒ Object
- #get(index) ⇒ Object
- #in_bucket?(index) ⇒ Boolean
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
-
#valid?(index) ⇒ Boolean
default behaviour is always validate if exists.
Constructor Details
#initialize ⇒ Cache
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 |
#bucket ⇒ Object
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
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.
27 28 29 |
# File 'lib/jerakia/cache.rb', line 27 def valid?(index) in_bucket?(index) end |