001
014
015 package com.liferay.portal.cache.transactional;
016
017 import com.liferay.portal.kernel.cache.PortalCache;
018 import com.liferay.portal.kernel.cache.PortalCacheWrapper;
019
020 import java.io.Serializable;
021
022
026 public class TransactionalPortalCache<K extends Serializable, V>
027 extends PortalCacheWrapper<K, V> {
028
029 public TransactionalPortalCache(PortalCache<K, V> portalCache) {
030 super(portalCache);
031 }
032
033 @Override
034 public V get(K key) {
035 V result = null;
036
037 if (TransactionalPortalCacheHelper.isEnabled()) {
038 if (key == null) {
039 throw new NullPointerException("Key is null");
040 }
041
042 result = TransactionalPortalCacheHelper.get(portalCache, key);
043
044 if (result == NULL_HOLDER) {
045 return null;
046 }
047 }
048
049 if (result == null) {
050 result = portalCache.get(key);
051 }
052
053 return result;
054 }
055
056 @Override
057 public void put(K key, V value) {
058 doPut(key, value, DEFAULT_TIME_TO_LIVE, false);
059 }
060
061 @Override
062 public void put(K key, V value, int timeToLive) {
063 doPut(key, value, timeToLive, false);
064 }
065
066 @Override
067 public void putQuiet(K key, V value) {
068 doPut(key, value, DEFAULT_TIME_TO_LIVE, true);
069 }
070
071 @Override
072 public void putQuiet(K key, V value, int timeToLive) {
073 doPut(key, value, timeToLive, true);
074 }
075
076 @Override
077 public void remove(K key) {
078 if (TransactionalPortalCacheHelper.isEnabled()) {
079 if (key == null) {
080 throw new NullPointerException("Key is null");
081 }
082
083 TransactionalPortalCacheHelper.put(
084 portalCache, key, (V)NULL_HOLDER, false, DEFAULT_TIME_TO_LIVE);
085 }
086 else {
087 portalCache.remove(key);
088 }
089 }
090
091 @Override
092 public void removeAll() {
093 if (TransactionalPortalCacheHelper.isEnabled()) {
094 TransactionalPortalCacheHelper.removeAll(portalCache);
095 }
096 else {
097 portalCache.removeAll();
098 }
099 }
100
101 protected void doPut(K key, V value, int timeToLive, boolean quiet) {
102 if (TransactionalPortalCacheHelper.isEnabled()) {
103 if (key == null) {
104 throw new NullPointerException("Key is null");
105 }
106
107 if (value == null) {
108 throw new NullPointerException("Value is null");
109 }
110
111 if (timeToLive < 0) {
112 throw new IllegalArgumentException("Time to live is negative");
113 }
114
115 TransactionalPortalCacheHelper.put(
116 portalCache, key, value, quiet, timeToLive);
117 }
118 else {
119 if (quiet) {
120 portalCache.putQuiet(key, value, timeToLive);
121 }
122 else {
123 portalCache.put(key, value, timeToLive);
124 }
125 }
126 }
127
128 protected static Serializable NULL_HOLDER = "NULL_HOLDER";
129
130 }