class Arachni::Support::Cache::Base
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.
@author Tasos “Zapotek” Laskos <tasos.laskos@arachni-scanner.com> @abstract
Attributes
@return [Integer]
Maximum cache size.
Public Class Methods
@param [Integer, nil] max_size
Maximum size of the cache (must be > 0, `nil` means unlimited). Once the size of the cache is about to exceed `max_size`, the pruning phase will be initiated.
# File lib/arachni/support/cache/base.rb, line 29 def initialize( max_size = nil ) self.max_size = max_size @cache = {} end
Public Instance Methods
# File lib/arachni/support/cache/base.rb, line 145 def ==( other ) hash == other.hash end
Retrieving method.
@param [Object] k
Entry key.
@return [Object, nil]
Value for key `k`, `nil` if there is no key `k`.
# File lib/arachni/support/cache/base.rb, line 90 def []( k ) get_with_internal_key( make_key( k ) ) end
@see {#store}
# File lib/arachni/support/cache/base.rb, line 79 def []=( k, v ) store( k, v ) end
@return [Bool]
`true` if cache is not empty, `false` otherwise.
# File lib/arachni/support/cache/base.rb, line 125 def any? !empty? end
@return [Bool]
`true` is there is a size limit, `false`` otherwise
# File lib/arachni/support/cache/base.rb, line 51 def capped? !!max_size end
Clears/empties the cache.
# File lib/arachni/support/cache/base.rb, line 141 def clear @cache.clear end
Removes entry with key ‘k` from the cache.
@param [Object] k
Key.
@return [Object, nil]
Value for key `k`, `nil` if there is no key `k`.
# File lib/arachni/support/cache/base.rb, line 136 def delete( k ) @cache.delete( make_key( k ) ) end
# File lib/arachni/support/cache/base.rb, line 153 def dup deep_clone end
@return [Bool]
`true` if cache is empty, false otherwise.
# File lib/arachni/support/cache/base.rb, line 119 def empty? @cache.empty? end
@note 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.
@param [Object] k
Entry key.
@return [Object]
Value for key `k` or `block.call` if key `k` does not exist.
# File lib/arachni/support/cache/base.rb, line 103 def fetch( k, &block ) k = make_key( k ) @cache.include?( k ) ? get_with_internal_key( k ) : store_with_internal_key( k, block.call ) end
# File lib/arachni/support/cache/base.rb, line 149 def hash @cache.hash end
@return [Bool]
`true` if cache includes an entry for key `k`, false otherwise.
# File lib/arachni/support/cache/base.rb, line 113 def include?( k ) @cache.include?( make_key( k ) ) end
# File lib/arachni/support/cache/base.rb, line 34 def max_size=( max ) @max_size = if !max nil else fail( 'Maximum size must be greater than 0.' ) if max <= 0 max end end
@return [Integer]
Number of entries in the cache.
# File lib/arachni/support/cache/base.rb, line 62 def size @cache.size end
Storage method.
@param [Object] k
Entry key.
@param [Object] v
Object to store.
@return [Object] ‘v`
# File lib/arachni/support/cache/base.rb, line 74 def store( k, v ) store_with_internal_key( make_key( k ), v ) end
Uncaps the cache {#max_size} limit
# File lib/arachni/support/cache/base.rb, line 56 def uncap @max_size = nil end
@return [Bool]
`true` is there is no size limit, `false` otherwise
# File lib/arachni/support/cache/base.rb, line 45 def uncapped? !capped? end
Private Instance Methods
# File lib/arachni/support/cache/base.rb, line 173 def cache @cache end
# File lib/arachni/support/cache/base.rb, line 165 def get_with_internal_key( k ) @cache[k] end
# File lib/arachni/support/cache/base.rb, line 169 def make_key( k ) k.hash end
Called to make room when the cache is about to reach its maximum size.
@abstract
# File lib/arachni/support/cache/base.rb, line 180 def prune fail NotImplementedError end
# File lib/arachni/support/cache/base.rb, line 159 def store_with_internal_key( k, v ) prune while capped? && (size > max_size - 1) @cache[k] = v end