Java and Caching: by Martin Nad
Java and Caching: by Martin Nad
CACHING
By Martin Nad
6/26/2009
martin_m_nad@yahoo.com
Java and cashing
OVERVIEW
Here I want to show why and you should use cache, and
presents some different kind of caching system in java, and
give you an example by on chaching system.
Page 2
Java and cashing
TABLE OF CONTEXT
Foreword
Caching
Different kind of caching in java
ehCache example
Page 3
Java and cashing
FORWORD
Page 4
Java and cashing
CACHING
Page 5
Java and cashing
Page 6
Java and cashing
2- OsCache: http://www.opensymphony.com/oscache/
It is used to generate dynamically jsp-pages and it
includes the jsp-library.
You can use it in the large system.
Some advantages:
- It has support all, memory, disc and some basic
support for clustering-cache system by using JMS,
JavaGroup.
- It has upport for read-only, read/write caching
3- SwarmCache: http://swarmcache.sourceforge.net/
It has simple support for clustering cache system.
It has support for read-only, read/write caching
It is very good to use this caching-method when you have
many read operation than write operation.
4-Jboss TreeCache
Page 7
Java and cashing
Caching planning:
As you can see there are different caching packages that you
can use them on what you want to do
We have at least 4 different caching plan:
1-read only; as you can guess this plan is good if you have
just data that it doesn’t ever change. You can use Ehcache or
OsCache
2- read/write: in this case the data needs to update,
frequently. You can EhCache or OsCache
3-nonStrick read/write. It doesn’t guaranty that any update
shouldn’t take place more than once. You can use
SwarmCache
4-transactional caching plan. You can use this plan for JTA
(Java transactional API). You can use Jobs treeCache
Page 8
Java and cashing
EHCACHE EXAMPLE
<diskStore path="java.io.tmpdir"/>
Page 9
Java and cashing
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<cache name="sampleCache1"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
/>
</ehcache>
import java.io.Serializable;
/**
*
*/
private static final long serialVersionUID = 1L;
private String name = "";
private String address = "";
public Customer () {
// Default constructor.
}
Page 10
Java and cashing
package martin.nad.cache.ehcache.exampel1;
/**
*
*/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import net.sf.ehcache.*;
Page 11
Java and cashing
{
public static void main( String[] args )
{
App app = new App();
try {
app.manager =
CacheManager.create("c:/tmp/enCache.xml");
System.out.println("manager
is...."+app.manager);
app.cache =
app.manager.getCache("sampleCache1");
} catch (Exception e) {
System.out.println(e);
}
read_writeInCache();
Page 12
Java and cashing
Page 13
Java and cashing
Customer my2customer =
(Customer)element.getValue();
And just run, and you are done with your first caching-
system.
If you want use in your servlet, your servlet can look like:
Page 14
Java and cashing
package martin.nad.cache.ehcache.exampel1;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import net.sf.ehcache.*;
Page 15
Java and cashing
PropertyConfigurator.configure( getServletContext().getRealP
ath ("WEB-INF/log4j.properties") );
try {
manager =
CacheManager.create(getServletContext().getRealPath
("WEB-INF/ehCache.xml"));
cache = manager.getCache("sampleCache1");
} catch (Exception e) {
logger.error("Unable to load EHCACHE
configuration file");
}
Page 16
Java and cashing
Page 17
Java and cashing
Customer myNewCustomer =
(Customer)element.getValue();
out.println("Name : " +
myNewCustomer.getName() +"<br/>");
out.println("Address : " +
myNewCustomer.getAddress() +"<br/>");
}
}
Page 18
Java and cashing
OSCACHE
DONATION
MY OTHER PAPERS
Page 19
Java and cashing
Page 20