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.

Friday, January 10, 2014

cfprocessingdirective

For years we have been using the tag cfprocessingdirective Tag at the top of our templates like this.

<cfprocessingdirective suppresswhitespace="yes" pageencoding="utf-8">

Then one of our colleagues found this in the Adobe Docs

"The cfprocssingdirective tag has limitations that depend on the attribute you use. For this reason, Adobe recommends that you include either the pageencoding or suppresswhitespace attribute in a cfprocessingdirective tag, not both." 

Ah hah. Afterwards we began to modify the templates.

<cfprocessingdirective pageEncoding="utf-8" />
<cfprocessingdirective suppressWhitespace="true">
...[some code]
</cfprocessingdirective>

It struck me however after playing around with the tag that the suppressWhitespace attribute did not seem to have an effect on the white space emitted. The white space was being stripped out regardless whether the tag was present or not. A quick check showed me that the "Enable Whitespace Management" Option on the server was active.

Thus it appears this setting essentially replicates at the page level the global setting for Whitespace Management.