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
CacheMyData.java
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; } }