# #-- # Copyright (c) 2006-2007, Nicolas Modryzk, OpenWFE.org # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # . Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # . Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # . Neither the name of the "OpenWFE" nor the names of its contributors may be # used to endorse or promote products derived from this software without # specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. #++ # # $Id: definitions.rb 2725 2006-06-02 13:26:32Z jmettraux $ # # # "made in Japan" # # Nicolas Modrzyk at openwfe.org # module OpenWFE # Simple LRU cache based on class LRUCache # initialize the LRU. # cache contains the mapping key,value # queue contains the list of keys, according to the LRU # max is the maximum number of items to keep in the cache def initialize(max=20, maxttl=nil) @cache = {} @queue = [] @ttl_cache = {} @max = max @maxttl = maxttl @finalizers = [] end # Check to see if the cache contains the given key. def include?(key) @cache.include?(key) end # Return the element identified by the given key. def [](key) r = nil if @cache.has_key? key r = @cache[key] @queue.push @queue.delete(key) @ttl_cache[key] = Time.now.to_i end r end # Set the element of the cache identified by the given key. def []=(key, val) if @queue.length == @max d = @queue.shift @cache.delete d end if @queue.first and @maxttl now = Time.now.to_i while ((@ttl_cache[@queue.first] + @maxttl) < now) d = @queue.shift do_finalization(d,@cache[d]) @cache.delete d end end @cache[key] = val @ttl_cache[key] = Time.now.to_i @queue.push key end # delete the key. # TODO: check this is working def delete(key) d = @queue.delete(key) do_finalization(d,@cache[d]) @cache.delete key end # Allows one to set the maximum size of the cache queue. If the queue # is currently larger than the size that it is being set to, elements # will be expired until the queue is at the maximum size. def size=(max) @max = max while @queue.length > @max d = @queue.shift do_finalization(d,@cache[d]) @cache.delete d end @max end alias maxsize= size= # Return the maximum size of the cache. def maxsize @max end # Return the current size of the cache. def size @queue.length end # Return a copy of current set of keys to cache elements. def queue @queue.dup end def add_finalizer(*args,&block) @finalizers.push [block,args] end def do_finalization(key,obj) begin @finalizers.each do |f| f[0].call(key,obj,*f[1]) end rescue Exception => e puts e, e.backtrace raise e end end end end