JTraining Blog

Share your knowledge.
Tags >> Cache
admin
admin

Interesting article from GeekListed. There has been excellent coverage on how to attain the most performance out of your Seam-based Web applications. One aspect of improving Seam performance that, in their opinion, has received less attention, is the framework’s excellent built-in support for caching of entity java beans and query results.

 

In this article, they will briefly describe the steps they  took in order to configure some basic to intermediate-level caching throughout our system. Although they didn't go as far as to gather metrics on the performance gain, the entire team roughly noticed a 30-50% improvement across the board – and all from some fairly easy work.

Check the source for the full article.

Tagged in: Cache
sneake75@hotmail.com
sneake75@hotmail.com

Introduction:

In part 1 we talked about Caching introduction and some terminologies of caching and in part 2 and part 3 we have seen some implementation of the famous replacement cache algorithms and now in this part we will see comparison between open source java caching frameworks as I am not that rich to buy commercial frameworks :D.
In this part we will talking about OSCache,Ehcache,JCS and Cache4J and we are going to concentrate on memory caching only, there will be performance comparison based on in memory caching by using JBoss caching benchmark framework and other test cases for cache.

The Task:

“Programming Mania” is a famous programming magazine from geeks to geeks every release from the magazine there a section specialized in frameworks comparison like MVC, ORM and so on, this month they decided that they are going to make a comparison about caching frameworks

And as we know the editors have programmatic background, in fact they are real programmers (not fake ones).



Tagged in: Frameworks , Caching , Cache , Algorithms
sneake75@hotmail.com
sneake75@hotmail.com

Introduction:

In part 1 we talked about the basics and terminologies of cache and we have also shown replacement policies , in part 2 we implemented some of these famous replacement polices and now in this part we will continue talking about the implementation of two famous algorithms which are LFU and LRU. Again, the implementation in this article is for sake of demonstration and in order to use it (we just concentrate over the replacement algorithm and we will skip other things like loading data and so on), you will have to do some extra work but you can base your implementation over it.

Meet LFU Cache Implementation:

public synchronized Object getElement(Object key) {

Object obj;

obj = table.get(key);

if (obj != null) {
CacheElement element = (CacheElement) obj;
element.setHitCount(element.getHitCount() + 1);
return element.getObjectValue();
}
return null;

}

public final synchronized void addElement(Object key, Object value) {

Object obj;

obj = table.get(key);

if (obj != null) {
CacheElement element;

// Just replace the value.
element = (CacheElement) obj;
element.setObjectValue(value);
element.setObjectKey(key);

return;
}

if (!isFull()) {

index = numEntries;
++numEntries;
} else {
CacheElement element = removeLfuElement();
index = element.getIndex();
table.remove(element.getObjectKey());

}

cache[index].setObjectValue(value);
cache[index].setObjectKey(key);
cache[index].setIndex(index);
table.put(key, cache[index]);
}

public CacheElement removeLfuElement() {

CacheElement[] elements = getElementsFromTable();
CacheElement leastElement = leastHit(elements);
return leastElement;
}

public static CacheElement leastHit(CacheElement[] elements) {

CacheElement lowestElement = null;
for (int i = 0; i < elements.length; i++) {
CacheElement element = elements[i];
if (lowestElement == null) {
lowestElement = element;

} else {
if (element.getHitCount() < lowestElement.getHitCount()) {
lowestElement = element;
}
}
}
return lowestElement;
}
Analyzing LFU Cache Code (Talk Show):


Presenter: it is getting hotter and hotter now, our next contestant is LFU cache, please make some noise for it.

Audience began to scream for LFU which made LFU hesitated.

Hello, I am LFU, when the cache client want to add a new element and cache is full (no enough room for the new entry) I will have to kick out the least frequently used entry, by using the help of the removelfuElement method which will allow me to get the least frequently used element, after I get it, I will remove this entry and place the new entry


else {
CacheElement element = removeLfuElement();
index = element.getIndex();
table.remove(element.getObjectKey());

}


If we dived into this method…, I am saying if we dived into this method (still nothing happened)
LFU tried pressing the next button on the presentation remote control (to get the next presentation slide) but I didn’t work.

Ahh now we are talking, ok if we dived into this method we will see that the method is just getting the whole elements in cache by calling getElementsFromTable method and then returns the element with the least hit.


public CacheElement removeLfuElement() {

CacheElement[] elements = getElementsFromTable();
CacheElement leastElement = leastHit(elements);
return leastElement;
}
}


By calling leastHit method which loops over the cache elements and check if the current element has the least hit, if so, I will make it my lowestElement which I am going replace the new entry with.

public static CacheElement leastHit(CacheElement[] elements) {

CacheElement lowestElement = null;
for (int i = 0; i <>
CacheElement element = elements[i];
if (lowestElement == null)
{ lowestElement = element; }
else {

if (element.getHitCount() <>
{ lowestElement = element; }
}
}
return lowestElement;
}

LFU stopped talking and waited for any action from the audience and the only action it get was scratching heads (audience didn’t get some stuff).

One of the production team whispered to LFU cache and said: you didn’t mention how the lowest element will be distinguished from another element?

Then LFU cache started talking gain and said: By default when you add the element to the cache its hitCoint will be the same as the previous element so how do we handle the hit count thing?

Every time I encounter a cache hit I will increment the hit count of the entry and then return the entry the cache client asked for which would be something like that


public synchronized Object getElement(Object key) {

Object obj;

obj = table.get(key);

if (obj != null) {
CacheElement element = (CacheElement) obj;
element.setHitCount(element.getHitCount() + 1);
return element.getObjectValue();
}
return null;

}


Magnifying the Code:

Did anyone say magnification?

 























































































































































Tagged in: Frameworks , Cache , Algorithms

Tags:

1.4 M1 2.1.1 6.10 2010 6.9.1 97 things Access Program advanced Agile Ajax Ajax4JSF Algorithms Applet applications Atlanta Atmosphere Audit available BDD beginner Behaviour beta blind BPM Build bus C# Cache Caching Certification Cloud Cloud computing code code to coast collection Computing Continuous Integration Database Databinding DC Debugging Develop developer development dienst download Driven Early Eclipse Editor EJB Enterprise event Events ExternalNews ExtJS Extreme Programming Facelets features Flex Flight recorder Framework Frameworks free GlassFish Grid Groovy Hat HD Hello World Hibernate HowTo ICT IDE IDEA installation Integration IntelliJ introduction iPhone Java Java EE Java EE 6 java road trip Java SE Java SE 6 JavaFX JavaFX Composer javascript JavaZone jaxws JBehave JBoss JDK 7 JetBrains JNI JPA jQuery JRebel JRebel 3.0 JRockit JRockit Mission Control JSF JSON JTraining jtraining.com JVM klant know lady Leak library license Lift Loom Lucene M1 Mark Maven Memory Milestone 1 milestones mkb music Netbeans NTNU oJava OOP Open Open Source Oracle ORM oslo patch 7 Performance PHP Plugin prizes programmer programming project project management Puzzles QA RAP Red Reflection Reinhold related Release Reuse Rich Ajax Platform RichFaces Road SaaS sample Scala Scaling Script Scrum Seam Server should soap software Software as a Service Source Spring Spring Web Flow SQL String Student Sun sweepstakes Swing Teaching Testing tools Tour Training Transaction Tutorial Update 21 validation video web Web applications web service Web Services Weblogic wiki XP

Sponsers