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

max_size[R]

@return [Integer]

Maximum cache size.

Public Class Methods

new( max_size = nil ) click to toggle source

@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

==( other ) click to toggle source
# File lib/arachni/support/cache/base.rb, line 145
def ==( other )
    hash == other.hash
end
[]( k ) click to toggle source

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
[]=( k, v ) click to toggle source

@see {#store}

# File lib/arachni/support/cache/base.rb, line 79
def []=( k, v )
    store( k, v )
end
any?() click to toggle source

@return [Bool]

`true` if cache is not empty, `false` otherwise.
# File lib/arachni/support/cache/base.rb, line 125
def any?
    !empty?
end
capped?() click to toggle source

@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
clear() click to toggle source

Clears/empties the cache.

# File lib/arachni/support/cache/base.rb, line 141
def clear
    @cache.clear
end
delete( k ) click to toggle source

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
dup() click to toggle source
# File lib/arachni/support/cache/base.rb, line 153
def dup
    deep_clone
end
empty?() click to toggle source

@return [Bool]

`true` if cache is empty, false otherwise.
# File lib/arachni/support/cache/base.rb, line 119
def empty?
    @cache.empty?
end
fetch( k, &block ) click to toggle source

@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
hash() click to toggle source
# File lib/arachni/support/cache/base.rb, line 149
def hash
    @cache.hash
end
include?( k ) click to toggle source

@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
max_size=( max ) click to toggle source
# 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
size() click to toggle source

@return [Integer]

Number of entries in the cache.
# File lib/arachni/support/cache/base.rb, line 62
def size
    @cache.size
end
store( k, v ) click to toggle source

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
uncap() click to toggle source

Uncaps the cache {#max_size} limit

# File lib/arachni/support/cache/base.rb, line 56
def uncap
    @max_size = nil
end
uncapped?() click to toggle source

@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

cache() click to toggle source
# File lib/arachni/support/cache/base.rb, line 173
def cache
    @cache
end
get_with_internal_key( k ) click to toggle source
# File lib/arachni/support/cache/base.rb, line 165
def get_with_internal_key( k )
    @cache[k]
end
make_key( k ) click to toggle source
# File lib/arachni/support/cache/base.rb, line 169
def make_key( k )
    k.hash
end
prune() click to toggle source

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
store_with_internal_key( k, v ) click to toggle source
# 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