Class: Arachni::Cache::Base Abstract

Inherits:
Object show all
Defined in:
lib/arachni/cache/base.rb

Overview

This class is abstract.

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:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size = nil) ⇒ Base

Returns a new instance of Base.

Parameters:

  • max_size (Integer, nil) (defaults to: nil)

    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.



38
39
40
41
# File 'lib/arachni/cache/base.rb', line 38

def initialize( max_size = nil )
    self.max_size = max_size
    @cache = {}
end

Instance Attribute Details

#max_sizeInteger

Returns maximum cache size.

Returns:

  • (Integer)

    maximum cache size



31
32
33
# File 'lib/arachni/cache/base.rb', line 31

def max_size
  @max_size
end

Instance Method Details

#[](k) ⇒ Object?

Retrieving method

Parameters:

Returns:

  • (Object, nil)

    value for key k, nil if there is no key k



98
99
100
# File 'lib/arachni/cache/base.rb', line 98

def []( k )
    cache[k]
end

#[]=(k, v) ⇒ Object

See Also:

  • Arachni::Cache::Base.{{#store}


87
88
89
# File 'lib/arachni/cache/base.rb', line 87

def []=( k, v )
    store( k, v )
end

#any?Bool

Returns true if cache is not empty, false otherwise.

Returns:

  • (Bool)

    true if cache is not empty, false otherwise



127
128
129
# File 'lib/arachni/cache/base.rb', line 127

def any?
    !empty?
end

#capped?Bool

Returns true is there is a size limit, false otherwise.

Returns:

  • (Bool)

    true is there is a size limit, false otherwise



58
59
60
# File 'lib/arachni/cache/base.rb', line 58

def capped?
    !!max_size
end

#clearObject

clears/empties the cache



143
144
145
# File 'lib/arachni/cache/base.rb', line 143

def clear
    cache.clear
end

#delete(k) ⇒ Object?

Removes entry with key k from the cache.

Parameters:

Returns:

  • (Object, nil)

    value for key k, nil if there is no key k



138
139
140
# File 'lib/arachni/cache/base.rb', line 138

def delete( k )
    cache.delete( k )
end

#empty?Bool

Returns true if cache is empty, false otherwise.

Returns:

  • (Bool)

    true if cache is empty, false otherwise



122
123
124
# File 'lib/arachni/cache/base.rb', line 122

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.

Parameters:

Returns:

  • (Object)

    value of key k or block.call if key k does not exist.



112
113
114
# File 'lib/arachni/cache/base.rb', line 112

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.

Returns:

  • (Bool)

    true if cache includes an entry for key k, false otherwise



117
118
119
# File 'lib/arachni/cache/base.rb', line 117

def include?( k )
    cache.include?( k )
end

#sizeInteger

Returns number of entries in the cache.

Returns:

  • (Integer)

    number of entries in the cache



68
69
70
# File 'lib/arachni/cache/base.rb', line 68

def size
    cache.size
end

#store(k, v) ⇒ Object

Storage method

Parameters:

Returns:



80
81
82
83
84
# File 'lib/arachni/cache/base.rb', line 80

def store( k, v )
    prune while capped? && (size > max_size - 1)

    cache[k] = v
end

#uncapObject

Uncaps the cache #max_size limit



63
64
65
# File 'lib/arachni/cache/base.rb', line 63

def uncap
    @max_size = nil
end

#uncapped?Bool

Returns true is there is no size limit, false otherwise.

Returns:

  • (Bool)

    true is there is no size limit, false otherwise



53
54
55
# File 'lib/arachni/cache/base.rb', line 53

def uncapped?
    !capped?
end