JTraining Blog
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 in part 4 we saw comparisons between some famous caching frameworks and in this part we are going to continue what we started in part 4 and as in part 4 we will concentrate only on memory caching.
The Task:
After programmer 1 released the caching article in “Programming Mania” the geek to geek magazine, he got a lot of threaten mails and terrible messages from caching geeks defending their beloved caching frameworks and warning him if he didn’t make their beloved caching framework win the contest, he will regret the day he became a programmer.
That didn’t scare our programmer and he went on completing the second part of the comparison
. ShiftOne
· WhirlyCache
· SwarmCache
· JBoss Cache
ShiftOne:
ShiftOne or as they call JOCache is a lightweight caching framework that implements several strict object caching policies which comes up with a set of cache algorithm implementations that supports in memory cache.
ShiftOne cache forces two rules for every cache:
- Max Size - each cache has a hard limit on the number of elements it will contain. When this limit is exceeded, the least valuable element is evicted. This happens immediately, on the same thread. This prevents the cache from growing uncontrollably
- Element Timeout - each cache has a maximum time that it's elements are considered valid. No element will ever be returned that exceeds this time limit. This ensures a predictable data freshness.
ShiftOne use decorator pattern in order to make it more flexible for the user to use any underneath caching product to maintain the cache.
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).
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?
In this part we are going to show how to implement some of the famous replacement algorithms as we mentioned in part 1, the code in this article is just for demonstration purpose which means you will have to do some extra effort if you want to make use of it in your application (if you are going to build your own implementation and wont use any caching frameworks)
The Leftover policy:
After programmer 1 read the article he proceeded to review the comments on this article, one of these comments were talking about leftover policy, which is named “Random Cache”
Random Cache:
Introduction:
A lot of us heard the word cache and when you ask them about caching they give you a perfect answer but they don’t know how it is built, or on which criteria I should favor this caching framework over that one and so on, in this article we are going to talk about Caching, Caching Algorithms and caching frameworks and which is better than the other.
The Interview:
Latest blog entries:
Java / Frontend jobs (Netherlands)
- Java Lead Developer / Technisch Architect - Java, J2ee, Risk, Schiphol-Rijk
- Javascript / jQuery / Ajax Developer, Den Bosch
- Java Architect, Haarlem / Randstad
- Java developer with risk management experience, Schiphol-Rijk (near Amsterdam)
- Java EE Developer, inhouse, J2EE, Java EE, Hibernate, Seam, Almere
- Hippo 7 ontwikkelaar, Haarlem / Randstad
- Java Portal ontwikkelaar, Haarlem / Randstad
- Java EE Ontwikkelaar (Java, JSP, Websphere, J2ee), Varsseveld
- Java EE Developer, Haarlem / Randstad
- Senior Java EE Developer, Almere / Randstad
- Portal / Java Front end specialist, Almere
- Enthousiaste PHP ontwikkelaar, Den Haag
- Senior Front-end Engineer Professional Services, Amsterdam
- UI Designer / Frontend developer, trading, CSS, HTML, Ajax, Amsterdam
- PHP ontwikkelaar, Eindhoven
- C# Webontwikkelaar, Visual Studio, Javascript,
Tags:
Blogs:
| evdh (45) |
|
| sneake75@hotmail.com (7) |
|
| svanhugten (21) |
|
| dhrubo (2) |
|
| Bram (29) |
|
| itajun (3) |
|
| farmerzen (2) |
|
| ppow (9) |
|
| fsalinas (4) |
|
| Casyi (13) |
|
| admin (4) |
|
| rpnman (2) |
|
| JavaGuy (2) |
|
| ludovicc (1) |
|
| archie (1) |
|
| gcsaba2 (1) |
|
| david (1) |
|
| JaywayPhil (1) |
|
| mironsadziak (2) |
|
| Zhak (3) |
|
Home
Community