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.

10 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. Nice to see your blog post.. I really enjoyed by reading your blog post. Thanks a lot for sharing this with us.. Keep on sharing like this informative post.

    Salesforce Training in Chennai

    ReplyDelete
  3. Wow amazing i saw the article with execution models you had posted. It was such informative. Really its a wonderful article. Thank you for sharing and please keep update like this type of article because i want to learn more relevant to this topic.

    Email Marketing Chennai

    ReplyDelete
  4. 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
  5. 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
  6. 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
  7. 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.
    Fresher Jobs
    Fresher Openings

    ReplyDelete
  8. 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
  9. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    Fresher Jobs in Mumbai
    Fresher Jobs in Pune
    Fresher Jobs in Noida
    Fresher Jobs in Hyderabad

    ReplyDelete
  10. 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