Skip to content

Commit

Permalink
optimalizations
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpoljak committed Feb 10, 2011
1 parent e52ad76 commit 052ab81
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -12,14 +12,14 @@ 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 max hits count of all items in the
cache. It's important set it in some cases.
*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
between 1 and 0 according dynamics of your application.
higher than 1 according dynamics of your application.

Usage is simple (examples here are for demonstration purposes written
without factor set):
Expand Down
28 changes: 19 additions & 9 deletions lib/fifocache.rb
Expand Up @@ -55,7 +55,7 @@ class Fifocache
##
# Indicates new items handicap factor.
#
# Handicap factor is multiplier of the max hits count of an item in
# Handicap factor is multiplier of the min hits count of all items in
# the cache. It's important set it in some cases. See {#[]=}.
#
# @return [Float]
Expand Down Expand Up @@ -93,7 +93,7 @@ def initialize(size, opts = { })
# 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 between 1 and 0 according dynamics of your application.
# number higher than 1 according dynamics of your application.
#
# @param [Object] key item key
# @param [Object] item item value
Expand Down Expand Up @@ -200,9 +200,7 @@ def remove(key)
@queue.delete_locator(locator)

# Data holders
result = @data[key]
@data.delete(key)
@counts.delete(key)
result = __purge(key)
else
result = nil
end
Expand All @@ -218,8 +216,8 @@ def remove(key)
def clean!(count = 1)
result = { }
count.times do
dkey = @queue.find_min
result[dkey] = self.remove(dkey)
dkey = @queue.delete_min
result[dkey] = __purge(dkey)

if @data.empty?
break
Expand Down Expand Up @@ -255,14 +253,26 @@ def to_h

private

##
# Purges item from data holders.
#

def __purge(key)
result = @data[key]
@data.delete(key)
@counts.delete(key)

return result
end

##
# Returns new priority.
#

def __new_priority
if (@puts or @hits) and (@factor != 0)
max = @queue.find_max_locator
priority = max.nil? ? 1 : (max.priority / @factor)
min = @queue.find_min_locator
priority = min.nil? ? 1 : (min.priority * @factor)
else
priority = 1
end
Expand Down
8 changes: 4 additions & 4 deletions test
Expand Up @@ -4,14 +4,14 @@
$:.push("./lib")
require "fifocache"

=begin
cache = Fifocache::new(100000)
100000.times do |i|

cache = Fifocache::new(100000, :puts => false, :factor => 0)
200000.times do |i|
cache[i] = i
end

exit
=end

cache = Fifocache::new(3, :puts => true)
cache[:alfa] = 'alfa'
cache[:beta] = 'beta'
Expand Down

0 comments on commit 052ab81

Please sign in to comment.