lib/arachni/support/cache/base.rb in arachni-1.3.2 vs lib/arachni/support/cache/base.rb in arachni-1.4
- old
+ new
@@ -1,7 +1,7 @@
=begin
- Copyright 2010-2015 Tasos Laskos <tasos.laskos@arachni-scanner.com>
+ Copyright 2010-2016 Tasos Laskos <tasos.laskos@arachni-scanner.com>
This file is part of the Arachni Framework project and is subject to
redistribution and commercial restrictions. Please see the Arachni Framework
web site for more information on licensing and terms of use.
=end
@@ -70,13 +70,11 @@
# @param [Object] v
# Object to store.
#
# @return [Object] `v`
def store( k, v )
- prune while capped? && (size > max_size - 1)
-
- @cache[make_key( k )] = v
+ store_with_internal_key( make_key( k ), v )
end
# @see {#store}
def []=( k, v )
store( k, v )
@@ -88,24 +86,28 @@
# Entry key.
#
# @return [Object, nil]
# Value for key `k`, `nil` if there is no key `k`.
def []( k )
- @cache[make_key( k )]
+ get_with_internal_key( make_key( k ) )
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 of key `k` or `block.call` if key `k` does not exist.
- def fetch_or_store( k, &block )
- include?( k ) ? self[k] : store( k, block.call )
+ # Value for key `k` or `block.call` if key `k` does not exist.
+ def fetch( k, &block )
+ k = make_key( k )
+
+ @cache.include?( k ) ?
+ get_with_internal_key( k ) :
+ store_with_internal_key( k, block.call )
end
# @return [Bool]
# `true` if cache includes an entry for key `k`, false otherwise.
def include?( k )
@@ -151,9 +153,19 @@
def dup
deep_clone
end
private
+
+ def store_with_internal_key( k, v )
+ prune while capped? && (size > max_size - 1)
+
+ @cache[k] = v
+ end
+
+ def get_with_internal_key( k )
+ @cache[k]
+ end
def make_key( k )
k.hash
end