<< Ehcache分布式缓存的最小化配置 | 首页 | 用Hibernate映射数据库视图View >>

配置JavaGroups作为OSCache分布式缓存的底部广播协议

配置JavaGroups作为OSCache缓存集群的底部广播协议,除了oscache-2.4.jar和oscache.properties之外,还依赖于concurrent.jarjgroups-all.jar

注意:OSCache is based on distributed cache invalidation——OSCache是基于分布式缓存失效的。集群的缓存只有在flush事件发生时才广播消息。意思是缓存的内容不依赖于每个服务器而建立,但是任何时候一个服务器的缓存内容变成过期时其他所有的也变成过期。OSCache不传输缓存对象到集群中,它只发送缓存过期的消息。

Caches across a cluster only broadcast messages when flush events occur. This means that the content of the caches are built up independently on each server, but whenever content becomes stale on one server it is made stale on them all. This provides a very high performing solution since we never have to pass cached objects around the cluster. And since there is no central server that is in charge of the cluster, the clustering is very robust.

Configuring OSCache to cluster is very simple. Follow either the JMS or the JavaGroups instructions below depending on which protocol you want to use.

OSCache 集群的JavaGroups配置

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 API的用法:

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 from the database)         myValue = "This is the content retrieved.";
        // Store in the cache         admin.putInCache(myKey, myValue);
    } catch (Exception ex) {
        // We have the current content if we want fail-over.         myValue = (String) nre.getCacheContent();
        // It is essential that cancelUpdate is 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 from the database)         myValue = "This is the content retrieved.";
        // Store in the cache         admin.putInCache(myKey, myValue);
        updated = true;
    } finally {
        if (!updated) {
            // It is essential that cancelUpdate is called if the             // cached content could not be rebuilt             admin.cancelUpdate(myKey);
        }
    }
}
标签 : , ,



发表评论 发送引用通报