Class: Cuboid::Support::Cache::Base Abstract
- Includes:
- Mixins::Profiler
- Defined in:
- lib/cuboid/support/cache/base.rb
Overview
Base cache implementation – stores, retrieves and removes entries.
The cache will be pruned (call #prune) upon storage operations, removing old entries to make room for new ones.
Direct Known Subclasses
LeastCostReplacement, LeastRecentlyPushed, Preference, RandomReplacement
Instance Attribute Summary collapse
-
#max_size ⇒ Integer
Maximum cache size.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](k) ⇒ Object?
Retrieving method.
- #[]=(k, v) ⇒ Object
-
#any? ⇒ Bool
‘true` if cache is not empty, `false` otherwise.
-
#capped? ⇒ Bool
‘true` is there is a size limit, `false“ otherwise.
-
#clear ⇒ Object
Clears/empties the cache.
-
#delete(k) ⇒ Object?
Removes entry with key ‘k` from the cache.
- #dup ⇒ Object
-
#empty? ⇒ Bool
‘true` if cache is empty, false otherwise.
-
#fetch(k, &block) ⇒ Object
Value for key ‘k` or `block.call` if key `k` does not exist.
- #hash ⇒ Object
-
#include?(k) ⇒ Bool
‘true` if cache includes an entry for key `k`, false otherwise.
-
#initialize(options = {}) ⇒ Base
constructor
# @option options [true, false] :freeze (true) Whether or not to freeze stored items.
-
#size ⇒ Integer
Number of entries in the cache.
- #statistics ⇒ Object
-
#store(k, v) ⇒ Object
Storage method.
-
#uncap ⇒ Object
Uncaps the cache #max_size limit.
-
#uncapped? ⇒ Bool
‘true` is there is no size limit, `false` otherwise.
Methods included from Mixins::Profiler
disable!, enable!, included, on?, #profile_proc, #profile_wrap_proc, results
Constructor Details
#initialize(options = {}) ⇒ Base
# @option options [true, false] :freeze (true)
Whether or not to freeze stored items.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/cuboid/support/cache/base.rb', line 25 def initialize( = {} ) super() @options = @freeze = @options[:freeze].nil? ? true : @options[:freeze] self.max_size = @options[:size] @cache = {} @hits = 0 @misses = 0 @prunings = 0 end |
Instance Attribute Details
#max_size ⇒ Integer
Returns Maximum cache size.
16 17 18 |
# File 'lib/cuboid/support/cache/base.rb', line 16 def max_size @max_size end |
Instance Method Details
#==(other) ⇒ Object
167 168 169 |
# File 'lib/cuboid/support/cache/base.rb', line 167 def ==( other ) hash == other.hash end |
#[](k) ⇒ Object?
Retrieving method.
109 110 111 |
# File 'lib/cuboid/support/cache/base.rb', line 109 def []( k ) get_with_internal_key( make_key( k ) ) end |
#[]=(k, v) ⇒ Object
98 99 100 |
# File 'lib/cuboid/support/cache/base.rb', line 98 def []=( k, v ) store( k, v ) end |
#any? ⇒ Bool
Returns ‘true` if cache is not empty, `false` otherwise.
147 148 149 |
# File 'lib/cuboid/support/cache/base.rb', line 147 def any? !empty? end |
#capped? ⇒ Bool
Returns ‘true` is there is a size limit, `false“ otherwise.
70 71 72 |
# File 'lib/cuboid/support/cache/base.rb', line 70 def capped? !!max_size end |
#clear ⇒ Object
Clears/empties the cache.
163 164 165 |
# File 'lib/cuboid/support/cache/base.rb', line 163 def clear @cache.clear end |
#delete(k) ⇒ Object?
Removes entry with key ‘k` from the cache.
158 159 160 |
# File 'lib/cuboid/support/cache/base.rb', line 158 def delete( k ) @cache.delete( make_key( k ) ) end |
#dup ⇒ Object
175 176 177 |
# File 'lib/cuboid/support/cache/base.rb', line 175 def dup self.class.new( @options.dup ).tap { |h| h.cache = @cache.dup } end |
#empty? ⇒ Bool
Returns ‘true` if cache is empty, false otherwise.
141 142 143 |
# File 'lib/cuboid/support/cache/base.rb', line 141 def empty? @cache.empty? end |
#fetch(k, &block) ⇒ Object
If key ‘k` exists, its corresponding value will be returned. If not, the return value of `block` will be assigned to key `k` and that value will be returned.
Returns Value for key ‘k` or `block.call` if key `k` does not exist.
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/cuboid/support/cache/base.rb', line 122 def fetch( k, &block ) k = make_key( k ) if @cache.include?( k ) get_with_internal_key( k ) else @misses += 1 store_with_internal_key( k, profile_proc( &block ) ) end end |
#hash ⇒ Object
171 172 173 |
# File 'lib/cuboid/support/cache/base.rb', line 171 def hash @cache.hash end |
#include?(k) ⇒ Bool
Returns ‘true` if cache includes an entry for key `k`, false otherwise.
135 136 137 |
# File 'lib/cuboid/support/cache/base.rb', line 135 def include?( k ) @cache.include?( make_key( k ) ) end |
#size ⇒ Integer
Returns Number of entries in the cache.
81 82 83 |
# File 'lib/cuboid/support/cache/base.rb', line 81 def size @cache.size end |
#statistics ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cuboid/support/cache/base.rb', line 38 def statistics lookups = @hits + @misses { lookups: lookups, hits: @hits, hit_ratio: @hits == 0 ? 0.0 : @hits / Float( lookups ), misses: @misses, miss_ratio: @misses == 0 ? 0.0 : @misses / Float( lookups ), prunings: @prunings, size: size, max_size: max_size } end |
#store(k, v) ⇒ Object
Storage method.
93 94 95 |
# File 'lib/cuboid/support/cache/base.rb', line 93 def store( k, v ) store_with_internal_key( make_key( k ), v ) end |
#uncap ⇒ Object
Uncaps the cache #max_size limit
75 76 77 |
# File 'lib/cuboid/support/cache/base.rb', line 75 def uncap @max_size = nil end |
#uncapped? ⇒ Bool
Returns ‘true` is there is no size limit, `false` otherwise.
64 65 66 |
# File 'lib/cuboid/support/cache/base.rb', line 64 def uncapped? !capped? end |