skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Thursday, September 20, 2012

Caching implementation using Static variable

Posted by Admin at 12:38 PM – 0 comments
 
In we application perfromance can be improved by caching some Datatable in Memory. Using the given code data can be cached on application startup

web.xml
This entry is to be made if caching needs to be done on startup of server
<servlet>
  <description>
  </description>
  <display-name>CacheMyData</display-name>
  <servlet-name>CacheMyData</servlet-name>
  <servlet-class>com.ultimatix.framework.CacheMyData</servlet-class>
  <load-on-startup>2</load-on-startup>
 </servlet>


CacheMyData.java
//This is the servlet which will initialize the Instance of the Data to be cached 
 
 
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
/**
 * Servlet implementation class CacheMyData
 */
public class CacheMyData extends HttpServlet {
 private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CacheMyData() {
        super();
        // TODO Auto-generated constructor stub
    }

 /**
  * @see Servlet#init(ServletConfig)
  */
 public void init(ServletConfig config) throws ServletException {
  // TODO Auto-generated method stub
     
  CacheDataVO.getInstance();
  
 }

}


CacheDataVO.java
//This is the class where Cached Data will be stored in a static variable
//From any Bean of the project the instance of this class can be called and Data can be retrieved
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class CacheDataVO{
 
 
 //these are the static members which will be initialized
 private static HashMap<string list="list"> menuHashMap = new HashMap<string list="list">();
 private static CacheDataVO cacheDataVO= null; 
 
 private CacheDataVO(){
  
  menuHashMap.put("Key","MyDataGoesHere");
 
 
 } 
 
 public static CacheDataVO getInstance(){  
  if(cacheDataVO == null){   
   cacheDataVO = new cacheDataVO();   
  }
  return cacheDataVO;
 }
}

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger