Class: Cache
- Inherits:
-
Object
- Object
- Cache
- Defined in:
- lib/libcache/cache.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#expiry_time ⇒ Object
Returns the value of attribute expiry_time.
-
#max_size ⇒ Object
Returns the value of attribute max_size.
-
#post_get ⇒ Object
Returns the value of attribute post_get.
-
#refresh ⇒ Object
Returns the value of attribute refresh.
-
#store ⇒ Object
Returns the value of attribute store.
Instance Method Summary collapse
-
#check_refresh(key) ⇒ Object
Refreshes an object if it has been invalidated.
-
#create_store ⇒ Object
Initializes the cache store.
-
#exists?(key) ⇒ Boolean
Checks if a key-value pair exists in the cache.
-
#get(key) ⇒ Object
Gets the object that corresponds with the key.
- #get_size ⇒ Object
-
#has_refresh? ⇒ Boolean
Checks if the cache has a refresh method.
-
#initialize ⇒ Cache
constructor
Creates a basic Cache with the UTC timezone.
-
#invalidate(key) ⇒ Object
Deletes a key-value pair from the cache.
-
#invalidate_all ⇒ Object
Clears all items in the cache.
-
#perform_post_get(key) ⇒ Object
Performs a function after a key has been summoned from the cache.
-
#put(key, value) ⇒ Object
Places an object inside the cache and handles max size eviction.
Constructor Details
#initialize ⇒ Cache
Creates a basic Cache with the UTC timezone
9 10 11 12 13 |
# File 'lib/libcache/cache.rb', line 9 def initialize ENV['TZ'] = 'UTC' @scheduler = Rufus::Scheduler.new @time_tracker = Hash.new end |
Instance Attribute Details
#expiry_time ⇒ Object
Returns the value of attribute expiry_time.
6 7 8 |
# File 'lib/libcache/cache.rb', line 6 def expiry_time @expiry_time end |
#max_size ⇒ Object
Returns the value of attribute max_size.
6 7 8 |
# File 'lib/libcache/cache.rb', line 6 def max_size @max_size end |
#post_get ⇒ Object
Returns the value of attribute post_get.
6 7 8 |
# File 'lib/libcache/cache.rb', line 6 def post_get @post_get end |
#refresh ⇒ Object
Returns the value of attribute refresh.
6 7 8 |
# File 'lib/libcache/cache.rb', line 6 def refresh @refresh end |
#store ⇒ Object
Returns the value of attribute store.
6 7 8 |
# File 'lib/libcache/cache.rb', line 6 def store @store end |
Instance Method Details
#check_refresh(key) ⇒ Object
Refreshes an object if it has been invalidated
70 71 72 73 74 75 76 |
# File 'lib/libcache/cache.rb', line 70 def check_refresh(key) if @cache[key] == nil && !has_refresh? val = refresh.call(key) put(key, val) return val end end |
#create_store ⇒ Object
Initializes the cache store
16 17 18 |
# File 'lib/libcache/cache.rb', line 16 def create_store @cache = Hash.new end |
#exists?(key) ⇒ Boolean
Checks if a key-value pair exists in the cache
63 64 65 |
# File 'lib/libcache/cache.rb', line 63 def exists?(key) return @cache.key?(key) end |
#get(key) ⇒ Object
Gets the object that corresponds with the key
51 52 53 54 55 56 57 58 |
# File 'lib/libcache/cache.rb', line 51 def get(key) check_refresh(key) if(@cache[key]) == nil return nil end perform_post_get(key) return @cache[key] end |
#get_size ⇒ Object
91 92 93 |
# File 'lib/libcache/cache.rb', line 91 def get_size return @cache.length end |
#has_refresh? ⇒ Boolean
Returns Checks if the cache has a refresh method.
87 88 89 |
# File 'lib/libcache/cache.rb', line 87 def has_refresh? return refresh == nil end |
#invalidate(key) ⇒ Object
Deletes a key-value pair from the cache
97 98 99 |
# File 'lib/libcache/cache.rb', line 97 def invalidate(key) @cache.delete key end |
#invalidate_all ⇒ Object
Clears all items in the cache
102 103 104 |
# File 'lib/libcache/cache.rb', line 102 def invalidate_all @cache.clear end |
#perform_post_get(key) ⇒ Object
Performs a function after a key has been summoned from the cache
80 81 82 83 84 |
# File 'lib/libcache/cache.rb', line 80 def perform_post_get(key) unless post_get.nil? post_get.call(key) end end |
#put(key, value) ⇒ Object
Places an object inside the cache and handles max size eviction
23 24 25 26 27 28 29 30 31 |
# File 'lib/libcache/cache.rb', line 23 def put(key, value) @cache[key] = value if expiry_time != nil @scheduler.in expiry_time, :blocking => true do invalidate key end end check_expiration(key) end |