OSCache中Java API

OSCache中Java API

在OSCache中我们主要对com.opensymphony.oscache.general.GeneralCacheAdministrator;进行操作,而com.opensymphony.oscache.general.GeneralCacheAdministrator;主要是对com.opensymphony.oscache.base.Cache进行操作。现在讲解这两个类中常用的方法。




一般我们所要存储的内容(content)是处在Cache的cacheEntry中,由com.opensymphony.oscache.base.CacheEntry来保存每一个存储单元。

 关于函数的信息可以看下载的解压缩包中的oscache-2.4-full\docs\api中的index.html,或者直接看源码。这里之讲解几个重要的函数。




Class GeneralCacheAdministrator

1. void cancelUpdate(String key)        取消更新 只用于在处理捕获的NeedsRefreshException异常并尝试生成新缓存内容失效的时候.

2. public void destroy()                      关闭一个cache管理者,即放弃对cache的管理

3. public void flushAll()                            立即刷新整个缓存,即将所有的缓存删除。

4. public void flushAll(Date date)              在给定的Data刷新huancun

5. public void flushEntry(String key)  按照String key刷新一个缓存

6. public void flushGroup(String group)按照一个group范围刷新缓存

7. public void putInCache(String key,Objectcontent,EntryRefreshPolicy policy)

添加一个缓存key为关键字,content为缓存内容Policy为所要实现的刷新侧路i

8. public void putInCache(String key,Object content)

                                                 添加一个缓存key为关键字,content为缓存内容。

9. public void putInCache(String key,Object content,String[]groups)

添加一个缓存key为关键字,content为缓存内容,group为该缓存的范围

10. public void putInCache(String key,Object content,String[]groups,EntryRefreshPolicy policy)

添加一个缓存key为关键字,content为缓存内容,group为该缓存的范围,policy为刷新策略。

11. public Cache getCache()

                                          得到该GeneralCacheAdministrator对应的Cache,

12. public Object getFromCache(String key)throwsNeedsRefreshException

取回相应的缓存内容,Key为关键字,如果发生错误抛出NeedsRefreshException。

13. public Object getFromCache(String key,int refreshPeriod)throwsNeedsRefreshException

取回相应的缓存内容,Key为关键字,refreshPeriod为在缓存中存储的秒数。如果发生错误则抛出NeedsRefreshException。

       14. public Object getFromCache(Stringkey,int refreshPeriod,String cronExpression)

       hrows NeedsRefreshException

取回相应的缓存内容,key,refreshPeriod与上面相同,cronExpression为一个cron表达式,表示相应的缓存内容过期的绝对过期时间。

一个GeneralCacheAdministrator的创建,刷新,管理的例子:

//---------------------------------------------------------------

 // Typical use with fail over

 // ---------------------------------------------------------------

 String myKey = "myKey";

 String myValue;

 int myRefreshPeriod = 1000;

 try {

     // Get from the cache

     myValue = (String)admin.getFromCache(myKey, myRefreshPeriod);

 } catch (NeedsRefreshException nre) {

     try{

         // Get the value (probably by callingan EJB)

         myValue = "This is the contentretrieved.";

         // Store in the cache

         admin.putInCache(myKey, myValue);

     } catch (Exception ex) {

         // We have the current content if wewant fail-over.

         myValue = (String)nre.getCacheContent();

         // It is essential that cancelUpdateis called if the

         // cached content is not rebuilt

         admin.cancelUpdate(myKey);

     }

 }

 

 

 

 // ---------------------------------------------------------------

 // Typical use without fail over

 //---------------------------------------------------------------

 String myKey = "myKey";

 String myValue;

 int myRefreshPeriod = 1000;

 try {

     // Get from the cache

     myValue = (String)admin.getFromCache(myKey, myRefreshPeriod);

 } catch (NeedsRefreshException nre) {

     try {

         // Get the value (probably by callingan EJB)

         myValue = "This is the contentretrieved.";

         // Store in the cache

         admin.putInCache(myKey, myValue);

         updated = true;

     } finally {

         if (!updated) {

             // It is essential thatcancelUpdate is called if the

             // cached content could not berebuilt

             admin.cancelUpdate(myKey);

         }

     }

 }

 //---------------------------------------------------------------

 //---------------------------------------------------------------

 

Class Cache

1. public Cache(boolean useMemoryCaching,booleanunlimitedDiskCache,

booleanoverflowPersistence)         useMemoryCaching表示是否进行内存缓存。

                                                        unlimitedDiskCache表示硬盘缓存是否限定

overflowPersistence表示是否只有当指定的内存缓存已经满时才进行持久化

2. public Cache(boolean useMemoryCaching,booleanunlimitedDiskCache,boolean overflowPersistence,boolean blocking,String algorithmClass,intcapacity)  

useMemoryCaching、unlimitedDiskCache overflowPersistence与之前相同

blocking表示当缓存中的某条数据更新时是否对客户请求返回更新前的数据

algorithmClass表示缺省的运算规则。

Capacity表示缓存的容量

       3. public void addCacheEventListener(CacheEventListenerlistener)

添加Cache事件监听器,listener必须添加CacheEventListener接口。

       4. public EventListenerListgetCacheEventListenerList()

                                                               返回所有的事件监听器。

5.public void cancelUpdate(String key)

取消更新 只用于在处理捕获的NeedsRefreshException异常并尝试生成新缓存内容失效的时候.

6.public void flushAll(Date date)

在给定的date刷新所有的缓存。

       7. publicvoid flushAll(Date date,String origin)

在给定的date刷新所有符合origin(防止事件进入错误的递归)的缓存

       8. protected void clear()   

                                                               彻底清除cache

       9. public voidflushGroup(String group)

刷新所有属于group的缓存,并派发一个CacheEntryEventType.GROUP_FLUSHED事件

       10. public voidflushGroup(String group,String origin)

刷新所有属于group的缓存,并派发一个CacheEntryEventType.GROUP_FLUSHED事件

       11. public voidputInCache(String key,Object content)

                                                               添加一个关键字为key,内容为content的缓存。

       12. public voidputInCache(String key,Object content,EntryRefreshPolicy policy)

添加一个关键字为key,内容为content,刷新政策为policy的缓存

       13. public voidputInCache(String key,Object content,String[] groups)

添加一个关键字为key,内容为content,属于group的缓存。

14. public void putInCache(String key,Object content,String[]groups,EntryRefreshPolicy policy,String origin)

添加一个关键字为key,内容为content,属于grou刷新方式为policy,来源为origin的缓存。

       15. public void removeCacheEventListener(CacheEventListenerlistener)

                                                        删除当前的监听器。

       16. protected CacheEntrygetCacheEntry(String key,EntryRefreshPolicy policy,String origin)

取得关键字为key,刷新方式为policy,来源为origin的CacheEntry,如果没有则创造一个。

       17. protected booleanisStale(CacheEntry cacheEntry,int refreshPeriod,String cronExpiry)

                                                        判断符合要求的cache是否是旧的。

       18. protectedEntryUpdateState getUpdateState(String key)

从更新的cache中取得cache entry。如果没有找到则创造一个新的,并返回EntryUpdateState.NOT_YET_UPDATING

       19. protected voidreleaseUpdateState(EntryUpdateState state,String key)

按照key移除update state并并通知其他的thread这是等待中这个方法被putInCache(java.lang.String, java.lang.Object)所使用。

       20,. public int getSize()

                                                        得到这个cache中持有的cache entry的数量。

       21. public Object getFromCache(String key)throws NeedsRefreshException

取回相应的缓存内容,Key为关键字,如发生错误,则抛出NeedsRefreshException。

       22. public Object getFromCache(String key,int refreshPeriod)throwsNeedsRefreshException

取回相应的缓存内容,,key为关键字,refreshPeriod为缓存内容在缓存中存储的时间。如发生错误,抛出NeedsRefreshException。

       23. public Object getFromCache(String key,int refreshPeriod,StringcronExpiry)throws NeedsRefreshException                              

取回相应的缓存内容,key,refreshPeroid与之前相同,cronExpiry是一个cron表达式,表示相应的缓存内容过期的绝对过期时间。

Class CacheEntry

这个一类比较简单,自己看源码并且一般不会对这个类进行直接操作。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值