@@ -94,11 +94,11 @@ public void init(String cacheName, Struct arguments) throws IOException {
94
94
e .printStackTrace ();
95
95
}
96
96
}
97
-
97
+
98
98
public void init (Config config ,String [] cacheName ,Struct [] arguments ){
99
99
//Not used at the moment
100
100
}
101
-
101
+
102
102
public void init (Config config , String cacheName , Struct arguments ) {
103
103
try {
104
104
init (cacheName ,arguments );
@@ -114,12 +114,12 @@ protected void crateIndexes(){
114
114
coll .createIndex (new BasicDBObject ("timeIdle" , 1 ));
115
115
coll .createIndex (new BasicDBObject ("expires" , 1 ));
116
116
}
117
-
117
+
118
118
protected void startCleaner (){
119
119
Timer timer = new Timer ();
120
120
timer .schedule (new MongoDbCleanTask (this ), 0 ,10000 );
121
121
}
122
-
122
+
123
123
@ Override
124
124
public boolean contains (String key ) {
125
125
BasicDBObject query = new BasicDBObject ();
@@ -132,56 +132,56 @@ public boolean contains(String key) {
132
132
public List entries () {
133
133
List result = new ArrayList <CacheEntry >();
134
134
DBCursor cur = qAll ();
135
-
135
+
136
136
while (cur .hasNext ()){
137
137
BasicDBObject obj = (BasicDBObject )cur .next ();
138
138
MongoDBCacheDocument doc = new MongoDBCacheDocument (obj );
139
139
result .add (new MongoDBCacheEntry (doc ));
140
- }
141
-
140
+ }
141
+
142
142
return result ;
143
143
}
144
144
145
145
@ Override
146
146
public List entries (CacheKeyFilter filter ) {
147
147
List result = new ArrayList <CacheEntry >();
148
148
DBCursor cur = qAll ();
149
-
149
+
150
150
while (cur .hasNext ()){
151
151
BasicDBObject obj = (BasicDBObject )cur .next ();
152
152
MongoDBCacheDocument doc = new MongoDBCacheDocument (obj );
153
153
if (filter .accept (doc .getKey ())){
154
- result .add (new MongoDBCacheEntry (doc ));
154
+ result .add (new MongoDBCacheEntry (doc ));
155
155
}
156
- }
156
+ }
157
157
return result ;
158
158
}
159
159
160
160
@ Override
161
161
public List entries (CacheEntryFilter filter ) {
162
162
List result = new ArrayList <CacheEntry >();
163
163
DBCursor cur = qAll ();
164
-
164
+
165
165
while (cur .hasNext ()){
166
166
BasicDBObject obj = (BasicDBObject )cur .next ();
167
167
MongoDBCacheDocument doc = new MongoDBCacheDocument (obj );
168
168
MongoDBCacheEntry entry = new MongoDBCacheEntry (doc );
169
169
if (filter .accept (entry )){
170
- result .add (entry );
170
+ result .add (entry );
171
171
}
172
- }
172
+ }
173
173
return result ;
174
174
}
175
175
176
176
@ Override
177
177
public MongoDBCacheEntry getCacheEntry (String key ) throws CacheException {
178
- Integer attempts = 0 ;
178
+ Integer attempts = 0 ;
179
179
DBCursor cur = null ;
180
180
BasicDBObject query = new BasicDBObject ("key" , key .toLowerCase ());
181
-
181
+
182
182
// be sure to flush
183
- flushInvalid ();
184
-
183
+ flushInvalid (query );
184
+
185
185
while (attempts <= maxRetry ){
186
186
try {
187
187
cur = coll .find (query );
@@ -193,20 +193,20 @@ public MongoDBCacheEntry getCacheEntry(String key) throws CacheException {
193
193
e .printStackTrace ();
194
194
}
195
195
}
196
-
196
+
197
197
}
198
-
198
+
199
199
if (cur .count () > 0 ){
200
200
hits ++;
201
201
MongoDBCacheDocument doc = new MongoDBCacheDocument ((BasicDBObject ) cur .next ());
202
202
doc .addHit ();
203
203
//update the statistic and persist
204
204
save (doc );
205
- return new MongoDBCacheEntry (doc );
205
+ return new MongoDBCacheEntry (doc );
206
206
}
207
207
misses ++;
208
208
throw (new CacheException ("The document with key [" + key +"] has not been found int this cache." ));
209
-
209
+
210
210
}
211
211
212
212
@ Override
@@ -230,7 +230,7 @@ public Object getValue(String key) throws IOException {
230
230
try {
231
231
MongoDBCacheEntry entry = getCacheEntry (key );
232
232
Object result = entry .getValue ();
233
- return result ;
233
+ return result ;
234
234
}catch (IOException e ){
235
235
throw (e );
236
236
}
@@ -240,7 +240,7 @@ public Object getValue(String key) throws IOException {
240
240
public Object getValue (String key , Object defaultValue ){
241
241
try {
242
242
Object value = getValue (key .toLowerCase ());
243
- return value ;
243
+ return value ;
244
244
}catch (IOException e ){
245
245
return defaultValue ;
246
246
}
@@ -255,7 +255,7 @@ public long hitCount() {
255
255
public List keys () {
256
256
List result = new ArrayList <String >();
257
257
DBCursor cur = qAll_Keys ();
258
-
258
+
259
259
if (cur .count () > 0 ){
260
260
while (cur .hasNext ()){
261
261
result .add (cur .next ().get ("key" ));
@@ -268,12 +268,12 @@ public List keys() {
268
268
public List keys (CacheKeyFilter filter ) {
269
269
List result = new ArrayList <String >();
270
270
DBCursor cur = qAll_Keys ();
271
-
271
+
272
272
if (cur .count () > 0 ){
273
273
while (cur .hasNext ()){
274
274
String key = new MongoDBCacheDocument ((BasicDBObject ) cur .next ()).getKey ();
275
275
if (filter .accept (key )){
276
- result .add (key );
276
+ result .add (key );
277
277
}
278
278
}
279
279
}
@@ -284,12 +284,12 @@ public List keys(CacheKeyFilter filter) {
284
284
public List keys (CacheEntryFilter filter ) {
285
285
List result = new ArrayList <String >();
286
286
DBCursor cur = qAll ();
287
-
287
+
288
288
if (cur .count () > 0 ){
289
289
while (cur .hasNext ()){
290
290
MongoDBCacheEntry entry = new MongoDBCacheEntry (new MongoDBCacheDocument ((BasicDBObject ) cur .next ()));
291
291
if (filter .accept (entry )){
292
- result .add (entry .getKey ());
292
+ result .add (entry .getKey ());
293
293
}
294
294
}
295
295
}
@@ -305,38 +305,38 @@ public long missCount() {
305
305
public void put (String key , Object value , Long idleTime , Long lifeSpan ) {
306
306
CFMLEngine engine = CFMLEngineFactory .getInstance ();
307
307
Cast caster = engine .getCastUtil ();
308
-
308
+
309
309
Long created = System .currentTimeMillis ();
310
310
int create = created .intValue ();
311
311
int idle = idleTime ==null ?0 :idleTime .intValue ();
312
312
int life = lifeSpan ==null ?0 :lifeSpan .intValue ();
313
313
314
314
BasicDBObject obj = new BasicDBObject ();
315
315
MongoDBCacheDocument doc = new MongoDBCacheDocument (obj );
316
-
316
+
317
317
try {
318
318
319
319
doc .setData (func .serialize (value ));
320
320
doc .setCreatedOn (create );
321
321
doc .setTimeIdle (idle );
322
322
doc .setLifeSpan (life );
323
323
doc .setHits (0 );
324
-
324
+
325
325
int expires = life + create ;
326
326
if (life == 0 ){
327
- doc .setExpires (0 );
327
+ doc .setExpires (0 );
328
328
}else {
329
- doc .setExpires (expires );
329
+ doc .setExpires (expires );
330
330
}
331
331
332
332
}
333
333
catch (PageException e ){
334
334
e .printStackTrace ();
335
335
}
336
-
336
+
337
337
doc .setKey (key .toLowerCase ());
338
338
save (doc );
339
-
339
+
340
340
}
341
341
342
342
@ Override
@@ -355,112 +355,114 @@ public boolean remove(String key) {
355
355
public int remove (CacheKeyFilter filter ) {
356
356
DBCursor cur = qAll_Keys ();
357
357
int counter = 0 ;
358
-
358
+
359
359
while (cur .hasNext ()){
360
360
DBObject obj = cur .next ();
361
- String key = (String )obj .get ("key" );
361
+ String key = (String )obj .get ("key" );
362
362
if (filter .accept (key )){
363
363
doDelete ((BasicDBObject )obj );
364
- counter ++;
364
+ counter ++;
365
365
}
366
366
}
367
-
367
+
368
368
return counter ;
369
369
}
370
370
371
371
@ Override
372
- public int remove (CacheEntryFilter filter ) {
372
+ public int remove (CacheEntryFilter filter ) {
373
373
DBCursor cur = qAll ();
374
374
int counter = 0 ;
375
-
375
+
376
376
while (cur .hasNext ()){
377
377
BasicDBObject obj = (BasicDBObject ) cur .next ();
378
378
MongoDBCacheEntry entry = new MongoDBCacheEntry (new MongoDBCacheDocument (obj ));
379
379
if (filter .accept (entry )){
380
380
doDelete (obj );
381
- counter ++;
381
+ counter ++;
382
382
}
383
383
}
384
-
384
+
385
385
return counter ;
386
386
}
387
387
388
388
@ Override
389
389
public List values () {
390
390
DBCursor cur = qAll_Values ();
391
391
List result = new ArrayList <Object >();
392
-
392
+
393
393
while (cur .hasNext ()){
394
394
Object value = new MongoDBCacheDocument ((BasicDBObject ) cur .next ()).getData ();
395
395
try {
396
- result .add (func .evaluate (value ));
396
+ result .add (func .evaluate (value ));
397
397
}catch (PageException e ){
398
398
e .printStackTrace ();
399
399
}
400
400
}
401
-
401
+
402
402
return result ;
403
403
}
404
404
405
405
@ Override
406
406
public List values (CacheKeyFilter filter ) {
407
407
DBCursor cur = qAll_Keys_Values ();
408
408
List result = new ArrayList <Object >();
409
-
409
+
410
410
while (cur .hasNext ()){
411
411
BasicDBObject obj = (BasicDBObject ) cur .next ();
412
412
MongoDBCacheDocument doc = new MongoDBCacheDocument (obj );
413
413
414
414
if (filter .accept (doc .getKey ())){
415
415
Object value = new MongoDBCacheDocument (obj ).getData ();
416
416
try {
417
- result .add (func .evaluate (value ));
417
+ result .add (func .evaluate (value ));
418
418
}catch (PageException e ){
419
419
e .printStackTrace ();
420
- }
420
+ }
421
421
}
422
-
422
+
423
423
}
424
-
424
+
425
425
return result ;
426
426
}
427
427
428
428
@ Override
429
429
public List values (CacheEntryFilter filter ) {
430
430
DBCursor cur = qAll_Keys_Values ();
431
431
List result = new ArrayList <Object >();
432
-
432
+
433
433
while (cur .hasNext ()){
434
434
BasicDBObject obj = (BasicDBObject ) cur .next ();
435
435
MongoDBCacheEntry entry = new MongoDBCacheEntry (new MongoDBCacheDocument (obj ));
436
436
437
437
if (filter .accept (entry )){
438
438
Object value = new MongoDBCacheDocument (obj ).getData ();
439
439
try {
440
- result .add (func .evaluate (value ));
440
+ result .add (func .evaluate (value ));
441
441
}catch (PageException e ){
442
442
e .printStackTrace ();
443
- }
443
+ }
444
444
}
445
-
445
+
446
446
}
447
-
447
+
448
448
return result ;
449
449
}
450
450
451
-
452
- protected void flushInvalid (){
453
- int attempts = 0 ;
451
+
452
+ protected void flushInvalid (BasicDBObject query ){
453
+ Integer attempts = 0 ;
454
+ DBCursor cur = null ;
454
455
Long now = System .currentTimeMillis ();
455
456
int nowi = now .intValue ();
457
+ BasicDBObject q = (BasicDBObject )query .clone ();
456
458
457
459
//execute the query
458
460
while (attempts <= maxRetry ){
459
461
try {
460
462
461
- BasicDBObject q = new BasicDBObject ("expires" , new BasicDBObject ("$lt" ,nowi ).append ("$gt" ,0 ));
463
+ q . append ("expires" , new BasicDBObject ("$lt" ,now ).append ("$gt" ,0 ));
462
464
coll .remove (q );
463
- break ;
465
+ break ;
464
466
}
465
467
catch (MongoException e ){
466
468
attempts ++;
0 commit comments