Wednesday, March 12, 2014

Using FIFO with cacheSetProperties

Although it is not explicitly stated in the CF10 docs, some testing today verified that a "FIFO" algorithm does function when provided as part of a cache property configuration.

FIFO is a "first-in, first out" algorithm, which means the oldest object in my cache will be the first one to be flushed out once my cache reaches its maximum size.

First let's create a cache with FIFO.

<cfscript>


CacheRegionNew(
"testfifo",
{
DISKPERSISTENT=false,
MAXELEMENTSINMEMORY=7,
STATISTICS=true,
OBJECTTYPE='object',
MEMORYEVICTIONPOLICY='fifo'
}
);

</cfscript>

Note that we have set our cache up with a policy of FIFO, and given it a maximum of 7 elements.

Next, add some objects to our cache. Here, test2 is an old test object I happen to have laying around. :)

<cfscript>
for (i=1;i LTE 7; i=i+1) {

objname="obj_#i#";
cacheput(
objname,
new test2(),
1,
1,
'testfifo',
true
);
sleep(100);
}
</cfscript>

Let's dump the cache. 

<cfscript>
if (CacheRegionExists("testfifo"))
{
writedump(
CacheGetAllIds('testfifo')
);
}
</cfscript>

We should see this.





We now have 7 objects in our cache. Just to prove that our cache is not running with a different algorithm, do a cacheGet on the oldest object. ('OBJ_1'). 

<cfscript>

x = cacheGet(
'obj_1',
'testfifo'
);

</cfscript>

If you wish, you can dump our first object and verify it has received a cache hit.

<cfscript>
writeDump(
cacheGetMetaData(
'obj_1',
'object',
'testfifo'
)
);

</cfscript>



We are now ready to begin implicitly evicting items from the cache. We do this by adding a new object to the cache.

<cfscript>
objname="cacheitem_#getTickCount()#";
cacheput(
objname,
new test2(),
1,
1,
'testfifo',
true
);

</cfscript>

If our FIFO cache is working correctly, our first object "OBJ_1" should now be gone from the cache since the cache is full and it was our oldest object.

Dump the region again.

<cfscript>
if (CacheRegionExists("testfifo"))
{
writedump(
CacheGetAllIds('testfifo')
);
}
</cfscript>




That's it. Our oldest object has been evicted from the cache. If our cache had been running with LRU (Least Recently Used) or LFU (Less Frequently Used), a different object would have been flushed. In both of those scenarios "OBJ_1" would have been maintained in the cache because of the cache hit it received.