`

oscache 缓存

    博客分类:
  • java
 
阅读更多

 

数据库查询结果缓存
//缓存对象
private static GeneralCacheAdministrator admin = new GeneralCacheAdministrator();
/* 刷新缓存的间隔,单位为秒,此处设置为 10 秒 */
int myRefreshPeriod = 10;
//标识是否更新缓存
boolean update = false;
try {
/* 从 Map 中取出键值为 key 的对象,返回一个Object 对象,需要强制转换 */
   list = (List<Users>) admin.getFromCache(key, myRefreshPeriod);
   logger.info("本次从 OSCache 中读取数据...........");

} catch (NeedsRefreshException e) {
    Session session = HibernateSessionFactory.getSession();
    try {
        logger.info("本次从 DataBase 中读取数据.................");
	Query query = session.createQuery("from Users");
	list = query.list();
	/* 添加或刷新oscache cache */
	admin.putInCache(key, list);
        update = true;
    } catch (HibernateException ex) {
	  ex.printStackTrace();
    } catch (Exception ex) {
	  ex.printStackTrace();
    } finally {
       /* * 当从OSCache 中读取数据时不对key进行更新 */
	 if (!update) {
		 admin.cancelUpdate(key);
	 }
	 HibernateSessionFactory.closeSession();
    }
}

 

 

页面缓存
没有缓存的日期: <%= new Date() %><p>
<!--自动刷新-->
<cache:cache time="30">
每30秒刷新缓存一次的日期: <%= new Date() %> <p>
</cache:cache>
<!--手动刷新-->
<cache:cache key="testcache">
手动刷新缓存的日期: <%= new Date() %> <p>
</cache:cache>
<a href="cache2.jsp">手动刷新</a>
缓存已刷新...<p>
<cache:flush key="testcache" scope="application"/>
<a href="cache1.jsp">返回</a>
 

 

 

 

 

lib : oscache-2.4.1.jar

 

oscache.tld

http://download.csdn.net/detail/linshaoyi2008/2828821

分享到:
评论
1 楼 sblig 2012-07-05  
<!-- 刷新application中的testcache这个缓存 -->
<cache:flush scope="application" key="testcache" />

相关推荐

Global site tag (gtag.js) - Google Analytics