JbossCache使用

来源:互联网 发布:葡萄牙 法国 知乎 编辑:程序博客网 时间:2024/06/06 00:30

附录:

1 在实际工作中的应用

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

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

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

2 JbossCache的用法

1 下载JbossCache的包

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

<?xmlversion="1.0"encoding="UTF-8"?>

 

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

 

 

   <!--

      isolation levels supported: READ_COMMITTED and REPEATABLE_READ

      nodeLockingSchemes: mvcc, pessimistic (deprecated), optimistic (deprecated)

   -->

   <locking

         isolationLevel="REPEATABLE_READ"

         lockParentForChildInsertRemove="false"

         lockAcquisitionTimeout="20000"

         nodeLockingScheme="mvcc"

         writeSkewCheck="false"

         concurrencyLevel="500"/>

 

   <!--

      Used to register a transaction manager and participate in ongoing transactions.

      -->

   <!-- <transaction

         transactionManagerLookupClass="org.jboss.cache.transaction.GenericTransactionManagerLookup"

         syncRollbackPhase="false"

         syncCommitPhase="false"/>

    -->

   <!--

      Used to register JMX statistics in any available MBean server

      -->

   <jmxStatistics

         enabled="false"/>

 

   <!--

      If region based marshalling is used, defines whether new regions are inactive on startup.

   -->

   <startup

         regionsInactiveOnStartup="true"/>

 

   <!--

      Used to register JVM shutdown hooks.

      hookBehavior: DEFAULT, REGISTER, DONT_REGISTER

   -->

   <shutdown

         hookBehavior="DEFAULT"/>

 

   <!--

      Used to define async listener notification thread pool size

   -->

   <listeners

         asyncPoolSize="1"

         asyncQueueSize="1000000"/>

 

   <!--

      Used to enable invocation batching and allow the use of Cache.startBatch()/endBatch() methods.

   -->

   <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数据的例子:

    publicvoidrefreshPdmmthdToMemory() {

              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<Pdmmthd> 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<Pdmmthd> 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);

                     returndest;

              } catch (Exception e) {

                     return getAllMethodFromDatabase();

              }

       }


转自:http://blog.csdn.net/hobbypei/article/details/4417825

0 0