Skip to content

Commit d8f1350

Browse files
committed
fixed timeSspan and timeIdle support
1 parent b35daf6 commit d8f1350

File tree

10 files changed

+83
-77
lines changed

10 files changed

+83
-77
lines changed

webroot/cachetests/cacheClear.cfm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,3 @@
3030
</cflock>
3131

3232
</cfif>
33-
<cfabort>

webroot/cachetests/cacheExpire.cfm

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
<cfcache action="flush">
1+
<cfset cacheClear()>
22
<cfcache action="put" id="peter" value="Peter">
33
<cfcache action="put" id="susi" value="Susanna" timespan="#createTimeSpan(0,0,0,2)#">
4+
<cfcache action="put" id="andrea" value="Andrea" timespan="#createTimeSpan(0,0,0,10)#">
45
<cfset sleep(3000)>
56
<cfcache action="get" id="susi" name="s">
67
<cfcache action="get" id="peter" name="p">
7-
8+
<cfcache action="get" id="andrea" name="a">
89
<cf_valueEquals left="#isDefined('s')#" right="false">
910
<cf_valueEquals left="#isDefined('p')#" right="true">
11+
<cf_valueEquals left="#isDefined('a')#" right="true">
12+
1013

11-
<cfcache action="flush">
14+
<cfset cacheClear()>
1215
<cfcache action="put" id="peter" value="Peter">
1316
<cfcache action="put" id="susi" value="Susanna" idletime="#createTimeSpan(0,0,0,2)#">
1417
<cfset sleep(3000)>
1518
<cfcache action="get" id="susi" name="s">
1619
<cfcache action="get" id="peter" name="p">
17-
1820
<cf_valueEquals left="#isDefined('s')#" right="false">
1921
<cf_valueEquals left="#isDefined('p')#" right="true">
22+
23+

webroot/cachetests/cacheGetAll.cfm

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
<cflock scope="server" timeout="1">
22
<cfif server.ColdFusion.ProductName EQ "railo">
33

4-
<cfset cacheName="sample">
54
<cfset cacheClear()>
65

76
<cfset cachePut('abc','123')>
87
<cfset cachePut('def','123')>
9-
<cf_valueEquals left="#ListSort(StructKeyList(cacheGetAll()),'textnocase')#" right="ABC,DEF" cs=true>
8+
<cf_valueEquals left="#ListSort(StructKeyList(cacheGetAll()),'textnocase')#" right="abc,def" cs=true>
109

1110
<cfset cachePut('abc','123')>
1211
<cfset cachePut('abd','123')>
1312
<cfset cachePut('def','123')>
14-
<cf_valueEquals left="#ListSort(StructKeyList(cacheGetAll("ab*")),'textnocase')#" right="ABC,ABD" cs=true>
15-
<cf_valueEquals left="#ListSort(StructKeyList(cacheGetAll("ab*")),'textnocase')#" right="ABC,ABD" cs=true>
13+
<cf_valueEquals left="#ListSort(StructKeyList(cacheGetAll("ab*")),'textnocase')#" right="abc,abd" cs=true>
14+
<cf_valueEquals left="#ListSort(StructKeyList(cacheGetAll("ab*")),'textnocase')#" right="abc,abd" cs=true>
1615
</cfif>
1716
</cflock>

webroot/cachetests/cacheGetAllIds.cfm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
<cfset cachePut('abc','123')>
1010
<cfset cachePut('def','123')>
11-
<cf_valueEquals left="#ListSort(arrayToList(cacheGetAllIds()),'textnocase')#" right="ABC,DEF" cs=true>
11+
<cf_valueEquals left="#ListSort(arrayToList(cacheGetAllIds()),'textnocase')#" right="abc,def" cs=true>
1212

1313
<cfif server.ColdFusion.ProductName EQ "railo">
1414
<cfset cacheClear()>
1515
<cfset cachePut('abc','123')>
1616
<cfset cachePut('abd','123')>
1717
<cfset cachePut('def','123')>
18-
<cf_valueEquals left="#ListSort(arrayToList(cacheGetAllIds("ab*")),'textnocase')#" right="ABC,ABD" cs=true>
19-
<cf_valueEquals left="#ListSort(arrayToList(cacheGetAllIds("ab*")),'textnocase')#" right="ABC,ABD" cs=true>
18+
<cf_valueEquals left="#ListSort(arrayToList(cacheGetAllIds("ab*")),'textnocase')#" right="abc,abd" cs=true>
19+
<cf_valueEquals left="#ListSort(arrayToList(cacheGetAllIds("ab*")),'textnocase')#" right="abc,abd" cs=true>
2020
</cfif>
2121
</cflock>

webroot/cachetests/cachePut.cfm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<cfset server.enableCache=true>
2+
<cfset cacheClear()>
23

34
<cflock scope="server" timeout="10">
45
<cfset cacheName="mongotest">
@@ -9,8 +10,8 @@
910
<cfset cachePut('ghi','_cachePut',CreateTimeSpan(0,0,0,0),CreateTimeSpan(0,0,0,0))>
1011

1112
<cfset a=cacheGet('abc')>
12-
<cfset b=cacheGet('def')>
13-
<cfset c=cacheGet('ghi')>
13+
<cfset b=cacheGet('def')>
14+
<cfset c=cacheGet('ghi')>
1415

1516
<cf_valueEquals left="#structKeyExists(variables,'a')#" right="true">
1617
<cf_valueEquals left="#structKeyExists(variables,'b')#" right="true">

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.1.201011112026</version>
5+
<version>1.1.201011122239</version>
66
<name>MongoDBCache</name>
77
<type>server</type>
88
<label>MongoDb Cache</label>
99
<description>MongoDb Cache Extension</description>
10-
<created>2010-November-11 20:26</created>
10+
<created>2010-November-12 22:39</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: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void init(String cacheName, Struct arguments) throws IOException {
8888
crateIndexes();
8989

9090
// start the cleaner schdule that remove entries by expires time and idle time
91-
startCleaner();
91+
//startCleaner();
9292

9393

9494
} catch (Exception e) {
@@ -118,7 +118,7 @@ protected void crateIndexes(){
118118

119119
protected void startCleaner(){
120120
Timer timer = new Timer();
121-
timer.schedule(new MongoDbCleanTask(this), 0,2000);
121+
timer.schedule(new MongoDbCleanTask(this), 0,10000);
122122
}
123123

124124
@Override
@@ -134,12 +134,11 @@ public List entries() {
134134
List result = new ArrayList<CacheEntry>();
135135
DBCursor cur = qAll();
136136

137-
if(cur.count() > 0){
138-
while(cur.hasNext()){
139-
MongoDBCacheDocument doc = new MongoDBCacheDocument((BasicDBObject) cur.next());
140-
result.add(new MongoDBCacheEntry(doc));
141-
}
142-
}
137+
while(cur.hasNext()){
138+
BasicDBObject obj = (BasicDBObject)cur.next();
139+
MongoDBCacheDocument doc = new MongoDBCacheDocument(obj);
140+
result.add(new MongoDBCacheEntry(doc));
141+
}
143142

144143
return result;
145144
}
@@ -149,14 +148,13 @@ public List entries(CacheKeyFilter filter) {
149148
List result = new ArrayList<CacheEntry>();
150149
DBCursor cur = qAll();
151150

152-
if(cur.count() > 0){
153-
while(cur.hasNext()){
154-
MongoDBCacheDocument doc = new MongoDBCacheDocument((BasicDBObject) cur.next());
155-
if(filter.accept(doc.getKey())){
156-
result.add(new MongoDBCacheEntry(doc));
157-
}
158-
}
159-
}
151+
while(cur.hasNext()){
152+
BasicDBObject obj = (BasicDBObject)cur.next();
153+
MongoDBCacheDocument doc = new MongoDBCacheDocument(obj);
154+
if(filter.accept(doc.getKey())){
155+
result.add(new MongoDBCacheEntry(doc));
156+
}
157+
}
160158
return result;
161159
}
162160

@@ -165,25 +163,26 @@ public List entries(CacheEntryFilter filter) {
165163
List result = new ArrayList<CacheEntry>();
166164
DBCursor cur = qAll();
167165

168-
if(cur.count() > 0){
169-
while(cur.hasNext()){
170-
MongoDBCacheDocument doc = new MongoDBCacheDocument((BasicDBObject) cur.next());
171-
MongoDBCacheEntry entry = new MongoDBCacheEntry(doc);
172-
if(filter.accept(entry)){
173-
result.add(entry);
174-
}
175-
}
176-
}
166+
while(cur.hasNext()){
167+
BasicDBObject obj = (BasicDBObject)cur.next();
168+
MongoDBCacheDocument doc = new MongoDBCacheDocument(obj);
169+
MongoDBCacheEntry entry = new MongoDBCacheEntry(doc);
170+
if(filter.accept(entry)){
171+
result.add(entry);
172+
}
173+
}
177174
return result;
178175
}
179176

180177
@Override
181178
public MongoDBCacheEntry getCacheEntry(String key) throws CacheException {
182179
Integer attempts = 0;
183180
DBCursor cur = null;
184-
BasicDBObject query = new BasicDBObject();
185-
query.put("key", key.toLowerCase());
186-
181+
BasicDBObject query = new BasicDBObject("key", key.toLowerCase());
182+
183+
// be sure to flush
184+
flushInvalid(query);
185+
187186
while(attempts <= maxRetry){
188187
try{
189188
cur = coll.find(query);
@@ -305,28 +304,30 @@ public long missCount() {
305304

306305
@Override
307306
public void put(String key, Object value, Long idleTime, Long lifeSpan) {
307+
CFMLEngine engine = CFMLEngineFactory.getInstance();
308+
Cast caster = engine.getCastUtil();
309+
308310
Long created = System.currentTimeMillis();
309-
long idle = idleTime==null?0:idleTime.longValue();
310-
long life = lifeSpan==null?0:lifeSpan.longValue();
311+
int create = created.intValue();
312+
int idle = idleTime==null?0:idleTime.intValue();
313+
int life = lifeSpan==null?0:lifeSpan.intValue();
311314

312315
BasicDBObject obj = new BasicDBObject();
313316
MongoDBCacheDocument doc = new MongoDBCacheDocument(obj);
314317

315318
try{
316-
Long span = new Long(life).longValue();
317-
Long timeIdle = new Long(idle).longValue();
318-
319+
319320
doc.setData(func.serialize(value));
320-
doc.setCreatedOn(created.toString());
321-
doc.setTimeIdle(timeIdle.toString());
322-
doc.setLifeSpan(span.toString());
321+
doc.setCreatedOn(create);
322+
doc.setTimeIdle(idle);
323+
doc.setLifeSpan(life);
323324
doc.setHits(0);
324325

325-
Long expires = span + created;
326-
if(span == 0){
327-
doc.setExpires(span.toString());
326+
int expires = life + create;
327+
if(life == 0){
328+
doc.setExpires(0);
328329
}else{
329-
doc.setExpires(expires.toString());
330+
doc.setExpires(expires);
330331
}
331332

332333
}
@@ -449,26 +450,26 @@ public List values(CacheEntryFilter filter) {
449450
}
450451

451452

452-
protected void flushInvalid(){
453+
protected void flushInvalid(BasicDBObject query){
453454
Integer attempts = 0;
454455
DBCursor cur = null;
455456
Long now = System.currentTimeMillis();
456-
BasicDBObject q = new BasicDBObject();
457-
//add to the query the terms to check if the item/items are not valid. Anything returned must be flushed.
458-
q.append("$where","this.expires > 0 && this.expires > " + now + " && this.timeIdle > 0 && this.timeIdle + this.lastAccessed > " + now);
459-
460-
457+
int nowi = now.intValue();
458+
BasicDBObject q = (BasicDBObject)query.clone();
459+
461460
//execute the query
462461
while(attempts <= maxRetry){
463462
try{
463+
464+
//add to the query the terms to check if the item/items are not valid. Anything returned must be flushed.
465+
q.append("$where","(this.expires != 0 && this.expires <= " + nowi + ") || (this.timeIdle != 0 && this.timeIdle + this.lastAccessed <= " + nowi + ")");
466+
464467
cur = coll.find(q);
465468

466-
if(cur.count() > 0){
467-
while(cur.hasNext()){
468-
DBObject doc = cur.next();
469-
coll.remove(doc);
470-
}
471-
}
469+
while(cur.hasNext()){
470+
DBObject doc = cur.next();
471+
coll.remove(doc);
472+
}
472473

473474
break;
474475
}
@@ -509,8 +510,8 @@ private void save(MongoDBCacheDocument doc){
509510
Integer attempts = 0;
510511
Long now = System.currentTimeMillis();
511512

512-
doc.setLastAccessed(now.toString());
513-
doc.setLastUpdated(now.toString());
513+
doc.setLastAccessed(now.intValue());
514+
doc.setLastUpdated(now.intValue());
514515
doc.addHit();
515516
/*
516517
* very atomic updated. Just the changed values are sent to db.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,39 @@ public String getKey(){
2929
return dbObject.getString("key");
3030
}
3131

32-
public void setCreatedOn(String value) {
32+
public void setCreatedOn(int value) {
3333
dbObject.put("createdOn",value);
3434
}
3535

3636
public String getCraetedOn(){
3737
return dbObject.getString("createdOn");
3838
}
3939

40-
public void setLastAccessed(String value) {
40+
public void setLastAccessed(int value) {
4141
dbObject.put("lastAccessed",value);
4242
}
4343

4444
public String getLastAccessed(){
4545
return dbObject.getString("lastAccessed");
4646
}
4747

48-
public void setLastUpdated(String value) {
48+
public void setLastUpdated(int value) {
4949
dbObject.put("lastUpdated",value);
5050
}
5151

5252
public String getLastUpdated(){
5353
return dbObject.getString("lastUpdated");
5454
}
5555

56-
public void setLifeSpan(String value) {
56+
public void setLifeSpan(int value) {
5757
dbObject.put("lifeSpan",value);
5858
}
5959

6060
public String getLifeSpan(){
6161
return dbObject.getString("lifeSpan");
6262
}
6363

64-
public void setTimeIdle(String value) {
64+
public void setTimeIdle(int value) {
6565
dbObject.put("timeIdle",value);
6666
}
6767

@@ -78,7 +78,7 @@ public int getHits(){
7878
return hits;
7979
}
8080

81-
public void setExpires(String value) {
81+
public void setExpires(int value) {
8282
dbObject.put("expires",value);
8383
}
8484

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.TimerTask;
44

5+
import com.mongodb.BasicDBObject;
6+
57
public class MongoDbCleanTask extends TimerTask {
68

79
private MongoDBCache cache;
@@ -13,7 +15,7 @@ public MongoDbCleanTask(MongoDBCache cache) {
1315
@Override
1416
public void run() {
1517
try{
16-
this.cache.flushInvalid();
18+
this.cache.flushInvalid(new BasicDBObject());
1719
}catch(Exception e){
1820
//just fails so that task does not die
1921
}

0 commit comments

Comments
 (0)