001
014
015 package com.liferay.portal.kernel.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 put(key, value, DEFAULT_TIME_TO_LIVE);
059 }
060
061 @Override
062 public void put(K key, V value, int timeToLive) {
063 if (TransactionalPortalCacheHelper.isEnabled()) {
064 if (key == null) {
065 throw new NullPointerException("Key is null");
066 }
067
068 if (value == null) {
069 throw new NullPointerException("Value is null");
070 }
071
072 if (timeToLive < 0) {
073 throw new IllegalArgumentException("Time to live is negative");
074 }
075
076 TransactionalPortalCacheHelper.put(
077 portalCache, key, value, timeToLive);
078 }
079 else {
080 portalCache.put(key, value, timeToLive);
081 }
082 }
083
084 @Override
085 public void remove(K key) {
086 if (TransactionalPortalCacheHelper.isEnabled()) {
087 if (key == null) {
088 throw new NullPointerException("Key is null");
089 }
090
091 TransactionalPortalCacheHelper.put(
092 portalCache, key, (V)NULL_HOLDER, DEFAULT_TIME_TO_LIVE);
093 }
094 else {
095 portalCache.remove(key);
096 }
097 }
098
099 @Override
100 public void removeAll() {
101 if (TransactionalPortalCacheHelper.isEnabled()) {
102 TransactionalPortalCacheHelper.removeAll(portalCache);
103 }
104 else {
105 portalCache.removeAll();
106 }
107 }
108
109 protected static Serializable NULL_HOLDER = "NULL_HOLDER";
110
111 }