---
title: "OSCACHE集群功能 转"
author: "javaeyetodj"
date: "2010-12-11T13:54:17Z"
tags: []
categories: ["编程语言"]
slug: "oscache"
draft: false
lastmod: "2010-12-13T09:09:44Z"
id: 839555
url: "https://javaeyetodj.iteye.com/blog/839555"
---

转自 [http://benx.iteye.com/blog/606083](http://benx.iteye.com/blog/606083)

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

Java代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

01. **public** **class** JavaGroupsBroadcastingListenerImpl **extends**
02. JavaGroupsBroadcastingListener {
03. **public** **void** handleClusterNotification(ClusterNotification message) {
04. **switch** (message.getType()) {
05. **case** CacheConstants.CLUSTER\_ENTRY\_ADD:
06. System.out.println("集群新增:" + message.getData());
07. **if**(message.getData() **instanceof** QflagCacheEvent) {
08. QflagCacheEvent event = (QflagCacheEvent)message.getData();
09. cache.putInCache(event.getKey(), event.getEntry().getContent(),**null**,**null**,CLUSTER\_ORIGIN);
10. }
11. **break**;
12. **case** CacheConstants.CLUSTER\_ENTRY\_UPDATE:
13. System.out.println("集群更新:" + message.getData());
14. **if**(message.getData() **instanceof** QflagCacheEvent) {
15. QflagCacheEvent event = (QflagCacheEvent)message.getData();
16. cache.putInCache(event.getKey(), event.getEntry().getContent(),**null**,**null**,CLUSTER\_ORIGIN);
17. }
18. **break**;
19. **case** CacheConstants.CLUSTER\_ENTRY\_DELETE:
20. System.out.println("集群删除:" + message.getData());
21. **if**(message.getData() **instanceof** QflagCacheEvent) {
22. QflagCacheEvent event = (QflagCacheEvent)message.getData();
23. //              cache.removeEntry(event.getKey(),event.getOrigin());
24. cache.removeEntry(event.getKey());
25. }
26. **break**;
27. }
28. }
29. **public** **void** cacheEntryAdded(CacheEntryEvent event) {
30. **super**.cacheEntryAdded(event);
31. **if**(!CLUSTER\_ORIGIN.equals(event.getOrigin())) {
32. sendNotification(**new** ClusterNotification(CacheConstants.CLUSTER\_ENTRY\_ADD, **new** QflagCacheEvent(event.getMap(),event.getEntry(),CLUSTER\_ORIGIN)));
33. }
34. }
35. //  @Override
36. //  public void cacheEntryFlushed(CacheEntryEvent event) {
37. //
38. //      super.cacheEntryFlushed(event);
39. //      if(!CLUSTER\_ORIGIN.equals(event.getOrigin())) {
40. //          sendNotification(new ClusterNotification(CacheConstants.CLUSTER\_ENTRY\_ADD, new UcallCacheEvent(event.getMap(),event.getEntry(),CLUSTER\_ORIGIN)));
41. //      }
42. //  }
43. @Override
44. **public** **void** cacheEntryRemoved(CacheEntryEvent event) {
45. **super**.cacheEntryRemoved(event);
46. **if**(!CLUSTER\_ORIGIN.equals(event.getOrigin())) {
47. sendNotification(**new** ClusterNotification(CacheConstants.CLUSTER\_ENTRY\_DELETE, **new** QflagCacheEvent(event.getMap(),event.getEntry(),CLUSTER\_ORIGIN)));
48. }
49. }
50. @Override
51. **public** **void** cacheEntryUpdated(CacheEntryEvent event) {
52. **super**.cacheEntryUpdated(event);
53. **if**(!CLUSTER\_ORIGIN.equals(event.getOrigin())) {
54. sendNotification(**new** ClusterNotification(CacheConstants.CLUSTER\_ENTRY\_UPDATE, **new** QflagCacheEvent(event.getMap(),event.getEntry(),CLUSTER\_ORIGIN)));
55. }
56. }
57. }

```
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)));  
        }  
    }  
      
}  
```

 

Java代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

01. **package** qflag.ucall.cache;
02. **public** **class** CacheConstants {
03. /\**
04. * 添加缓存对象操作
05. \*/
06. **public** **final** **static** **int** ACTION\_ADD\_OBJ = 1;
07. /\**
08. * 更新缓存对象操作
09. \*/
10. **public** **final** **static** **int** ACTION\_UPDATE\_OBJ = 2;
11. /\**
12. * 删除缓存对象操作
13. \*/
14. **public** **final** **static** **int** ACTION\_DELETE\_OBJ = 3;
15. /\**
16. * 刷新缓存对象
17. \*/
18. **public** **final** **static** **int** ACTION\_FLUSH\_OBJ = 4;
19. /\**
20. * 集群entry add处理
21. \*/
22. **public** **final** **static** **int** CLUSTER\_ENTRY\_ADD = 20;
23. /\**
24. * 集群entry update处理
25. \*/
26. **public** **final** **static** **int** CLUSTER\_ENTRY\_UPDATE = 21;
27. /\**
28. * 集群entry delete处理
29. \*/
30. **public** **final** **static** **int** CLUSTER\_ENTRY\_DELETE = 22;
31. }

```
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;  
}  

```

 

Java代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

01. **package** qflag.ucall.cache.event;
02. **import** java.io.Serializable;
03. **import** com.opensymphony.oscache.base.Cache;
04. **import** com.opensymphony.oscache.base.CacheEntry;
05. **import** com.opensymphony.oscache.base.events.CacheEntryEvent;
06. **import** com.opensymphony.oscache.base.events.CacheEvent;
07. **public** **class** QflagCacheEvent **extends** CacheEvent **implements** Serializable {
08. /\**
09. * The cache where the entry resides.
10. \*/
11. **private** Cache map = **null**;
12. /\**
13. * The entry that the event applies to.
14. \*/
15. **private** CacheEntry entry = **null**;
16. /\**
17. * Constructs a cache entry event object with no specified origin
18. \*
19. * @param map
20. *            The cache map of the cache entry
21. * @param entry
22. *            The cache entry that the event applies to
23. \*/
24. **public** QflagCacheEvent(Cache map, CacheEntry entry) {
25. **this**(map, entry, **null**);
26. }
27. /\**
28. * Constructs a cache entry event object
29. \*
30. * @param map
31. *            The cache map of the cache entry
32. * @param entry
33. *            The cache entry that the event applies to
34. * @param origin
35. *            The origin of this event
36. \*/
37. **public** QflagCacheEvent(Cache map, CacheEntry entry, String origin) {
38. **super**(origin);
39. **this**.map = map;
40. **this**.entry = entry;
41. }
42. /\**
43. * Retrieve the cache entry that the event applies to.
44. \*/
45. **public** CacheEntry getEntry() {
46. **return** entry;
47. }
48. /\**
49. * Retrieve the cache entry key
50. \*/
51. **public** String getKey() {
52. **return** entry.getKey();
53. }
54. /\**
55. * Retrieve the cache map where the entry resides.
56. \*/
57. **public** Cache getMap() {
58. **return** map;
59. }
60. **public** String toString() {
61. **return** "key=" + entry.getKey();
62. }
63. }

```
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();  
    }  
}  
```