unicache 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +4 -1
- data/Rakefile +29 -4
- data/doc/UniCache/CallbackError.html +1 -1
- data/doc/UniCache/Error.html +1 -1
- data/doc/UniCache/FetchError.html +1 -1
- data/doc/UniCache/LruEviction.html +52 -47
- data/doc/UniCache/RemoveError.html +1 -1
- data/doc/UniCache/SizeError.html +1 -1
- data/doc/UniCache/UniCacheCommon.html +132 -0
- data/doc/UniCache.html +395 -148
- data/doc/_index.html +1 -1
- data/doc/file.CHANGELOG.html +6 -2
- data/doc/file.README.html +1 -1
- data/doc/index.html +1 -1
- data/doc/method_list.html +34 -16
- data/doc/top-level-namespace.html +1 -1
- data/lib/unicache.rb +91 -47
- data/lib/version.rb +6 -0
- data/test/test_all.rb +62 -10
- metadata +4 -2
data/doc/_index.html
CHANGED
@@ -194,7 +194,7 @@
|
|
194
194
|
</div>
|
195
195
|
|
196
196
|
<div id="footer">
|
197
|
-
Generated on
|
197
|
+
Generated on Thu Jul 17 14:54:02 2014 by
|
198
198
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
199
199
|
0.8.6.1 (ruby-1.9.3).
|
200
200
|
</div>
|
data/doc/file.CHANGELOG.html
CHANGED
@@ -63,7 +63,11 @@
|
|
63
63
|
|
64
64
|
<div id="content"><div id='filecontents'>
|
65
65
|
<h1>Version history</h1>
|
66
|
-
<dl class="rdoc-list"><dt>0.0.
|
66
|
+
<dl class="rdoc-list"><dt>0.0.3</dt>
|
67
|
+
<dd>
|
68
|
+
<p>"open" and "close" method for cache content init and dump. Size can be
|
69
|
+
"nil" (unlimited size).</p>
|
70
|
+
</dd><dt>0.0.2</dt>
|
67
71
|
<dd>
|
68
72
|
<p>Uses NotifyHub for callbacks. No user visible changes.</p>
|
69
73
|
</dd><dt>0.0.1</dt>
|
@@ -73,7 +77,7 @@
|
|
73
77
|
</div></div>
|
74
78
|
|
75
79
|
<div id="footer">
|
76
|
-
Generated on
|
80
|
+
Generated on Thu Jul 17 14:54:02 2014 by
|
77
81
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
78
82
|
0.8.6.1 (ruby-1.9.3).
|
79
83
|
</div>
|
data/doc/file.README.html
CHANGED
@@ -79,7 +79,7 @@ policy by default. Cache can be configured to use another policy.</p>
|
|
79
79
|
</div></div>
|
80
80
|
|
81
81
|
<div id="footer">
|
82
|
-
Generated on
|
82
|
+
Generated on Thu Jul 17 14:54:02 2014 by
|
83
83
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
84
84
|
0.8.6.1 (ruby-1.9.3).
|
85
85
|
</div>
|
data/doc/index.html
CHANGED
@@ -79,7 +79,7 @@ policy by default. Cache can be configured to use another policy.</p>
|
|
79
79
|
</div></div>
|
80
80
|
|
81
81
|
<div id="footer">
|
82
|
-
Generated on
|
82
|
+
Generated on Thu Jul 17 14:54:02 2014 by
|
83
83
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
84
84
|
0.8.6.1 (ruby-1.9.3).
|
85
85
|
</div>
|
data/doc/method_list.html
CHANGED
@@ -59,71 +59,83 @@
|
|
59
59
|
|
60
60
|
|
61
61
|
<li class="r1 ">
|
62
|
-
<span class='object_link'><a href="UniCache.html#clear-instance_method" title="UniCache#clear (method)">#clear</a></span>
|
63
|
-
<small>UniCache</small>
|
62
|
+
<span class='object_link'><a href="UniCache/LruEviction.html#clear-instance_method" title="UniCache::LruEviction#clear (method)">#clear</a></span>
|
63
|
+
<small>UniCache::LruEviction</small>
|
64
64
|
</li>
|
65
65
|
|
66
66
|
|
67
67
|
<li class="r2 ">
|
68
|
-
<span class='object_link'><a href="UniCache
|
69
|
-
<small>UniCache
|
68
|
+
<span class='object_link'><a href="UniCache.html#clear-instance_method" title="UniCache#clear (method)">#clear</a></span>
|
69
|
+
<small>UniCache</small>
|
70
70
|
</li>
|
71
71
|
|
72
72
|
|
73
73
|
<li class="r1 ">
|
74
|
-
<span class='object_link'><a href="UniCache.html#
|
74
|
+
<span class='object_link'><a href="UniCache.html#close-instance_method" title="UniCache#close (method)">#close</a></span>
|
75
75
|
<small>UniCache</small>
|
76
76
|
</li>
|
77
77
|
|
78
78
|
|
79
79
|
<li class="r2 ">
|
80
|
-
<span class='object_link'><a href="UniCache.html#
|
80
|
+
<span class='object_link'><a href="UniCache.html#exist%3F-instance_method" title="UniCache#exist? (method)">#exist?</a></span>
|
81
81
|
<small>UniCache</small>
|
82
82
|
</li>
|
83
83
|
|
84
84
|
|
85
85
|
<li class="r1 ">
|
86
|
-
<span class='object_link'><a href="UniCache.html#
|
86
|
+
<span class='object_link'><a href="UniCache.html#get-instance_method" title="UniCache#get (method)">#get</a></span>
|
87
87
|
<small>UniCache</small>
|
88
88
|
</li>
|
89
89
|
|
90
90
|
|
91
91
|
<li class="r2 ">
|
92
|
-
<span class='object_link'><a href="UniCache.html#
|
92
|
+
<span class='object_link'><a href="UniCache.html#hash_get_op-instance_method" title="UniCache#hash_get_op (method)">#hash_get_op</a></span>
|
93
93
|
<small>UniCache</small>
|
94
94
|
</li>
|
95
95
|
|
96
96
|
|
97
97
|
<li class="r1 ">
|
98
|
-
<span class='object_link'><a href="UniCache.html#
|
98
|
+
<span class='object_link'><a href="UniCache.html#hash_set_op-instance_method" title="UniCache#hash_set_op (method)">#hash_set_op</a></span>
|
99
99
|
<small>UniCache</small>
|
100
100
|
</li>
|
101
101
|
|
102
102
|
|
103
103
|
<li class="r2 ">
|
104
|
+
<span class='object_link'><a href="UniCache.html#hash_store-instance_method" title="UniCache#hash_store (method)">#hash_store</a></span>
|
105
|
+
<small>UniCache</small>
|
106
|
+
</li>
|
107
|
+
|
108
|
+
|
109
|
+
<li class="r1 ">
|
104
110
|
<span class='object_link'><a href="UniCache/LruEviction.html#initialize-instance_method" title="UniCache::LruEviction#initialize (method)">#initialize</a></span>
|
105
111
|
<small>UniCache::LruEviction</small>
|
106
112
|
</li>
|
107
113
|
|
108
114
|
|
109
|
-
<li class="
|
115
|
+
<li class="r2 ">
|
110
116
|
<span class='object_link'><a href="UniCache.html#initialize-instance_method" title="UniCache#initialize (method)">#initialize</a></span>
|
111
117
|
<small>UniCache</small>
|
112
118
|
</li>
|
113
119
|
|
114
120
|
|
115
|
-
<li class="
|
121
|
+
<li class="r1 ">
|
116
122
|
<span class='object_link'><a href="UniCache.html#lock-instance_method" title="UniCache#lock (method)">#lock</a></span>
|
117
123
|
<small>UniCache</small>
|
118
124
|
</li>
|
119
125
|
|
120
126
|
|
121
|
-
<li class="
|
127
|
+
<li class="r2 ">
|
122
128
|
<span class='object_link'><a href="UniCache/LruEviction.html#nextEvict-instance_method" title="UniCache::LruEviction#nextEvict (method)">#nextEvict</a></span>
|
123
129
|
<small>UniCache::LruEviction</small>
|
124
130
|
</li>
|
125
131
|
|
126
132
|
|
133
|
+
<li class="r1 ">
|
134
|
+
<span class='object_link'><a href="UniCache.html#open-instance_method" title="UniCache#open (method)">#open</a></span>
|
135
|
+
<small>UniCache</small>
|
136
|
+
</li>
|
137
|
+
|
138
|
+
|
127
139
|
<li class="r2 ">
|
128
140
|
<span class='object_link'><a href="UniCache.html#peek-instance_method" title="UniCache#peek (method)">#peek</a></span>
|
129
141
|
<small>UniCache</small>
|
@@ -143,14 +155,14 @@
|
|
143
155
|
|
144
156
|
|
145
157
|
<li class="r1 ">
|
146
|
-
<span class='object_link'><a href="UniCache
|
147
|
-
<small>UniCache
|
158
|
+
<span class='object_link'><a href="UniCache.html#remove-instance_method" title="UniCache#remove (method)">#remove</a></span>
|
159
|
+
<small>UniCache</small>
|
148
160
|
</li>
|
149
161
|
|
150
162
|
|
151
163
|
<li class="r2 ">
|
152
|
-
<span class='object_link'><a href="UniCache.html#remove-instance_method" title="UniCache#remove (method)">#remove</a></span>
|
153
|
-
<small>UniCache</small>
|
164
|
+
<span class='object_link'><a href="UniCache/LruEviction.html#remove-instance_method" title="UniCache::LruEviction#remove (method)">#remove</a></span>
|
165
|
+
<small>UniCache::LruEviction</small>
|
154
166
|
</li>
|
155
167
|
|
156
168
|
|
@@ -190,6 +202,12 @@
|
|
190
202
|
</li>
|
191
203
|
|
192
204
|
|
205
|
+
<li class="r1 ">
|
206
|
+
<span class='object_link'><a href="UniCache.html#version-class_method" title="UniCache.version (method)">version</a></span>
|
207
|
+
<small>UniCache</small>
|
208
|
+
</li>
|
209
|
+
|
210
|
+
|
193
211
|
</ul>
|
194
212
|
</div>
|
195
213
|
</body>
|
@@ -103,7 +103,7 @@
|
|
103
103
|
</div>
|
104
104
|
|
105
105
|
<div id="footer">
|
106
|
-
Generated on
|
106
|
+
Generated on Thu Jul 17 14:54:02 2014 by
|
107
107
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
108
108
|
0.8.6.1 (ruby-1.9.3).
|
109
109
|
</div>
|
data/lib/unicache.rb
CHANGED
@@ -1,19 +1,21 @@
|
|
1
|
-
# UniCache is Universal purpose Cache with Least Recently Used
|
1
|
+
# {UniCache} is Universal purpose Cache with Least Recently Used
|
2
2
|
# replacement policy by default. Cache can be configured with another
|
3
3
|
# policy.
|
4
4
|
#
|
5
|
-
# UniCache is intended to be Thread safe.
|
5
|
+
# {UniCache} is intended to be Thread safe.
|
6
6
|
#
|
7
|
-
# User can register callbacks that are run at various UniCache events.
|
7
|
+
# User can register callbacks that are run at various {UniCache} events.
|
8
8
|
# ":getdata" callback is used to retreive cache data if not in
|
9
9
|
# cache. Others do not effect the UniCache state.
|
10
10
|
#
|
11
|
+
# UniCache is inherits Hash, i.e. almost all Hash methods are usable.
|
12
|
+
#
|
11
13
|
# Usage example:
|
12
14
|
# require 'unicache'
|
13
15
|
#
|
14
16
|
# # Create cache with size 2.
|
15
17
|
# c = UniCache.new( 2 )
|
16
|
-
#
|
18
|
+
#
|
17
19
|
# # Register callbacks for some events (two for hit).
|
18
20
|
# c.registerCallback( :hit, Proc.new{ |k,v| puts "Hit: #{k}:#{v}" } )
|
19
21
|
# c.registerCallback( :hit, Proc.new{ |k,v| puts "Found: #{k}:#{v}" } )
|
@@ -67,7 +69,7 @@ class UniCache < Hash
|
|
67
69
|
|
68
70
|
# Cache initialization.
|
69
71
|
#
|
70
|
-
# @param size [Integer] Cache size.
|
72
|
+
# @param size [Integer] Cache size (> 0), nil for infinite size.
|
71
73
|
# @param evict [Object] Eviction policy.
|
72
74
|
def initialize( size, evict = LruEviction.new )
|
73
75
|
super()
|
@@ -78,7 +80,7 @@ class UniCache < Hash
|
|
78
80
|
|
79
81
|
setEviction( evict )
|
80
82
|
|
81
|
-
@cb = NotifyHub.declare( :getdata,
|
83
|
+
@cb = NotifyHub.declare( :open, :close, :getdata,
|
82
84
|
:add, :replace, :put, :overwrite,
|
83
85
|
:remove, :valueremove, :hit, :miss,
|
84
86
|
:peek )
|
@@ -109,9 +111,26 @@ class UniCache < Hash
|
|
109
111
|
undef_method method
|
110
112
|
end
|
111
113
|
|
114
|
+
|
115
|
+
# Open {UniCache} for access. The ":open" callback is
|
116
|
+
# executed. Typically the user would perform {UniCache} content
|
117
|
+
# initialization with the callback.
|
118
|
+
def open
|
119
|
+
runCallbacks( :open, nil )
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
# Close {UniCache} access. The ":close" callback is
|
124
|
+
# executed. Typically the user would perform {UniCache} content
|
125
|
+
# store with the callback.
|
126
|
+
def close
|
127
|
+
runCallbacks( :close, nil )
|
128
|
+
end
|
129
|
+
|
130
|
+
|
112
131
|
# Dummy definition for "store" just for removal.
|
113
132
|
def store(); end
|
114
|
-
|
133
|
+
|
115
134
|
# Remove "store" from UniCache.
|
116
135
|
remove_method :store
|
117
136
|
|
@@ -125,11 +144,11 @@ class UniCache < Hash
|
|
125
144
|
@lock.lock
|
126
145
|
ret = _put( key, value )
|
127
146
|
@lock.unlock
|
128
|
-
ret
|
147
|
+
ret
|
129
148
|
end
|
130
149
|
|
131
150
|
|
132
|
-
# Put operator
|
151
|
+
# Put operator (See: {UniCache#put})
|
133
152
|
def []=( key, value )
|
134
153
|
self.put( key, value )
|
135
154
|
end
|
@@ -137,24 +156,24 @@ class UniCache < Hash
|
|
137
156
|
|
138
157
|
# Get Cache entry by key or return nil if not in Cache.
|
139
158
|
#
|
140
|
-
# If block is provided, it is run under UniCache lock and will
|
159
|
+
# If block is provided, it is run under {UniCache} lock and will
|
141
160
|
# protect the used data from being removed (concurrently).
|
142
161
|
#
|
143
162
|
# If ":getdata" callback is defined, it is used to get data. Data
|
144
|
-
# is always used, also when nil. UniCache lock is released before
|
163
|
+
# is always used, also when nil. {UniCache} lock is released before
|
145
164
|
# callback is called.
|
146
165
|
#
|
147
166
|
# @param key [Object] Key (or tag) of the Cache entry.
|
148
|
-
# @yield Procedure to
|
167
|
+
# @yield Procedure to run with the data.
|
149
168
|
def get( key, &blk )
|
150
169
|
@lock.lock
|
151
170
|
ret = _get( key, &blk )
|
152
171
|
@lock.unlock
|
153
|
-
ret
|
172
|
+
ret
|
154
173
|
end
|
155
174
|
|
156
175
|
|
157
|
-
# Get operator
|
176
|
+
# Get operator (See: {UniCache#get})
|
158
177
|
def []( key )
|
159
178
|
get( key )
|
160
179
|
end
|
@@ -170,7 +189,7 @@ class UniCache < Hash
|
|
170
189
|
|
171
190
|
|
172
191
|
# Get Cache entry existance by key (no effect to eviction). Uses
|
173
|
-
#
|
192
|
+
# {UniCache#peek}, i.e. peek CB is run.
|
174
193
|
#
|
175
194
|
# @param key [Object] Entry key or tag.
|
176
195
|
def exist?( key )
|
@@ -196,9 +215,12 @@ class UniCache < Hash
|
|
196
215
|
|
197
216
|
# Resize the Cache.
|
198
217
|
#
|
199
|
-
# @param size [Integer] New Cache size.
|
218
|
+
# @param size [Integer] New Cache size, "> 0" or "nil".
|
219
|
+
# @return [Integer] New Cache size.
|
200
220
|
def resize( size )
|
201
|
-
|
221
|
+
|
222
|
+
return nil if size == nil
|
223
|
+
|
202
224
|
raise SizeError, "UniCache: Size must be bigger than 0" unless size > 0
|
203
225
|
|
204
226
|
@lock.lock
|
@@ -218,41 +240,52 @@ class UniCache < Hash
|
|
218
240
|
end
|
219
241
|
|
220
242
|
|
221
|
-
# Register a callback to be executed on Cache event.
|
243
|
+
# Register a callback to be executed on Cache event. Callbacks are
|
244
|
+
# passed with following arguments: CacheKey, CacheValue,
|
245
|
+
# CacheObject.
|
246
|
+
#
|
222
247
|
#
|
223
248
|
# Possible events:
|
224
249
|
#
|
225
|
-
# [
|
226
|
-
#
|
227
|
-
#
|
228
|
-
#
|
250
|
+
# [:open] Called when {UniCache#open} is executed. This is
|
251
|
+
# typically used to initialize the UniCache content.
|
252
|
+
#
|
253
|
+
# [:close] Called when {UniCache#close} is executed. This is
|
254
|
+
# typically used to store the UniCache content for next
|
255
|
+
# session.
|
229
256
|
#
|
230
|
-
# [
|
231
|
-
#
|
257
|
+
# [:getdata] Data not found in cache, callback is used to fetch
|
258
|
+
# it. Data is always added to cache unless an exception
|
259
|
+
# is raised. Also all retrys should be captured into
|
260
|
+
# ":getdata". <key,value> from request.
|
232
261
|
#
|
233
|
-
# [
|
234
|
-
#
|
262
|
+
# [:add] Item is added to Cache without anything being
|
263
|
+
# replaced. <key,value> from request.
|
235
264
|
#
|
236
|
-
# [
|
237
|
-
#
|
265
|
+
# [:replace] Item is added to Cache and another item is
|
266
|
+
# removed. <key,value> is evicted entry.
|
238
267
|
#
|
239
|
-
# [
|
240
|
-
#
|
268
|
+
# [:put] Item is added to Cache. Cache conditions has no effect
|
269
|
+
# i.e. always run for "put". <key,value> from request.
|
241
270
|
#
|
242
|
-
# [
|
271
|
+
# [:overwrite] Existing entry value is replaced by new item
|
272
|
+
# value. <key,value> from request.
|
243
273
|
#
|
244
|
-
# [
|
245
|
-
# <key,value> from old entry.
|
274
|
+
# [:remove] Entry is deleted from Cache.
|
246
275
|
#
|
247
|
-
# [
|
276
|
+
# [:valueremove] Entry value is removed (remove or overwrite).
|
277
|
+
# <key,value> from old entry.
|
248
278
|
#
|
249
|
-
# [
|
279
|
+
# [:hit] Entry is found in Cache. <key,value> from cache.
|
250
280
|
#
|
251
|
-
# [
|
281
|
+
# [:miss] Entry is not found in Cache. <key> from request.
|
282
|
+
#
|
283
|
+
# [:peek] Cache peek. <key,value> from request.
|
252
284
|
#
|
253
285
|
# Example:
|
254
286
|
# registerCallback( :add,
|
255
287
|
# Proc.new do |k,v,o| puts "Key: #{k} with value #{v} added" end
|
288
|
+
# )
|
256
289
|
#
|
257
290
|
# @param type [Symbol] Callback event.
|
258
291
|
# @param proc [Proc] Callback called with args: <key>, <value>, <self>.
|
@@ -321,14 +354,14 @@ class UniCache < Hash
|
|
321
354
|
# Keep track of the least recently used keys. Place oldest at
|
322
355
|
# the beginning of the list.
|
323
356
|
#
|
324
|
-
# @param key [Object]
|
357
|
+
# @param key [Object] Entry key.
|
325
358
|
def update( key )
|
326
359
|
|
327
360
|
@lock.lock
|
328
361
|
|
329
362
|
# Delete old entries of this key.
|
330
363
|
@list.delete_if do |i| i == key end
|
331
|
-
|
364
|
+
|
332
365
|
# Add to end of LRU.
|
333
366
|
@list.push key
|
334
367
|
|
@@ -367,8 +400,17 @@ class UniCache < Hash
|
|
367
400
|
|
368
401
|
|
369
402
|
private
|
370
|
-
|
371
|
-
|
403
|
+
|
404
|
+
|
405
|
+
def _full
|
406
|
+
if @size && self.length >= @size
|
407
|
+
true
|
408
|
+
else
|
409
|
+
false
|
410
|
+
end
|
411
|
+
end
|
412
|
+
|
413
|
+
|
372
414
|
def _put( key, value )
|
373
415
|
runAdd = false
|
374
416
|
|
@@ -378,7 +420,7 @@ class UniCache < Hash
|
|
378
420
|
runCallbacks( :overwrite, key, value )
|
379
421
|
runCallbacks( :valueremove, key )
|
380
422
|
|
381
|
-
elsif
|
423
|
+
elsif _full
|
382
424
|
|
383
425
|
# Overflow.
|
384
426
|
runCallbacks( :replace, @evict.nextEvict )
|
@@ -394,13 +436,13 @@ class UniCache < Hash
|
|
394
436
|
@evict.update( key )
|
395
437
|
|
396
438
|
hash_store( key, value )
|
397
|
-
|
439
|
+
|
398
440
|
runCallbacks( :add, key ) if runAdd
|
399
441
|
runCallbacks( :put, key )
|
400
442
|
|
401
443
|
self
|
402
444
|
end
|
403
|
-
|
445
|
+
|
404
446
|
|
405
447
|
def _get( key, &blk )
|
406
448
|
|
@@ -417,7 +459,7 @@ class UniCache < Hash
|
|
417
459
|
|
418
460
|
if @cb[ :getdata ][0]
|
419
461
|
|
420
|
-
# Get value using callback.
|
462
|
+
# Get value using callback.
|
421
463
|
|
422
464
|
@lock.unlock if @lock.locked?
|
423
465
|
ret = runCallbacks( :getdata, key, nil )
|
@@ -444,18 +486,18 @@ class UniCache < Hash
|
|
444
486
|
|
445
487
|
|
446
488
|
def _remove( key = nil )
|
447
|
-
|
489
|
+
|
448
490
|
if key
|
449
491
|
unless keys.index( key )
|
450
492
|
@lock.unlock if @lock.locked?
|
451
|
-
raise RemoveError, "Key \"#{key}\" does not exist..."
|
493
|
+
raise RemoveError, "Key \"#{key}\" does not exist..."
|
452
494
|
end
|
453
495
|
else
|
454
496
|
key = @evict.remove
|
455
497
|
end
|
456
498
|
|
457
499
|
value = removeEntry( key )
|
458
|
-
|
500
|
+
|
459
501
|
[ key, value ]
|
460
502
|
end
|
461
503
|
|
@@ -481,3 +523,5 @@ class UniCache < Hash
|
|
481
523
|
end
|
482
524
|
|
483
525
|
end
|
526
|
+
|
527
|
+
require 'version'
|
data/lib/version.rb
ADDED
data/test/test_all.rb
CHANGED
@@ -7,7 +7,7 @@ class TestableUniCache < UniCache
|
|
7
7
|
end
|
8
8
|
|
9
9
|
class UniCacheTest < Test::Unit::TestCase
|
10
|
-
|
10
|
+
|
11
11
|
def test_put_get
|
12
12
|
c = UniCache.new( 2 )
|
13
13
|
|
@@ -34,7 +34,7 @@ class UniCacheTest < Test::Unit::TestCase
|
|
34
34
|
|
35
35
|
assert_equal( 'duu', c[ 'dii2' ] )
|
36
36
|
assert_equal( 'bar', c[ 'foo2' ] )
|
37
|
-
|
37
|
+
|
38
38
|
assert_equal( nil, c.get( 'dii' ) )
|
39
39
|
assert_equal( nil, c.get( 'foo' ) )
|
40
40
|
end
|
@@ -74,7 +74,7 @@ class UniCacheTest < Test::Unit::TestCase
|
|
74
74
|
c.put( 'dii', 'duu' )
|
75
75
|
|
76
76
|
assert_equal( 2, c.length )
|
77
|
-
|
77
|
+
|
78
78
|
c.resize( 1 )
|
79
79
|
|
80
80
|
assert_equal( 1, c.length )
|
@@ -148,12 +148,12 @@ class UniCacheTest < Test::Unit::TestCase
|
|
148
148
|
c.clear
|
149
149
|
|
150
150
|
end
|
151
|
-
|
151
|
+
|
152
152
|
|
153
153
|
def test_callbacks
|
154
154
|
c = TestableUniCache.new( 2 )
|
155
155
|
c.cb_table = {}
|
156
|
-
|
156
|
+
|
157
157
|
# Register dummy callbacks.
|
158
158
|
c.cb.ids.each do |type|
|
159
159
|
eval "c.registerCallback( :#{type.to_s}, Proc.new{ |k,v,o| o.cb_table[ :#{type.to_s} ] = [ k, v ] } )"
|
@@ -161,7 +161,7 @@ class UniCacheTest < Test::Unit::TestCase
|
|
161
161
|
|
162
162
|
c.removeCallback( :getdata )
|
163
163
|
c.registerCallback( :getdata, Proc.new{ |k| k+1 } )
|
164
|
-
|
164
|
+
|
165
165
|
# Wrong type.
|
166
166
|
exception = false
|
167
167
|
begin
|
@@ -170,7 +170,7 @@ class UniCacheTest < Test::Unit::TestCase
|
|
170
170
|
exception = true
|
171
171
|
end
|
172
172
|
assert_equal( true, exception )
|
173
|
-
|
173
|
+
|
174
174
|
# Invalid proc.
|
175
175
|
exception = false
|
176
176
|
begin
|
@@ -203,7 +203,32 @@ class UniCacheTest < Test::Unit::TestCase
|
|
203
203
|
c[1]
|
204
204
|
assert_equal( {}, c.cb_table )
|
205
205
|
c.cb_table.clear
|
206
|
-
|
206
|
+
|
207
|
+
end
|
208
|
+
|
209
|
+
|
210
|
+
def test_callbacks_open_close
|
211
|
+
c = TestableUniCache.new( 2 )
|
212
|
+
|
213
|
+
c.registerCallback( :open,
|
214
|
+
|
215
|
+
Proc.new{ |k,v,o|
|
216
|
+
o.put( 'foo', 'bar' )
|
217
|
+
o.put( 'dii', 'duu' )
|
218
|
+
} )
|
219
|
+
|
220
|
+
c.registerCallback( :close,
|
221
|
+
Proc.new{ |k,v,o|
|
222
|
+
o.clear
|
223
|
+
} )
|
224
|
+
|
225
|
+
c.open
|
226
|
+
assert_equal( 'bar', c['foo'] )
|
227
|
+
assert_equal( 'duu', c['dii'] )
|
228
|
+
|
229
|
+
c.close
|
230
|
+
assert_equal( nil, c['foo'] )
|
231
|
+
|
207
232
|
end
|
208
233
|
|
209
234
|
|
@@ -216,9 +241,9 @@ class UniCacheTest < Test::Unit::TestCase
|
|
216
241
|
sleep( 1 )
|
217
242
|
c.remove( 0 )
|
218
243
|
end
|
219
|
-
|
244
|
+
|
220
245
|
c.registerCallback( :getdata, Proc.new do 'data' end )
|
221
|
-
|
246
|
+
|
222
247
|
gotValue = nil
|
223
248
|
|
224
249
|
# Block value through get.
|
@@ -243,4 +268,31 @@ class UniCacheTest < Test::Unit::TestCase
|
|
243
268
|
assert_equal( 'data', gotValue )
|
244
269
|
end
|
245
270
|
|
271
|
+
|
272
|
+
def test_nosize
|
273
|
+
c = UniCache.new( nil )
|
274
|
+
|
275
|
+
assert_equal( nil, c.size )
|
276
|
+
|
277
|
+
c.put( 'foo', 'bar' )
|
278
|
+
|
279
|
+
assert_equal( 1, c.length )
|
280
|
+
|
281
|
+
c.put( 'dii', 'duu' )
|
282
|
+
|
283
|
+
assert_equal( 2, c.length )
|
284
|
+
|
285
|
+
assert_equal( 'duu', c.get( 'dii' ) )
|
286
|
+
assert_equal( 'bar', c.get( 'foo' ) )
|
287
|
+
|
288
|
+
c[ 'foo2' ] = 'bar'
|
289
|
+
|
290
|
+
assert_equal( 3, c.length )
|
291
|
+
|
292
|
+
c[ 'dii2' ] = 'duu'
|
293
|
+
|
294
|
+
assert_equal( 4, c.length )
|
295
|
+
|
296
|
+
end
|
297
|
+
|
246
298
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unicache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-07-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: notifyhub
|
@@ -42,6 +42,7 @@ files:
|
|
42
42
|
- CHANGELOG.rdoc
|
43
43
|
- LICENSE
|
44
44
|
- Rakefile
|
45
|
+
- lib/version.rb
|
45
46
|
- lib/unicache.rb
|
46
47
|
- test/test_all.rb
|
47
48
|
- doc/top-level-namespace.html
|
@@ -56,6 +57,7 @@ files:
|
|
56
57
|
- doc/file.README.html
|
57
58
|
- doc/method_list.html
|
58
59
|
- doc/file_list.html
|
60
|
+
- doc/UniCache/UniCacheCommon.html
|
59
61
|
- doc/UniCache/CallbackError.html
|
60
62
|
- doc/UniCache/FetchError.html
|
61
63
|
- doc/UniCache/LruEviction.html
|