OSCACHE集群功能 转

Oscache的集群功能没有完全实现,只是实现了flush的接口,需要用户自己去实现它的其它集群接口(不知道他是出于什么考虑). 如果采用Jgroups组件,则需要继承自JavaGroupsBroadcastingListener抽象类,继承它的handleClusterNotification方法,完成集群其它功能的实现:
注意:集群中传递的对象必须实现Serializable接口。


public class JavaGroupsBroadcastingListenerImpl extends
JavaGroupsBroadcastingListener {
public void handleClusterNotification(ClusterNotification message) {

switch (message.getType()) {
case CacheConstants.CLUSTER_ENTRY_ADD:
System.out.println("集群新增:" + message.getData());
if(message.getData() instanceof QflagCacheEvent) {
QflagCacheEvent event = (QflagCacheEvent)message.getData();
cache.putInCache(event.getKey(), event.getEntry().getContent(),null,null,CLUSTER_ORIGIN);
}
break;
case CacheConstants.CLUSTER_ENTRY_UPDATE:
System.out.println("集群更新:" + message.getData());
if(message.getData() instanceof QflagCacheEvent) {
QflagCacheEvent event = (QflagCacheEvent)message.getData();
cache.putInCache(event.getKey(), event.getEntry().getContent(),null,null,CLUSTER_ORIGIN);
}
break;
case CacheConstants.CLUSTER_ENTRY_DELETE:
System.out.println("集群删除:" + message.getData());
if(message.getData() instanceof QflagCacheEvent) {
QflagCacheEvent event = (QflagCacheEvent)message.getData();
// cache.removeEntry(event.getKey(),event.getOrigin());
cache.removeEntry(event.getKey());
}
break;
}

}

public void cacheEntryAdded(CacheEntryEvent event) {
super.cacheEntryAdded(event);
if(!CLUSTER_ORIGIN.equals(event.getOrigin())) {
sendNotification(new ClusterNotification(CacheConstants.CLUSTER_ENTRY_ADD, new QflagCacheEvent(event.getMap(),event.getEntry(),CLUSTER_ORIGIN)));
}
}

// @Override
// public void cacheEntryFlushed(CacheEntryEvent event) {
//
// super.cacheEntryFlushed(event);
// if(!CLUSTER_ORIGIN.equals(event.getOrigin())) {
// sendNotification(new ClusterNotification(CacheConstants.CLUSTER_ENTRY_ADD, new UcallCacheEvent(event.getMap(),event.getEntry(),CLUSTER_ORIGIN)));
// }
// }

@Override
public void cacheEntryRemoved(CacheEntryEvent event) {

super.cacheEntryRemoved(event);
if(!CLUSTER_ORIGIN.equals(event.getOrigin())) {
sendNotification(new ClusterNotification(CacheConstants.CLUSTER_ENTRY_DELETE, new QflagCacheEvent(event.getMap(),event.getEntry(),CLUSTER_ORIGIN)));
}
}

@Override
public void cacheEntryUpdated(CacheEntryEvent event) {

super.cacheEntryUpdated(event);
if(!CLUSTER_ORIGIN.equals(event.getOrigin())) {
sendNotification(new ClusterNotification(CacheConstants.CLUSTER_ENTRY_UPDATE, new QflagCacheEvent(event.getMap(),event.getEntry(),CLUSTER_ORIGIN)));
}
}

}


package qflag.ucall.cache;
public class CacheConstants {
/**
* 添加缓存对象操作
*/
public final static int ACTION_ADD_OBJ = 1;
/**
* 更新缓存对象操作
*/
public final static int ACTION_UPDATE_OBJ = 2;
/**
* 删除缓存对象操作
*/
public final static int ACTION_DELETE_OBJ = 3;
/**
* 刷新缓存对象
*/
public final static int ACTION_FLUSH_OBJ = 4;


/**
* 集群entry add处理
*/
public final static int CLUSTER_ENTRY_ADD = 20;

/**
* 集群entry update处理
*/
public final static int CLUSTER_ENTRY_UPDATE = 21;

/**
* 集群entry delete处理
*/
public final static int CLUSTER_ENTRY_DELETE = 22;
}




package qflag.ucall.cache.event;

import java.io.Serializable;

import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.base.CacheEntry;
import com.opensymphony.oscache.base.events.CacheEntryEvent;
import com.opensymphony.oscache.base.events.CacheEvent;

public class QflagCacheEvent extends CacheEvent implements Serializable {
/**
* The cache where the entry resides.
*/
private Cache map = null;

/**
* The entry that the event applies to.
*/
private CacheEntry entry = null;

/**
* Constructs a cache entry event object with no specified origin
*
* @param map
* The cache map of the cache entry
* @param entry
* The cache entry that the event applies to
*/
public QflagCacheEvent(Cache map, CacheEntry entry) {
this(map, entry, null);
}

/**
* Constructs a cache entry event object
*
* @param map
* The cache map of the cache entry
* @param entry
* The cache entry that the event applies to
* @param origin
* The origin of this event
*/
public QflagCacheEvent(Cache map, CacheEntry entry, String origin) {
super(origin);
this.map = map;
this.entry = entry;
}

/**
* Retrieve the cache entry that the event applies to.
*/
public CacheEntry getEntry() {
return entry;
}

/**
* Retrieve the cache entry key
*/
public String getKey() {
return entry.getKey();
}

/**
* Retrieve the cache map where the entry resides.
*/
public Cache getMap() {
return map;
}

public String toString() {
return "key=" + entry.getKey();
}
}


转自 http://rwl6813021.iteye.com/blog/246473
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值