1 7 package com.jofti.cache.adapter.listener; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 12 import com.jofti.cache.IBaseAdaptor; 13 import com.jofti.core.InternalIndex; 14 import com.jofti.exception.JoftiException; 15 import com.opensymphony.oscache.base.events.CacheEntryEvent; 16 import com.opensymphony.oscache.base.events.CacheEntryEventListener; 17 import com.opensymphony.oscache.base.events.CacheGroupEvent; 18 import com.opensymphony.oscache.base.events.CachePatternEvent; 19 import com.opensymphony.oscache.base.events.CachewideEvent; 20 21 28 public class OSEventListener implements CacheEntryEventListener { 29 30 33 IBaseAdaptor base = null; 34 35 private static Log log = LogFactory.getLog(OSEventListener.class); 36 37 public OSEventListener(IBaseAdaptor base) { 38 this.base = base; 39 } 40 41 44 public void cacheEntryAdded(CacheEntryEvent arg0) { 45 String key = arg0.getKey(); 47 Object value = arg0.getEntry().getContent(); 48 49 try { 50 base.acquireUpdateLock(); 51 try { 52 53 synchronized (base.getCacheLock(key)) { 54 base.getIndex().insert(key, value); 56 } 57 if (log.isDebugEnabled()) { 58 log.debug("Add Event: entry added to index " + key 59 + " value: " + value); 60 } 61 } catch (JoftiException e) { 62 log.error( 63 "Add Event: Unable to add index value for key " + key, 64 e); 65 } finally { 67 base.releaseUpdateLock(); 68 } 69 70 } catch (Exception e) { 71 log.warn(e); 72 } 73 74 } 75 76 79 public void cacheEntryFlushed(CacheEntryEvent arg0) { 80 String key = arg0.getKey(); 81 82 try { 83 base.acquireUpdateLock(); 84 try { 85 86 synchronized (base.getCacheLock(key)) { 89 base.getIndex().removeByKey((Comparable ) key); 90 } 91 if (log.isDebugEnabled()) { 93 log.debug("Flush Event: removed from index " + key); 94 } 95 } catch (JoftiException e) { 96 log.error("Flush event: Unable to remove value for key " + key, 97 e); 98 } finally { 99 base.releaseUpdateLock(); 100 } 101 102 } catch (Exception e) { 103 log.error("unable to aquire update lock", e); 104 } 105 106 } 107 108 111 public void cacheEntryRemoved(CacheEntryEvent arg0) { 112 String key = arg0.getKey(); 113 114 try { 115 base.acquireUpdateLock(); 116 try { 117 118 synchronized (base.getCacheLock(key)) { 121 base.getIndex().removeByKey((Comparable ) key); 122 } 123 if (log.isDebugEnabled()) { 124 log.debug("Remove event: removed from index " + key); 125 } 126 } catch (JoftiException e) { 127 log.error( 128 "Remove event: Unable to remove value for key " + key, 129 e); 130 } finally { 131 base.releaseUpdateLock(); 132 } 133 134 } catch (Exception e) { 135 log.error("unable to aquire update lock", e); 136 } 137 } 138 139 142 public void cacheEntryUpdated(CacheEntryEvent arg0) { 143 String key = arg0.getKey(); 144 Object value = arg0.getEntry().getContent(); 145 146 InternalIndex index = base.getIndex(); 147 try { 148 base.acquireUpdateLock(); 149 try { 150 151 synchronized (base.getCacheLock(key)) { 154 index.removeByKey(key); 155 index.insert(key, value); 156 } 157 if (log.isDebugEnabled()) { 158 log.debug("Update event: inserted into index " + key 159 + " value " + value); 160 } 161 } catch (JoftiException e) { 162 log.error("Update event: Unable to index value for key " + key, 163 e); 164 } finally { 165 base.releaseUpdateLock(); 166 } 167 168 } catch (Exception e) { 169 log.error("unable to aquire update lock", e); 170 } 171 } 172 173 176 public void cacheGroupFlushed(CacheGroupEvent arg0) { 177 log.debug("IndexCache group flushed"); 179 180 } 181 182 185 public void cachePatternFlushed(CachePatternEvent arg0) { 186 log.debug("IndexCache pattern flushed"); 188 } 189 190 193 public void cacheFlushed(CachewideEvent arg0) { 194 195 log.debug("All IndexCache flushed"); 196 197 } 198 199 } 200 | Popular Tags |