Added LRU#maximum=/maximum

This allows one to change the maximum amount of keys stored in the
XPath/CSS caches, for example:

    Oga::XPath::Parser::CACHE.maximum = 2056
This commit is contained in:
Yorick Peterse 2015-03-23 00:26:48 +01:00
parent 12aa21fb50
commit 66fa9f62ef
2 changed files with 46 additions and 0 deletions

View File

@ -33,6 +33,24 @@ module Oga
@owner = Thread.current
end
##
# @param [Fixnum] value
#
def maximum=(value)
synchronize do
@maximum = value
resize
end
end
##
# @return [Fixnum]
#
def maximum
return synchronize { @maximum }
end
##
# Returns the value of the key.
#

View File

@ -1,6 +1,34 @@
require 'spec_helper'
describe Oga::LRU do
describe '#maximum=' do
it 'sets the maximum amount of keys' do
cache = described_class.new(10)
cache.maximum = 20
cache.maximum.should == 20
end
it 'resizes the cache when needed' do
cache = described_class.new(2)
cache[:a] = 10
cache[:b] = 20
cache.maximum = 1
cache.size.should == 1
cache.keys.should == [:b]
end
end
describe '#maximum' do
it 'returns the maximum amount of keys' do
described_class.new(5).maximum.should == 5
end
end
describe '#[]' do
it 'returns nil for a non existing key' do
described_class.new[:a].should be_nil