Class: Toaster::RAMCache
- Inherits:
-
Cache
- Object
- Cache
- Toaster::RAMCache
show all
- Defined in:
- lib/toaster/db/ram_cache.rb
Constant Summary
collapse
- KEY_CACHE =
"__cache__"
- KEY_CACHE_SIZE =
"__cachesize__"
- KEY_QUERY_KEYS =
"__keys__"
- DO_CACHE_OBJECTS =
false
- DO_CACHE_QUERIES =
true
Constants inherited
from Cache
Cache::KEY_OBJECTS, Cache::KEY_QUERIES
Instance Method Summary
collapse
Methods inherited from Cache
by_db_type, by_id, by_key, by_obj_props, clear, flush, get_cache, get_hits, get_misses, set, set_cache
Constructor Details
Returns a new instance of RAMCache.
20
21
22
23
24
25
|
# File 'lib/toaster/db/ram_cache.rb', line 20
def initialize()
@cache = nil
@hits = nil
@misses = nil
@dirty = false
end
|
Instance Method Details
#by_db_type(db_type) ⇒ Object
27
28
29
30
31
|
# File 'lib/toaster/db/ram_cache.rb', line 27
def by_db_type(db_type)
load_cache()
return by_obj_prop("db_type", db_type)
end
|
#by_id(id) ⇒ Object
33
34
35
36
37
|
# File 'lib/toaster/db/ram_cache.rb', line 33
def by_id(id)
load_cache()
return by_obj_props({"id" => id})
end
|
#by_key(key_path_array) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/toaster/db/ram_cache.rb', line 87
def by_key(key_path_array)
load_cache()
if DO_CACHE_QUERIES
key_path_str = key_path_array.inspect
cached = @cache[KEY_QUERIES][key_path_str]
if cached
@hits << key_path_array if @hits
return cached
end
end
if DO_CACHE_OBJECTS
key_path_array = [key_path_array] if !key_path_array.kind_of?(Array)
obj = @cache
key_path_array.each do |k|
obj = obj[k]
if !obj
@misses << key_path_array if @misses
return nil
end
end
@hits << key_path_array if @hits
return obj
end
@misses << key_path_array if @misses
return nil
end
|
#by_obj_props(props_hash) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/toaster/db/ram_cache.rb', line 39
def by_obj_props(props_hash)
load_cache()
if DO_CACHE_QUERIES
props_str = props_hash.inspect
cached = @cache[KEY_QUERIES][props_str]
if cached
@hits << props_hash if @hits
return cached
end
end
if DO_CACHE_OBJECTS
@cache[KEY_OBJECTS] = [] if !@cache[KEY_OBJECTS]
puts "size: #{@cache[KEY_OBJECTS].size}"
objs = @cache[KEY_OBJECTS]
result = []
objs.each do |o|
arr = o.kind_of?(Array) ? o : [o]
all_match = true
arr.each do |item|
if !item || !matches(item, props_hash)
all_match = false
break
end
end
result << o if all_match
end
if result.size == 0
@misses << props_hash if @misses
return nil
end
@hits << props_hash if @hits
return result
end
props_str = props_hash.inspect
cached = @cache[KEY_CACHE][props_str]
if cached
@hits << props_hash if @hits
return cached
end
@misses << props_hash if @misses
return nil
end
|
#clear ⇒ Object
150
151
152
153
154
155
|
# File 'lib/toaster/db/ram_cache.rb', line 150
def clear()
@cache = nil
@hits = nil
@misses = nil
load_cache()
end
|
#flush ⇒ Object
167
168
169
170
|
# File 'lib/toaster/db/ram_cache.rb', line 167
def flush()
return if !@dirty
@cache = {}
end
|
#get_hits ⇒ Object
157
158
159
160
|
# File 'lib/toaster/db/ram_cache.rb', line 157
def get_hits()
@hits = [] if !@hits
@hits
end
|
#get_misses ⇒ Object
162
163
164
165
|
# File 'lib/toaster/db/ram_cache.rb', line 162
def get_misses()
@misses = [] if !@misses
@misses
end
|
#set(value, key = KEY_OBJECTS) ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/toaster/db/ram_cache.rb', line 118
def set(value, key=KEY_OBJECTS)
load_cache()
if key.kind_of?(Array) && key[0] == KEY_QUERIES
@cache[KEY_QUERIES][key[1]] = value
@dirty = true
return
end
if DO_CACHE_OBJECTS && key == KEY_OBJECTS
value = value[0] if value.kind_of?(Array) && value.size == 1
@cache[key] = [] if !@cache[key]
value = [value] if !value.kind_of?(Array)
@cache[key].concat(value)
@dirty = true
return
end
if key != KEY_OBJECTS
key = key.inspect if !key.kind_of?(String)
@cache[KEY_CACHE][key] = value
@dirty = true
return
end
end
|