在Hibernate中使用分佈式 OSCache

 

OSCache是一個工業級的J2EE緩存實現。

它可以緩存 :

1 java對象,
2 頁面
3 http請求
4 二進制內容:例如PDF文件




正文開始 。。。

 爲了減少與數據庫通信 來提高應用的性能,我們在Hibernate中使用了分佈式緩存:OSCache。

       Oscache是得到了廣泛使用的開源 Cache 實現(Hibernate中對它提供了支持),它基於更加可靠高效的設計,最重要的是,新版本的OSCache已經支持集羣分佈式。如果系統需要在部署在集羣中,或者需要部署在多機負載均衡模式的環境中來獲取更高性能,那麼 OSCache將是不二之選。

      我們知道爲了實現分佈式環境下消息的通知,目前兩種比較流行的做法是使用 :

1 JavaGroups[http: // www.jgroups.org]
2 JMS

這兩種方式都在底層實現了廣播發布消息。 由於JGroups可以提供可靠的廣播通信.所以我們準備採用JGroups。

一、環境說明

1 OSCache: oscache - 2.3 .jar
2 JGroups:  2.2 . 8 ,目前最高版本是  2.2 . 9.1
3 Hibernate:  3.05

二、下面給出 OScache 官方文檔對 JavaGroups 使用的說明:     

JavaGroups Configuration
Just make sure you have jgroups-all.jar file in your classpath (for a webapp put it in WEB-INF/lib), and add the JavaGroups broadcasting listener to your oscache.properties file like this:


cache.event.listeners=com.opensymphony.oscache.plugins.clustersupport.JavaGroupsBroadcastingListener

In most cases, that's it! OSCache will now broadcast any cache flush events across the LAN. The jgroups-all.jar library is not included with the binary distribution due to its size, however you can obtain it either by downloading the full OSCache distribution, or by visiting the JavaGroups website.

If you want to run more than one OSCache cluster on the same LAN, you will need to use different multicast IP addresses. This allows the caches to exist in separate multicast groups and therefore not interfere with each other. The IP to use can be specified in your oscache.properties file by the cache.cluster.multicast.ip property. The default value is 231.12.21.132, however you can use any class D IP address. Class D address fall in the range 224.0.0.0 through 239.255.255.255.

If you need more control over the multicast configuration (eg setting network timeout or time-to-live values), you can use the cache.cluster.properties configuration property. Use this instead of the cache.cluster.multicast.ip property. The default value is:

UDP(mcast_addr=231.12.21.132;mcast_port=45566;ip_ttl=32;/
mcast_send_buf_size=150000;mcast_recv_buf_size=80000):/
PING(timeout=2000;num_initial_members=3):/
MERGE2(min_interval=5000;max_interval=10000):/
FD_SOCK:VERIFY_SUSPECT(timeout=1500):/
pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800;max_xmit_size=8192):/
UNICAST(timeout=300,600,1200,2400):/
pbcast.STABLE(desired_avg_gossip=20000):/
FRAG(frag_size=8096;down_thread=false;up_thread=false):/
pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=true)

See the JavaGroups site for more information. In particular, look at the documentation of Channels in the User's Guide.

三、我在應用中使用的 OSCache.properties

# CACHE IN MEMORY
#
# If you want to disable memory caching, just uncomment this line.
#
# 不使用內存

cache.memory=false


# CACHE KEY
#
# This is the key that will be used to store the cache in the application
# and session scope.
#
# If you want to set the cache key to anything other than the default
# uncomment this line and change the cache.key
#
# cache.key=__oscache_cache


# USE HOST DOMAIN NAME IN KEY
#
# Servers for multiple host domains may wish to add host name info to
# the generation of the key. If this is true, then uncomment the
# following line.
#
# cache.use.host.domain.in.key=true


# CACHE LISTENERS
#
# These hook OSCache events and perform various actions such as logging
# cache hits and misses, or broadcasting to other cache instances across a cluster.
# See the documentation for further information.
#
# cache.event.listeners=com.opensymphony.oscache.plugins.clustersupport.JMSBroadcastingListener, /
# com.opensymphony.oscache.extra.CacheEntryEventListenerImpl, /
# com.opensymphony.oscache.extra.CacheMapAccessEventListenerImpl, /
# com.opensymphony.oscache.extra.ScopeEventListenerImpl

# 使用 JavaGroups 來實現監聽廣播式緩存

cache.event.listeners=com.opensymphony.oscache.plugins.clustersupport.JavaGroupsBroadcastingListener

 

# CACHE PERSISTENCE CLASS
#
# Specify the class to use for persistence. If you use the supplied DiskPersistenceListener,
# don't forget to supply the cache.path property to specify the location of the cache
# directory.
#
# If a persistence class is not specified, OSCache will use memory caching only.
#
# 啓動硬盤持久化 Cache
cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener

# CACHE OVERFLOW PERSISTENCE
# Use persistent cache in overflow or not. The default value is false, which means
# the persistent cache will be used at all times for every entry. true is the recommended setting.
#
# cache.persistence.overflow.only=true

# CACHE DIRECTORY
#
# This is the directory on disk where caches will be stored by the DiskPersistenceListener.
# it will be created if it doesn't already exist. Remember that OSCache must have
# write permission to this directory.
#
# Note: for Windows machines, this needs / to be escaped
# ie Windows:
# cache.path=c://myapp//cache
# or *ix:

# 在Linux下的Cache存放路徑
cache.path=/data/jsp/boss.21cn.com/bossimpls/cache/bossuud
#
# cache.path=c://app//cache


# CACHE ALGORITHM
#
# Default cache algorithm to use. Note that in order to use an algorithm
# the cache size must also be specified. If the cache size is not specified,
# the cache algorithm will be Unlimited cache.
#

#使用LRU運算算法,把最少使用的Cache剔除出去
cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache
# cache.algorithm=com.opensymphony.oscache.base.algorithm.FIFOCache
# cache.algorithm=com.opensymphony.oscache.base.algorithm.UnlimitedCache

# THREAD BLOCKING BEHAVIOR
#
# When a request is made for a stale cache entry, it is possible that another thread is already
# in the process of rebuilding that entry. This setting specifies how OSCache handles the
# subsequent 'non-building' threads. The default behaviour (cache.blocking=false) is to serve
# the old content to subsequent threads until the cache entry has been updated. This provides
# the best performance (at the cost of serving slightly stale data). When blocking is enabled,
# threads will instead block until the new cache entry is ready to be served. Once the new entry
# is put in the cache the blocked threads will be restarted and given the new entry.
# Note that even if blocking is disabled, when there is no stale data available to be served
# threads will block until the data is added to the cache by the thread that is responsible
# for building the data.
#
# cache.blocking=false

# CACHE SIZE
#
# Default cache size in number of items. If a size is specified but not
# an algorithm, the cache algorithm used will be LRUCache.
#

#這個值設得夠大了,就是Cache的容量項目個數
cache.capacity=100000000


# CACHE UNLIMITED DISK
# Use unlimited disk cache or not. The default value is false, which means
# the disk cache will be limited in size to the value specified by cache.capacity.
#

#不使用無限硬盤空間
cache.unlimited.disk=false


# JMS CLUSTER PROPERTIES
#
# Configuration properties for JMS clustering. See the clustering documentation
# for more information on these settings.
#
#cache.cluster.jms.topic.factory=java:comp/env/jms/TopicConnectionFactory
#cache.cluster.jms.topic.name=java:comp/env/jms/OSCacheTopic
#cache.cluster.jms.node.name=node1


# JAVAGROUPS CLUSTER PROPERTIES
#
# Configuration properites for the JavaGroups clustering. Only one of these
# should be specified. Default values (as shown below) will be used if niether
# property is set. See the clustering documentation and the JavaGroups project
# (www.javagroups.com) for more information on these settings.
#
cache.cluster.properties=UDP(mcast_addr=231.12.21.132;mcast_port=45566;ip_ttl=32;/
mcast_send_buf_size=150000;mcast_recv_buf_size=80000):/
PING(timeout=2000;num_initial_members=3):/
MERGE2(min_interval=5000;max_interval=10000):/
FD_SOCK:VERIFY_SUSPECT(timeout=1500):/
pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800;max_xmit_size=8192):/
UNICAST(timeout=300,600,1200,2400):/
pbcast.STABLE(desired_avg_gossip=20000):/
FRAG(frag_size=8096;down_thread=false;up_thread=false):/
pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=true)
cache.cluster.multicast.ip=231.12.21.132

關於作者

網名:潤名
E-mail: [email protected]
現從事於 21cn 公司,從事 J2EE 架構設計,有四年 J2EE工作經驗。善長於對 Open Source Framework的技術整合開發和各種應用服務器的調優。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章