Class: Arachni::Support::Cache::Base Abstract
- Defined in:
- lib/arachni/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, LeastRecentlyUsed, Preference, RandomReplacement
Instance Attribute Summary collapse
-
#max_size ⇒ Integer
Maximum cache size.
Instance Method Summary collapse
-
#[](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.
-
#empty? ⇒ Bool
‘true` if cache is empty, false otherwise.
-
#fetch_or_store(k, &block) ⇒ Object
If key ‘k` exists, its corresponding value will be returned.
-
#include?(k) ⇒ Bool
‘true` if cache includes an entry for key `k`, false otherwise.
-
#initialize(max_size = nil) ⇒ Base
constructor
A new instance of Base.
-
#size ⇒ Integer
Number of entries in the cache.
-
#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.
Constructor Details
#initialize(max_size = nil) ⇒ Base
Returns a new instance of Base.
38 39 40 41 |
# File 'lib/arachni/support/cache/base.rb', line 38 def initialize( max_size = nil ) self.max_size = max_size @cache = {} end |
Instance Attribute Details
#max_size ⇒ Integer
Returns Maximum cache size.
32 33 34 |
# File 'lib/arachni/support/cache/base.rb', line 32 def max_size @max_size end |
Instance Method Details
#[](k) ⇒ Object?
Retrieving method.
99 100 101 |
# File 'lib/arachni/support/cache/base.rb', line 99 def []( k ) cache[k.hash] end |
#[]=(k, v) ⇒ Object
87 88 89 |
# File 'lib/arachni/support/cache/base.rb', line 87 def []=( k, v ) store( k, v ) end |
#any? ⇒ Bool
Returns ‘true` if cache is not empty, `false` otherwise.
130 131 132 |
# File 'lib/arachni/support/cache/base.rb', line 130 def any? !empty? end |
#capped? ⇒ Bool
Returns ‘true` is there is a size limit, `false“ otherwise.
58 59 60 |
# File 'lib/arachni/support/cache/base.rb', line 58 def capped? !!max_size end |
#clear ⇒ Object
clears/empties the cache
147 148 149 |
# File 'lib/arachni/support/cache/base.rb', line 147 def clear cache.clear end |
#delete(k) ⇒ Object?
Removes entry with key ‘k` from the cache.
142 143 144 |
# File 'lib/arachni/support/cache/base.rb', line 142 def delete( k ) cache.delete( k.hash ) end |
#empty? ⇒ Bool
Returns ‘true` if cache is empty, false otherwise.
125 126 127 |
# File 'lib/arachni/support/cache/base.rb', line 125 def empty? cache.empty? end |
#fetch_or_store(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.
114 115 116 |
# File 'lib/arachni/support/cache/base.rb', line 114 def fetch_or_store( k, &block ) include?( k ) ? self[k] : store( k, block.call ) end |
#include?(k) ⇒ Bool
Returns ‘true` if cache includes an entry for key `k`, false otherwise.
120 121 122 |
# File 'lib/arachni/support/cache/base.rb', line 120 def include?( k ) cache.include?( k.hash ) end |
#size ⇒ Integer
Returns number of entries in the cache.
68 69 70 |
# File 'lib/arachni/support/cache/base.rb', line 68 def size cache.size end |
#store(k, v) ⇒ Object
Storage method
80 81 82 83 84 |
# File 'lib/arachni/support/cache/base.rb', line 80 def store( k, v ) prune while capped? && (size > max_size - 1) cache[k.hash] = v end |
#uncap ⇒ Object
Uncaps the cache #max_size limit
63 64 65 |
# File 'lib/arachni/support/cache/base.rb', line 63 def uncap @max_size = nil end |
#uncapped? ⇒ Bool
Returns ‘true` is there is no size limit, `false` otherwise.
53 54 55 |
# File 'lib/arachni/support/cache/base.rb', line 53 def uncapped? !capped? end |