Class: Fifocache

Inherits:
Object
  • Object
show all
Defined in:
lib/fifocache.rb

Overview

Represents cache object accessible by similar way as hash, but with fixed capacity with FIFO funcionality.

It’s useful for limited size caches. Oldest cache records are removed. Also can be used by dynamic mode, so the less putted or less accessed (or both) cache records are removed instead of the oldest records in that cases.

For touches tracking utilizes implicit heap queue.

Constant Summary collapse

INFINITY =

Integer overflow protection.

1.0/0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size, opts = { }) ⇒ Fifocache

Constructor. Initializes cache to appropriate size.

Parameters:

  • site (Integer)

    size of the cache

  • opts (Hash) (defaults to: { })

    tracking options

Options Hash (opts):

  • :puts (Boolean)

    indicates, puts should be tracked

  • :hits (Boolean)

    indicates, hits should be tracked

  • :factor (Float, Integer)

    indicates new items priority correction factor



79
80
81
82
83
84
85
86
87
88
# File 'lib/fifocache.rb', line 79

def initialize(size, opts = {  })
    @data = { }
    @queue = Depq::new
    @counts = { }

    @size = size
    @puts = opts[:puts].to_b
    @hits = opts[:hits].to_b
    @factor = opts[:factor].to_f
end

Instance Attribute Details

#factorFloat

Indicates new items handicap factor.

Handicap factor is multiplier of the min hits count of all items in the cache. It’s important set it in some cases. See #[]=.

Returns:

  • (Float)

See Also:



66
67
68
# File 'lib/fifocache.rb', line 66

def factor
  @factor
end

#hitsBoolean

Indicates hits should be tracked.

Returns:

  • (Boolean)


53
54
55
# File 'lib/fifocache.rb', line 53

def hits
  @hits
end

#putsBoolean

Indicates puts should be tracked.

Returns:

  • (Boolean)


45
46
47
# File 'lib/fifocache.rb', line 45

def puts
  @puts
end

#sizeInteger

Contains maximal size of the cache.

Returns:

  • (Integer)

    maximal size of the cache



37
38
39
# File 'lib/fifocache.rb', line 37

def size
  @size
end

Instance Method Details

#[](key) ⇒ Object

Returns item from cache.

Parameters:

  • key (Object)

    item key

Returns:

  • (Object)

    item value



133
134
135
136
137
138
139
# File 'lib/fifocache.rb', line 133

def [](key)
    if @hits
        self.touch(key)
    end
    
    @data[key]
end

#[]=(key, item) ⇒ Object

Puts item with key to cache.

If tracking is turned on and no #factor 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.

Parameters:

  • key (Object)

    item key

  • item (Object)

    item value

See Also:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fifocache.rb', line 104

def []=(key, item)

    # Adds to cache
    
    if not self.has_key? key
    
        # Inserts to tracking structures
        @data[key] = item
        locator = @queue.insert(key, __new_priority)
        @counts[key] = locator
            
        # Eventually removes first (last)
        if self.length > @size
            self.clean!
        end
        
    elsif @puts
        self.touch(key)
    end

end

#clean!(count = 1) ⇒ Hash Also known as: clean

Cleans specified number of slots.

Returns:

  • (Hash)

    removed pairs



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/fifocache.rb', line 220

def clean!(count = 1)
    result = { }
    count.times do
        dkey = @queue.delete_min
        result[dkey] = __purge(dkey)  
        
        if @data.empty?
            break
        end
    end
    
    return result
end

#clear!Object Also known as: clear

Clear whole cache.



240
241
242
243
244
# File 'lib/fifocache.rb', line 240

def clear!
    @data.replace({ })
    @counts.replace({ })
    @queue.clear
end

#has_key?(key) ⇒ Boolean Also known as: include?

Indicates key is in cache.

Parameters:

  • key (Object)

    item key

Returns:

  • (Boolean)

    true it it is, false otherwise



148
149
150
# File 'lib/fifocache.rb', line 148

def has_key?(key)
    @data.has_key? key
end

#lengthInteger

Indicates current size of the cache.

Returns:

  • (Integer)

    current size of the cache



176
177
178
# File 'lib/fifocache.rb', line 176

def length
    @data.length
end

#remove(key) ⇒ Object

Removes key.

Parameters:

  • key (Object)

    item key

Returns:

  • (Object)

    removed item value



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/fifocache.rb', line 200

def remove(key)
    if self.has_key? key
        # Heap queue
        locator = @counts[key]
        @queue.delete_locator(locator)
        
        # Data holders
        result = __purge(key)
    else
        result = nil
    end
        
    return result
end

#to_hHash

Converts to hash.

Returns:

  • (Hash)

    cache data



253
254
255
# File 'lib/fifocache.rb', line 253

def to_h
    @data.dup
end

#touch(key) ⇒ Object

Touches key in cache.

Parameters:

  • key (Object)

    item key



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/fifocache.rb', line 159

def touch(key)
    locator = @counts[key]

    if locator
        priority = locator.priority + 1
    end

    if locator and priority != self.class::INFINITY
        locator.update(key, locator.priority + 1)
    end
end