JbossCache使用

1 在实际工作中的应用

看了上面的说明,是不是对Jboss Cache有了一定的了解了哪?

在数据库缓存的实际应用中,Jboss Cache一般用来缓存两种类型的数据,一种是频繁变化的数据,比如说证券业的行情,1秒钟更新一次,要读取N多次,放到数据库中根本不可行。

一种是不太变化的数据,比说如用户,组织,权限的数据,不会经常变化,但是会经常check用户的权限,这类表也适合放入到缓存中保存。

2 JbossCache的用法

1 下载JbossCache的包

2 配置JbossCache.xml文件,例子如下:

xml version="1.0" encoding="UTF-8"?>

 

<jbosscache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:jboss:jbosscache-core:config:3.0">

 

 

  

   <locking

         isolationLevel="REPEATABLE_READ"

         lockParentForChildInsertRemove="false"

         lockAcquisitionTimeout="20000"

         nodeLockingScheme="mvcc"

         writeSkewCheck="false"

         concurrencyLevel="500"/>

 

  

  

  

   <jmxStatistics

         enabled="false"/>

 

  

   <startup

         regionsInactiveOnStartup="true"/>

 

  

   <shutdown

         hookBehavior="DEFAULT"/>

 

  

   <listeners

         asyncPoolSize="1"

         asyncQueueSize="1000000"/>

 

  

   <invocationBatching

         enabled="true"/>

 

jbosscache>

 

3 在启动时加载JbossCache,可以放到Listener中启动,示例程序如下:

/**

        *loadtheConsttabletomemorywhenappserverstart

        */

       privatevoid initConstTableToMemory() {

              // init the jboss cache

              CacheFactory factory = new DefaultCacheFactory();

              Cache cache = factory.createCache("JBossCache.xml");

              cache.create();

              cache.start();

              WebContextHolder.getInstence().setJbossCache(cache);

              RefreshConstToMemory memory = new RefreshConstToMemory();

              memory. refreshPdmmthdToMemory ();

       }

4        RefreshConstToMemory是存放Jboss Cache数据的例子:

    publicvoid refreshPdmmthdToMemory() {

              Cache cache = WebContextHolder.getInstence().getJbossCache();

              Node rootNode = cache.getRoot();

              Node node = null;

              Fqn fqn = null;

              //从数据库中取出pdmmthd表的内容,放到Jboss Cache中。

              PdmmthdDAO pdmmthdDAO = (PdmmthdDAO) BeanUtil.getBean("pdmmthdDAO");

              List pdmmthdList = pdmmthdDAO.getAllMethodFromDatabase();

              node = cache.getNode("/pdmmthd");

              if (node == null) {

                     fqn = Fqn.fromString("/pdmmthd");

                     node = rootNode.addChild(fqn);

              }

              node.put("/pdmmthd", pdmmthdList);

              for (Pdmmthd pdmmthd : pdmmthdList) {

                     node.put(pdmmthd.getMtcode(), pdmmthd);

              }

}

 

在数据库第一次启动,在数据更新以后,都需要手工调用上述的refreshALLMemory()方法。

为了保险起见,对于不支持Jboss Cache的情况,或者Jboss Cache出现故障,我们可以在查询时增加出错处理,示例程序如下:

public List getAllMethodFromMemory() {

              // try the Exception in case of can't find 'method' in memory

              try {

                     Cache cache = WebContextHolder.getInstence().getJbossCache();

                     Node node = cache.getNode("/pdmmthd");

                     List list = (List) node.get("/pdmmthd");

                     // return a new List,cause the user need change the list.

                     // For example,It will add "All" method to the list.

                     List dest = new ArrayList();

                     dest.addAll(list);

                     return dest;

              } catch (Exception e) {

                     return getAllMethodFromDatabase();

              }

       }

 

你可能感兴趣的:(缓存)