Project

fifo-cache

0.0
No commit activity in last 3 years
No release in over 3 years
Fast hash-like fixed size cache class with FIFO functionality which removes oldest or less accessed records based on implicit heap.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.0.0
>= 1.5.2

Runtime

>= 0.4
 Project Readme

FIFO Cache

FIFO Cache is fast hash-like fixed size cache class with FIFO functionality which removes oldest or less accessed records based on implicit heap.

Examples and Tracking & Handicap Factor

Cache allows track both hits and puts and remove items from cache according these statistics. They are turned off by default, but can be turned on by setting #factor (or :factor in costructor) to another value than 0.

Handicap factor is multiplier of the minimal hits count of all items in the cache. It's important set it in some cases.

If tracking is turned on and no handicap factor is explicitly set, handicap 1 is assigned to new items. It's safe, but not very acceptable because cache will become static after filling. So it's necessary (or at least higly reasonable) to set priority weighting factor to number higher than 1 according dynamics of your application.

Usage is simple (examples here are for demonstration purposes written without factor set):

require "fifocache"

cache = Fifocache::new(3, :puts => true)   # or 300000, od sure :-)
cache[:alfa] = 'alfa'
cache[:beta] = 'beta'
cache[:gama] = 'gama'
cache[:delta] = 'delta'     # in this moment, :alfa is removed

But multiple addings are tracked, so subsequent call:

cache[:beta] = 'beta'      # :beta, :gama, :delta in cache
cache[:alfa] = 'alfa'      # :beta, :delta, :alfa in cache

…will cause :gama will be removed, not :beta because :beta is fresher now. If hits tracking is turned on:

cache.hits = true           # you can do it in constructor too

puts cache[:delta]          # cache hit
cache[:gama] = 'gama'       # :beta, :delta, :gama in cache

…because :beta has been put-in two times, :delta has been hit recently, so :alfa is less accessed row and has been removed. In case of hits tracking turned off, :delta would be removed of sure and :alfa kept.

Changing size of existing cache is possible although reducing the size is generally rather slow because of necessity to remove all redundant "oldest" rows.

Copyright

Copyright © 2010 – 2015 Martin Poljak. See LICENSE.txt for further details.