kelefa  
大千世界中,唯一缺乏的就是人类的注意力。
日历
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
统计
  • 随笔 - 11
  • 文章 - 0
  • 评论 - 28
  • 引用 - 0

导航

常用链接

留言簿(2)

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

OSCache是OpenSymphony组织提供的一个J2EE架构中Web应用层的缓存技术实现方案,可以使用内存、硬盘空间、同时使用内存和硬盘作为缓存区灵活的缓存系统:OSCache支持多种缓存级别,使用相当灵活简单,在jsp中刷新缓存只要两行代码:


<% @ taglib uri = " oscache "  prefix = " cache " %>

< cache:flush group = " device_types "  scope = " application "   />

但是有时需要在java代码中刷新缓存,以下这个OSCacheUtil类可以工作,但是只能在webwork环境内调用:
import javax.servlet.jsp.PageContext;

import org.apache.log4j.Logger;
import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.web.ServletCacheAdministrator;
import com.opensymphony.webwork.ServletActionContext;

/**
 * osCache缓存工具类.
 * 只能在webwork环境内调用
 *
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * 
@author 杨杰荣
 * 
@version 1.0
 
*/

public class OSCacheUtil
{
  
private static final Logger log = Logger.getLogger( OSCacheUtil.class );

  
public static final int ALL_SCOPE = 0;
  
public static final int SESSION_SCOPE = PageContext.SESSION_SCOPE;
  
public static final int APPLICATION_SCOPE = PageContext.APPLICATION_SCOPE;

  
private static ServletCacheAdministrator admin = null;

  
private OSCacheUtil()
  
{
  }


  
/**
   * 刷新osCache组
   * 
@param group Cache组名
   * 
@param cacheScope Cache范围,只能是SESSION_SCOPE或APPLICATION_SCOPE
   
*/

  
public static void flushGroup( String group, int cacheScope )
  
{
    initCacheAdmin();

    
if ( cacheScope == SESSION_SCOPE || cacheScope == APPLICATION_SCOPE )
    
{
      Cache cache 
= admin.getCache( ServletActionContext.getRequest(),
                                    cacheScope );
      cache.flushGroup( group );
    }

    
else
    
{
      log.warn( 
"A cache group was specified for flushing, but the scope wasn't supplied or was invalid" );
    }

  }


  
/**
   * 刷新osCache中的某个key'
   * 
@param key String
   * 
@param cacheScope Cache范围,只能是SESSION_SCOPE或APPLICATION_SCOPE
   
*/

  
public static void flushKey( String key, int cacheScope )
  
{
    initCacheAdmin();

    
if ( cacheScope == SESSION_SCOPE || cacheScope == APPLICATION_SCOPE )
    
{
      String actualKey 
= admin.generateEntryKey(
          key, ServletActionContext.getRequest(), cacheScope, 
null );

      Cache cache 
= admin.getCache( ServletActionContext.getRequest(), cacheScope );
      cache.flushEntry( actualKey );
    }

    
else
    
{
      log.warn( 
"A cache key was specified for flushing, but the scope wasn't supplied or was invalid" );
    }

  }


  
/**
   * 刷新所有的osCache
   * 
@param cacheScope Cache范围,可以是SESSION_SCOPE,APPLICATION_SCOPE,ALL_SCOPE
   
*/

  
public static void flushAll( int cacheScope )
  
{
    initCacheAdmin();

    
if ( cacheScope == SESSION_SCOPE || cacheScope == APPLICATION_SCOPE )
    
{
      admin.setFlushTime( cacheScope );
    }

    
else
    
{
      admin.flushAll();
    }

  }



  
private static void initCacheAdmin()
  
{
    
if ( admin == null )
    
{
      admin 
= ServletCacheAdministrator.getInstance( ServletActionContext.
          getServletContext() );
    }

  }

}
posted on 2006-06-29 09:32 杨杰荣 阅读(1469) 评论(2)  编辑  收藏 所属分类: 开源应用
评论:
  • # re: OSCache工具类  pandaxiaoxi Posted @ 2006-07-02 21:36
    问个跟本主题无关的问题,你是怎么在blog上发代码的,代码怎么跟在ide里一样那?看起来很工整,能交交我吗?呵呵  回复  更多评论   

  • # re: OSCache工具类  杨杰荣 Posted @ 2006-07-03 13:15
    @pandaxiaoxi

    点击“代码”图标,然后粘贴代码就ok了  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © 杨杰荣 Powered by: 博客园 模板提供:沪江博客