Class: Boogaloo::Cache::Base

Inherits:
Object
  • Object
show all
Includes:
DRbUndumped
Defined in:
lib/boogaloo/cache/base.rb

Overview

Provides fundemental cache operations such as add, set, get and various statistical operations.

Direct Known Subclasses

Persistent, Temporary

Instance Method Summary collapse

Constructor Details

#initialize(name, config) ⇒ Base

Initialize a new base cache instance.

Parameters: name: The name of the cache config: A Hash containing configuration for the cache.



15
16
17
18
19
20
21
# File 'lib/boogaloo/cache/base.rb', line 15

def initialize(name, config)

    @name = name
    @config = config
    reset

end

Instance Method Details

#add(namespace, key, obj) ⇒ Object

Add an object to the cache only if it doesn’t already exist.

Parameters:

namespace

The namespace in which the object will exist. Use nil if global namespace should be used.

key

A uniqeue identifier for the object relative to the given namespace.

obj

The object to add to the cache.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/boogaloo/cache/base.rb', line 79

def add(namespace, key, obj)
    
    return nil_error if obj.nil?
    debug("[#{@name}] ADD: #{namespace or 'nil'}, #{key}") if $debug
    
    if namespace.nil?
        
        @global_store[key] = obj if not @global_store.key?(key)
        
    else
            
        create_namespace(namespace)
        @namespace_store[namespace][key] = obj if not @namespace_store[namespace].key?(key)
            
    end
    
end

#get(namespace, key) ⇒ Object

Get an object from the cache.

Parameters:

namespace

The namespace to get the object from. Use nil if global namespace should be used.

key

A uniqeue identifier for the object relative to the given namespace.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/boogaloo/cache/base.rb', line 28

def get(namespace, key)

    debug("[#{@name}] GET: #{namespace or 'nil'}, #{key}") if $debug

    if namespace.nil?

        @global_store.key?(key) ? @hit += 1 : @miss += 1
        @global_store[key]
                
    else
        
        @miss += 1 and return nil if not @namespace_store.key?(namespace)
        @namespace_store[namespace].key?(key) ? @hit += 1 : @miss += 1
        @namespace_store[namespace][key]                        
        
    end

end

#inspect_keysObject

Returns an Array of all object keys currently contained in the cache.

Array elements:

0

Keys in global scope (Array).

1

Namespaces and their respective keys (Hash).



177
178
179
180
181
182
183
# File 'lib/boogaloo/cache/base.rb', line 177

def inspect_keys
    
    tmp = {}
    @namespace_store.keys.each { |key| tmp[key] = @namespace_store[key].keys }
    [@global_store.keys, tmp]
    
end

#pull(namespace, key) ⇒ Object

Pull and object from the cache (get and delete).

Parameters:

namespace

The namespace to get the object from. Use nil if global namespace should be used.

key

A uniqeue identifier for the object relative to the given namespace.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/boogaloo/cache/base.rb', line 52

def pull(namespace, key)

    debug("[#{@name}] PULL: #{namespace or 'nil'}, #{key}") if $debug

    if namespace.nil?
    
        @global_store.key?(key) ? @hit += 1 : @miss += 1
        @global_store.delete(key)
    
    else
    
        @miss += 1 and return nil if not @namespace_store.key?(namespace)
        @namespace_store[namespace].key?(key) ? @hit += 1 : @miss += 1
        ret = @namespace_store[namespace].delete(key)
        @namespace_store.delete(namespace) if @namespace_store[namespace].size == 0
        ret
    
    end

end

#resetObject

Reset the cache to an empty state.



186
187
188
189
190
191
192
193
194
# File 'lib/boogaloo/cache/base.rb', line 186

def reset
    
    @hit = 0
    @miss = 0
    @global_store = {}
    @namespace_store = {}
    @reset_at = Time.now                
    
end

#set(namespace, key, obj) ⇒ Object

Add an object to the cache, overwriting if one already exists.

Parameters:

namespace

The namespace in which the object will exist. Use nil if global namespace should be used.

key

A uniqeue identifier for the object relative to the given namespace.

obj

The object to add to the cache.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/boogaloo/cache/base.rb', line 103

def set(namespace, key, obj)
    
    return nil_error if obj.nil?
    debug("[#{@name}] SET: #{namespace or 'nil'}, #{key}") if $debug
    
    if namespace.nil?

        @global_store[key] = obj                
    
    else
        
        create_namespace(namespace)
        @namespace_store[namespace][key] = obj
        
    end
    
end

#stat_allObject

Returns all available statistics in an Array.

Array elements:

0

stat_reset_at

1

stat_hits

2

stat_misses

3

stat_namespaces

4

stat_objects



130
131
132
133
134
# File 'lib/boogaloo/cache/base.rb', line 130

def stat_all
    
    return [stat_reset_at, stat_hits, stat_misses, stat_namespaces, stat_objects]
    
end

#stat_hitsObject

The number of successful cache gets.



144
145
146
147
148
# File 'lib/boogaloo/cache/base.rb', line 144

def stat_hits

    @hit

end

#stat_missesObject

The number of unsuccessful cache gets.



151
152
153
154
155
# File 'lib/boogaloo/cache/base.rb', line 151

def stat_misses

    @miss

end

#stat_namespacesObject

The number of namespaces (plus the global namespace).



158
159
160
161
162
# File 'lib/boogaloo/cache/base.rb', line 158

def stat_namespaces

    @namespace_store.size + 1

end

#stat_objectsObject

The number of objects stored in the cache.



165
166
167
168
169
# File 'lib/boogaloo/cache/base.rb', line 165

def stat_objects

    get_obj_count

end

#stat_reset_atObject

The time the cache was created or last reset.



137
138
139
140
141
# File 'lib/boogaloo/cache/base.rb', line 137

def stat_reset_at

    @reset_at

end