Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

EhCache Sample Code

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to write sample code for Ehcache which loads values into the cache from the database automatically if it is not present in cache.

I have a model which calls the DAO. The DAO has the implementation of creating the cache based on the ehcache.xml configuration. I fail to see the connection between the DAO and the CacheEntryFactory which has a method createEntry() which is supposed to load the cache with the values. Is EhCache supposed to call creatEntry() internally ? If so, it is not calling and nothing gets printed on the console.

Iam attaching the codes here. Sorry if it is too long. Thanks for all the help.

CacheModel.java

public class CacheModel
{
public static void testCache() throws Exception
{

VistaCacheDAO sampledao = new VistaCacheDAO();
VistaCacheVO VO = sampledao.fetchContent(1);
String name = VO.getName();
int age = VO.getAge();
System.out.println ("Name :");
System.out.println (name);
System.out.println ("Age :");
System.out.println (age);
}
}

CacheFactory.java

public class CacheFactory implements CacheEntryFactory
{

public CacheFactory()
{

}

public Object createEntry(Object key) throws Exception
{
CacheEntryKey Key = (CacheEntryKey) key;
int new_ID = Key.getID();
System.out.println(new_ID);
CacheVO VO = new CacheVO();
String query = null;
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://xxxxxxxx:xxxx/xxxxxx", "root", "root");
Statement st = con.createStatement();
ResultSet rs = null;
query = "select Name, Age from testcache where ID="+ new_ID;
rs = st.executeQuery(query);

while(rs.next())
{
VO.setAge(rs.getInt("Age"));
VO.setName(rs.getString("Name"));
}

return VO;
}
}

CacheDAO.java

public class CacheDAO
{

public CacheVO fetchContent(int ID) throws Exception
{
CacheVO VO = null;
CacheEntryKey key = new CacheEntryKey();
key.setID(ID);
CacheManager samplemanager = CacheManager
.getInstance("D:/ehcache.xml");
Cache cache = samplemanager
.getCache("sampleCache1");
Serializable value = cache.get(key);
VO = (VistaCacheVO) value;
return VO;
}
}

CacheEntryKey.java

public class CacheEntryKey implements Serializable
{
private int ID;

public int getID()
{
return ID;
}

public void setID(int id)
{
ID = id;
}
}

CacheVO.java

public class CacheVO implements Serializable
{
private int Age;

private String Name;

public String getName()
{
return Name;
}

public void setName(String name)
{
Name = name;
}

public int getAge()
{
return Age;
}

public void setAge(int age)
{
Age = age;
}

}
 
I think she's lovely. It's this tiny ad that called her crazy:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic