Class: Cuboid::Support::Cache::Base Abstract

Inherits:
Object
  • Object
show all
Includes:
Mixins::Profiler
Defined in:
lib/cuboid/support/cache/base.rb

Overview

This class is abstract.

Base cache implementation – stores, retrieves and removes entries.

The cache will be pruned (call #prune) upon storage operations, removing old entries to make room for new ones.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Profiler

disable!, enable!, included, on?, #profile_proc, #profile_wrap_proc, results

Constructor Details

#initialize(options = {}) ⇒ Base

# @option options [true, false] :freeze (true)

Whether or not to freeze stored items.

Parameters:

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

Options Hash (options):

  • :size (Integer, nil)

    Maximum size of the cache (must be > 0, ‘nil` means unlimited). Once the size of the cache is about to exceed `max_size`, the pruning phase will be initiated.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cuboid/support/cache/base.rb', line 25

def initialize( options = {} )
    super()

    @options      = options
    @freeze       = @options[:freeze].nil? ? true : @options[:freeze]
    self.max_size = @options[:size]

    @cache    = {}
    @hits     = 0
    @misses   = 0
    @prunings = 0
end

Instance Attribute Details

#max_sizeInteger

Returns Maximum cache size.

Returns:

  • (Integer)

    Maximum cache size.



16
17
18
# File 'lib/cuboid/support/cache/base.rb', line 16

def max_size
  @max_size
end

Instance Method Details

#==(other) ⇒ Object



167
168
169
# File 'lib/cuboid/support/cache/base.rb', line 167

def ==( other )
    hash == other.hash
end

#[](k) ⇒ Object?

Retrieving method.

Parameters:

Returns:

  • (Object, nil)

    Value for key ‘k`, `nil` if there is no key `k`.



109
110
111
# File 'lib/cuboid/support/cache/base.rb', line 109

def []( k )
    get_with_internal_key( make_key( k ) )
end

#[]=(k, v) ⇒ Object

See Also:



98
99
100
# File 'lib/cuboid/support/cache/base.rb', line 98

def []=( k, v )
    store( k, v )
end

#any?Bool

Returns ‘true` if cache is not empty, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if cache is not empty, `false` otherwise.



147
148
149
# File 'lib/cuboid/support/cache/base.rb', line 147

def any?
    !empty?
end

#capped?Bool

Returns ‘true` is there is a size limit, `false“ otherwise.

Returns:

  • (Bool)

    ‘true` is there is a size limit, `false“ otherwise



70
71
72
# File 'lib/cuboid/support/cache/base.rb', line 70

def capped?
    !!max_size
end

#clearObject

Clears/empties the cache.



163
164
165
# File 'lib/cuboid/support/cache/base.rb', line 163

def clear
    @cache.clear
end

#delete(k) ⇒ Object?

Removes entry with key ‘k` from the cache.

Parameters:

Returns:

  • (Object, nil)

    Value for key ‘k`, `nil` if there is no key `k`.



158
159
160
# File 'lib/cuboid/support/cache/base.rb', line 158

def delete( k )
    @cache.delete( make_key( k ) )
end

#dupObject



175
176
177
# File 'lib/cuboid/support/cache/base.rb', line 175

def dup
    self.class.new( @options.dup ).tap { |h| h.cache = @cache.dup }
end

#empty?Bool

Returns ‘true` if cache is empty, false otherwise.

Returns:

  • (Bool)

    ‘true` if cache is empty, false otherwise.



141
142
143
# File 'lib/cuboid/support/cache/base.rb', line 141

def empty?
    @cache.empty?
end

#fetch(k, &block) ⇒ Object

Note:

If key ‘k` exists, its corresponding value will be returned. If not, the return value of `block` will be assigned to key `k` and that value will be returned.

Returns Value for key ‘k` or `block.call` if key `k` does not exist.

Parameters:

Returns:

  • (Object)

    Value for key ‘k` or `block.call` if key `k` does not exist.



122
123
124
125
126
127
128
129
130
131
# File 'lib/cuboid/support/cache/base.rb', line 122

def fetch( k, &block )
    k = make_key( k )

    if @cache.include?( k )
        get_with_internal_key( k )
    else
        @misses += 1
        store_with_internal_key( k, profile_proc( &block ) )
    end
end

#hashObject



171
172
173
# File 'lib/cuboid/support/cache/base.rb', line 171

def hash
    @cache.hash
end

#include?(k) ⇒ Bool

Returns ‘true` if cache includes an entry for key `k`, false otherwise.

Returns:

  • (Bool)

    ‘true` if cache includes an entry for key `k`, false otherwise.



135
136
137
# File 'lib/cuboid/support/cache/base.rb', line 135

def include?( k )
    @cache.include?( make_key( k ) )
end

#sizeInteger

Returns Number of entries in the cache.

Returns:

  • (Integer)

    Number of entries in the cache.



81
82
83
# File 'lib/cuboid/support/cache/base.rb', line 81

def size
    @cache.size
end

#statisticsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cuboid/support/cache/base.rb', line 38

def statistics
    lookups = @hits + @misses

    {
        lookups:    lookups,
        hits:       @hits,
        hit_ratio:  @hits   == 0 ? 0.0 : @hits   / Float( lookups ),
        misses:     @misses,
        miss_ratio: @misses == 0 ? 0.0 : @misses / Float( lookups ),
        prunings:   @prunings,
        size:       size,
        max_size:   max_size
    }
end

#store(k, v) ⇒ Object

Storage method.

Parameters:

Returns:



93
94
95
# File 'lib/cuboid/support/cache/base.rb', line 93

def store( k, v )
    store_with_internal_key( make_key( k ), v )
end

#uncapObject

Uncaps the cache #max_size limit



75
76
77
# File 'lib/cuboid/support/cache/base.rb', line 75

def uncap
    @max_size = nil
end

#uncapped?Bool

Returns ‘true` is there is no size limit, `false` otherwise.

Returns:

  • (Bool)

    ‘true` is there is no size limit, `false` otherwise



64
65
66
# File 'lib/cuboid/support/cache/base.rb', line 64

def uncapped?
    !capped?
end