Skip to content

Commit a20368b

Browse files
committed
merging actual query with expire control in flushInvalid()
Removing timeidle control inprove 20% speed
1 parent d8e3e83 commit a20368b

File tree

4 files changed

+66
-64
lines changed

4 files changed

+66
-64
lines changed

webroot/extensions/mongodb-cache-extension/config.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
<config>
33
<info>
44
<id>10EEC23A-0779-4068-9507A9C5ED4A8699</id>
5-
<version>1.2.201102082243</version>
5+
<version>1.2.201102082254</version>
66
<name>MongoDBCache</name>
77
<type>all</type>
88
<label>MongoDb Cache</label>
99
<description>MongoDb Cache Extension</description>
10-
<created>2011-February-8 22:43</created>
10+
<created>2011-February-8 22:54</created>
1111
<category>Core Extension</category>
1212
<author>Andrea Campolonghi</author>
1313
<image>http://preview.getrailo.org/logos/mongodb-logo.png</image>
Binary file not shown.

webroot/extensions/mongodb-cache-extension/src/railo/extension/io/cache/mongodb/MongoDBCache.java

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ public void init(String cacheName, Struct arguments) throws IOException {
9494
e.printStackTrace();
9595
}
9696
}
97-
97+
9898
public void init(Config config ,String[] cacheName,Struct[] arguments){
9999
//Not used at the moment
100100
}
101-
101+
102102
public void init(Config config, String cacheName, Struct arguments) {
103103
try {
104104
init(cacheName,arguments);
@@ -114,12 +114,12 @@ protected void crateIndexes(){
114114
coll.createIndex(new BasicDBObject("timeIdle", 1));
115115
coll.createIndex(new BasicDBObject("expires", 1));
116116
}
117-
117+
118118
protected void startCleaner(){
119119
Timer timer = new Timer();
120120
timer.schedule(new MongoDbCleanTask(this), 0,10000);
121121
}
122-
122+
123123
@Override
124124
public boolean contains(String key) {
125125
BasicDBObject query = new BasicDBObject();
@@ -132,56 +132,56 @@ public boolean contains(String key) {
132132
public List entries() {
133133
List result = new ArrayList<CacheEntry>();
134134
DBCursor cur = qAll();
135-
135+
136136
while(cur.hasNext()){
137137
BasicDBObject obj = (BasicDBObject)cur.next();
138138
MongoDBCacheDocument doc = new MongoDBCacheDocument(obj);
139139
result.add(new MongoDBCacheEntry(doc));
140-
}
141-
140+
}
141+
142142
return result;
143143
}
144144

145145
@Override
146146
public List entries(CacheKeyFilter filter) {
147147
List result = new ArrayList<CacheEntry>();
148148
DBCursor cur = qAll();
149-
149+
150150
while(cur.hasNext()){
151151
BasicDBObject obj = (BasicDBObject)cur.next();
152152
MongoDBCacheDocument doc = new MongoDBCacheDocument(obj);
153153
if(filter.accept(doc.getKey())){
154-
result.add(new MongoDBCacheEntry(doc));
154+
result.add(new MongoDBCacheEntry(doc));
155155
}
156-
}
156+
}
157157
return result;
158158
}
159159

160160
@Override
161161
public List entries(CacheEntryFilter filter) {
162162
List result = new ArrayList<CacheEntry>();
163163
DBCursor cur = qAll();
164-
164+
165165
while(cur.hasNext()){
166166
BasicDBObject obj = (BasicDBObject)cur.next();
167167
MongoDBCacheDocument doc = new MongoDBCacheDocument(obj);
168168
MongoDBCacheEntry entry = new MongoDBCacheEntry(doc);
169169
if(filter.accept(entry)){
170-
result.add(entry);
170+
result.add(entry);
171171
}
172-
}
172+
}
173173
return result;
174174
}
175175

176176
@Override
177177
public MongoDBCacheEntry getCacheEntry(String key) throws CacheException {
178-
Integer attempts = 0;
178+
Integer attempts = 0;
179179
DBCursor cur = null;
180180
BasicDBObject query = new BasicDBObject("key", key.toLowerCase());
181-
181+
182182
// be sure to flush
183-
flushInvalid();
184-
183+
flushInvalid(query);
184+
185185
while(attempts <= maxRetry){
186186
try{
187187
cur = coll.find(query);
@@ -193,20 +193,20 @@ public MongoDBCacheEntry getCacheEntry(String key) throws CacheException {
193193
e.printStackTrace();
194194
}
195195
}
196-
196+
197197
}
198-
198+
199199
if(cur.count() > 0){
200200
hits++;
201201
MongoDBCacheDocument doc = new MongoDBCacheDocument((BasicDBObject) cur.next());
202202
doc.addHit();
203203
//update the statistic and persist
204204
save(doc);
205-
return new MongoDBCacheEntry(doc);
205+
return new MongoDBCacheEntry(doc);
206206
}
207207
misses++;
208208
throw(new CacheException("The document with key [" + key +"] has not been found int this cache."));
209-
209+
210210
}
211211

212212
@Override
@@ -230,7 +230,7 @@ public Object getValue(String key) throws IOException {
230230
try{
231231
MongoDBCacheEntry entry = getCacheEntry(key);
232232
Object result = entry.getValue();
233-
return result;
233+
return result;
234234
}catch(IOException e){
235235
throw(e);
236236
}
@@ -240,7 +240,7 @@ public Object getValue(String key) throws IOException {
240240
public Object getValue(String key, Object defaultValue){
241241
try{
242242
Object value = getValue(key.toLowerCase());
243-
return value;
243+
return value;
244244
}catch(IOException e){
245245
return defaultValue;
246246
}
@@ -255,7 +255,7 @@ public long hitCount() {
255255
public List keys() {
256256
List result = new ArrayList<String>();
257257
DBCursor cur = qAll_Keys();
258-
258+
259259
if(cur.count() > 0){
260260
while(cur.hasNext()){
261261
result.add(cur.next().get("key"));
@@ -268,12 +268,12 @@ public List keys() {
268268
public List keys(CacheKeyFilter filter) {
269269
List result = new ArrayList<String>();
270270
DBCursor cur = qAll_Keys();
271-
271+
272272
if(cur.count() > 0){
273273
while(cur.hasNext()){
274274
String key = new MongoDBCacheDocument((BasicDBObject) cur.next()).getKey();
275275
if(filter.accept(key)){
276-
result.add(key);
276+
result.add(key);
277277
}
278278
}
279279
}
@@ -284,12 +284,12 @@ public List keys(CacheKeyFilter filter) {
284284
public List keys(CacheEntryFilter filter) {
285285
List result = new ArrayList<String>();
286286
DBCursor cur = qAll();
287-
287+
288288
if(cur.count() > 0){
289289
while(cur.hasNext()){
290290
MongoDBCacheEntry entry = new MongoDBCacheEntry(new MongoDBCacheDocument((BasicDBObject) cur.next()));
291291
if(filter.accept(entry)){
292-
result.add(entry.getKey());
292+
result.add(entry.getKey());
293293
}
294294
}
295295
}
@@ -305,38 +305,38 @@ public long missCount() {
305305
public void put(String key, Object value, Long idleTime, Long lifeSpan) {
306306
CFMLEngine engine = CFMLEngineFactory.getInstance();
307307
Cast caster = engine.getCastUtil();
308-
308+
309309
Long created = System.currentTimeMillis();
310310
int create = created.intValue();
311311
int idle = idleTime==null?0:idleTime.intValue();
312312
int life = lifeSpan==null?0:lifeSpan.intValue();
313313

314314
BasicDBObject obj = new BasicDBObject();
315315
MongoDBCacheDocument doc = new MongoDBCacheDocument(obj);
316-
316+
317317
try{
318318

319319
doc.setData(func.serialize(value));
320320
doc.setCreatedOn(create);
321321
doc.setTimeIdle(idle);
322322
doc.setLifeSpan(life);
323323
doc.setHits(0);
324-
324+
325325
int expires = life + create;
326326
if(life == 0){
327-
doc.setExpires(0);
327+
doc.setExpires(0);
328328
}else{
329-
doc.setExpires(expires);
329+
doc.setExpires(expires);
330330
}
331331

332332
}
333333
catch(PageException e){
334334
e.printStackTrace();
335335
}
336-
336+
337337
doc.setKey(key.toLowerCase());
338338
save(doc);
339-
339+
340340
}
341341

342342
@Override
@@ -355,112 +355,114 @@ public boolean remove(String key) {
355355
public int remove(CacheKeyFilter filter) {
356356
DBCursor cur = qAll_Keys();
357357
int counter = 0;
358-
358+
359359
while(cur.hasNext()){
360360
DBObject obj = cur.next();
361-
String key = (String)obj.get("key");
361+
String key = (String)obj.get("key");
362362
if(filter.accept(key)){
363363
doDelete((BasicDBObject)obj);
364-
counter++;
364+
counter++;
365365
}
366366
}
367-
367+
368368
return counter;
369369
}
370370

371371
@Override
372-
public int remove(CacheEntryFilter filter) {
372+
public int remove(CacheEntryFilter filter) {
373373
DBCursor cur = qAll();
374374
int counter = 0;
375-
375+
376376
while(cur.hasNext()){
377377
BasicDBObject obj = (BasicDBObject) cur.next();
378378
MongoDBCacheEntry entry = new MongoDBCacheEntry(new MongoDBCacheDocument(obj));
379379
if(filter.accept(entry)){
380380
doDelete(obj);
381-
counter++;
381+
counter++;
382382
}
383383
}
384-
384+
385385
return counter;
386386
}
387387

388388
@Override
389389
public List values() {
390390
DBCursor cur = qAll_Values();
391391
List result = new ArrayList<Object>();
392-
392+
393393
while(cur.hasNext()){
394394
Object value = new MongoDBCacheDocument((BasicDBObject) cur.next()).getData();
395395
try{
396-
result.add(func.evaluate(value));
396+
result.add(func.evaluate(value));
397397
}catch(PageException e){
398398
e.printStackTrace();
399399
}
400400
}
401-
401+
402402
return result;
403403
}
404404

405405
@Override
406406
public List values(CacheKeyFilter filter) {
407407
DBCursor cur = qAll_Keys_Values();
408408
List result = new ArrayList<Object>();
409-
409+
410410
while(cur.hasNext()){
411411
BasicDBObject obj = (BasicDBObject) cur.next();
412412
MongoDBCacheDocument doc = new MongoDBCacheDocument(obj);
413413

414414
if(filter.accept(doc.getKey())){
415415
Object value = new MongoDBCacheDocument(obj).getData();
416416
try{
417-
result.add(func.evaluate(value));
417+
result.add(func.evaluate(value));
418418
}catch(PageException e){
419419
e.printStackTrace();
420-
}
420+
}
421421
}
422-
422+
423423
}
424-
424+
425425
return result;
426426
}
427427

428428
@Override
429429
public List values(CacheEntryFilter filter) {
430430
DBCursor cur = qAll_Keys_Values();
431431
List result = new ArrayList<Object>();
432-
432+
433433
while(cur.hasNext()){
434434
BasicDBObject obj = (BasicDBObject) cur.next();
435435
MongoDBCacheEntry entry = new MongoDBCacheEntry(new MongoDBCacheDocument(obj));
436436

437437
if(filter.accept(entry)){
438438
Object value = new MongoDBCacheDocument(obj).getData();
439439
try{
440-
result.add(func.evaluate(value));
440+
result.add(func.evaluate(value));
441441
}catch(PageException e){
442442
e.printStackTrace();
443-
}
443+
}
444444
}
445-
445+
446446
}
447-
447+
448448
return result;
449449
}
450450

451-
452-
protected void flushInvalid(){
453-
int attempts = 0;
451+
452+
protected void flushInvalid(BasicDBObject query){
453+
Integer attempts = 0;
454+
DBCursor cur = null;
454455
Long now = System.currentTimeMillis();
455456
int nowi = now.intValue();
457+
BasicDBObject q = (BasicDBObject)query.clone();
456458

457459
//execute the query
458460
while(attempts <= maxRetry){
459461
try{
460462

461-
BasicDBObject q = new BasicDBObject("expires", new BasicDBObject("$lt",nowi).append("$gt",0));
463+
q.append("expires", new BasicDBObject("$lt",now).append("$gt",0));
462464
coll.remove(q);
463-
break;
465+
break;
464466
}
465467
catch(MongoException e){
466468
attempts++;

webroot/extensions/mongodb-cache-extension/src/railo/extension/io/cache/mongodb/MongoDbCleanTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public MongoDbCleanTask(MongoDBCache cache) {
1515
@Override
1616
public void run() {
1717
try{
18-
this.cache.flushInvalid();
18+
this.cache.flushInvalid(new BasicDBObject());
1919
}catch(Exception e){
2020
//just fails so that task does not die
2121
}

0 commit comments

Comments
 (0)