001
014
015 package com.liferay.portal.cache.ehcache;
016
017 import com.liferay.portal.kernel.cache.AbstractPortalCache;
018 import com.liferay.portal.kernel.cache.PortalCacheManager;
019
020 import java.io.Serializable;
021
022 import java.util.List;
023
024 import net.sf.ehcache.Ehcache;
025 import net.sf.ehcache.Element;
026 import net.sf.ehcache.event.NotificationScope;
027 import net.sf.ehcache.event.RegisteredEventListeners;
028
029
034 public class EhcachePortalCache<K extends Serializable, V>
035 extends AbstractPortalCache<K, V> {
036
037 public EhcachePortalCache(
038 PortalCacheManager<K, V> portalCacheManager, Ehcache ehcache) {
039
040 super(portalCacheManager);
041
042 this.ehcache = ehcache;
043
044 RegisteredEventListeners registeredEventListeners =
045 ehcache.getCacheEventNotificationService();
046
047 registeredEventListeners.registerListener(
048 new PortalCacheCacheEventListener<K, V>(
049 aggregatedCacheListener, this),
050 NotificationScope.ALL);
051 }
052
053 @Override
054 public List<K> getKeys() {
055 return ehcache.getKeys();
056 }
057
058 @Override
059 public String getName() {
060 return ehcache.getName();
061 }
062
063 @Override
064 public void removeAll() {
065 ehcache.removeAll();
066 }
067
068 @Override
069 protected V doGet(K key) {
070 Element element = ehcache.get(key);
071
072 if (element == null) {
073 return null;
074 }
075
076 return (V)element.getObjectValue();
077 }
078
079 @Override
080 protected void doPut(K key, V value, int timeToLive) {
081 Element element = new Element(key, value);
082
083 if (timeToLive != DEFAULT_TIME_TO_LIVE) {
084 element.setTimeToLive(timeToLive);
085 }
086
087 ehcache.put(element);
088 }
089
090 @Override
091 protected V doPutIfAbsent(K key, V value, int timeToLive) {
092 Element element = new Element(key, value);
093
094 if (timeToLive != DEFAULT_TIME_TO_LIVE) {
095 element.setTimeToLive(timeToLive);
096 }
097
098 Element oldElement = ehcache.putIfAbsent(element);
099
100 if (oldElement == null) {
101 return null;
102 }
103
104 return (V)oldElement.getObjectValue();
105 }
106
107 @Override
108 protected void doRemove(K key) {
109 ehcache.remove(key);
110 }
111
112 @Override
113 protected boolean doRemove(K key, V value) {
114 Element element = new Element(key, value);
115
116 return ehcache.removeElement(element);
117 }
118
119 @Override
120 protected V doReplace(K key, V value, int timeToLive) {
121 Element element = new Element(key, value);
122
123 if (timeToLive != DEFAULT_TIME_TO_LIVE) {
124 element.setTimeToLive(timeToLive);
125 }
126
127 Element oldElement = ehcache.replace(element);
128
129 if (oldElement == null) {
130 return null;
131 }
132
133 return (V)oldElement.getObjectValue();
134 }
135
136 @Override
137 protected boolean doReplace(K key, V oldValue, V newValue, int timeToLive) {
138 Element oldElement = new Element(key, oldValue);
139
140 Element newElement = new Element(key, newValue);
141
142 if (timeToLive != DEFAULT_TIME_TO_LIVE) {
143 newElement.setTimeToLive(timeToLive);
144 }
145
146 return ehcache.replace(oldElement, newElement);
147 }
148
149 protected void reconfigEhcache(Ehcache ehcache) {
150 this.ehcache = ehcache;
151 }
152
153 protected volatile Ehcache ehcache;
154
155 }