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.

8 comments:

  1. very cool idea and the subject at the top of magnificence and I am happy to this post..Interesting post! Thanks for writing it. What's wrong with this kind of post exactly? It follows your previous guideline for post length as well as clarity..

    Android Training in Chennai

    ReplyDelete
  2. Very nice post here and thanks for latest smartphone applications it .I always like and such a super colors of phone for these post.Excellent and very cool idea and great models and different kinds of the more information's.

    digital marketing course in chennai

    ReplyDelete
  3. That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
    SAT Coaching Chennai

    ReplyDelete
  4. i like that kind of information,not only i like that post all peoples like that post,because of all given information was very excellent.
    Best Dental Clinic in Anna Nagar | Best Dental Clinic in Ashok Nagar

    ReplyDelete
  5. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
    GMAT Training in Chennai

    ReplyDelete
  6. Great post!! This can be one particular of the most useful blogs We’ve ever arrive across on this subject. Basically Wonderful. I am also a specialist in this topic so I can understand your hard work.
    Office Interior Designers in Coimbatore
    Office Interior Designers in Bangalore
    Office Interior Designers in Hyderabad

    ReplyDelete
  7. It is really a great and useful piece of info. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thank you for sharing.
    Logistics management software
    Human resources management software
    CCTV Camera Dealers in Chennai
    ERP for the manufacturing industry
    Warehouse Management Software

    ReplyDelete
  8. The Interior Designer is a plans, researches, coordinates, and manages the projects. Interior design is a multifaceted profession that includes conceptual development, space planning, site inspections, programming, research, communicating with the stakeholders of a project, construction management, and execution of the design.

    Interior Designers in OMR

    ReplyDelete