001package io.jboot.components.cache;
002
003
004import com.jfinal.log.Log;
005import com.jfinal.plugin.ehcache.IDataLoader;
006
007import java.util.List;
008
009public class AopCache {
010
011    private static final Log LOG = Log.getLog(AopCache.class);
012
013    private static JbootCache aopCache;
014
015    public static JbootCache setThreadCacheNamePrefix(String cacheNamePrefix) {
016        return getAopCache().setThreadCacheNamePrefix(cacheNamePrefix);
017    }
018
019    public static void clearThreadCacheNamePrefix() {
020        getAopCache().clearThreadCacheNamePrefix();
021    }
022
023    static JbootCache getAopCache() {
024        if (aopCache == null) {
025            aopCache = JbootCacheManager.me().getCache(AopCacheConfig.getInstance().getUseCacheName());
026        }
027        return aopCache;
028    }
029
030    public static void setAopCache(JbootCache aopCache) {
031        AopCache.aopCache = aopCache;
032    }
033
034
035    public static void put(String cacheName, Object key, Object value) {
036        try {
037            getAopCache().put(cacheName, key, value);
038        } catch (Exception ex) {
039            LOG.error(ex.toString(), ex);
040        }
041    }
042
043    public static void put(String cacheName, Object key, Object value, int liveSeconds) {
044        try {
045            getAopCache().put(cacheName, key, value, liveSeconds);
046        } catch (Exception ex) {
047            LOG.error(ex.toString(), ex);
048        }
049    }
050
051    public static List getKeys(String cacheName) {
052        try {
053            return getAopCache().getKeys(cacheName);
054        } catch (Exception ex) {
055            LOG.error(ex.toString(), ex);
056        }
057        return null;
058    }
059
060    public static void remove(String cacheName, Object key) {
061        try {
062            getAopCache().remove(cacheName, key);
063        } catch (Exception ex) {
064            LOG.error(ex.toString(), ex);
065        }
066    }
067
068    public static void removeAll(String cacheName) {
069        try {
070            getAopCache().removeAll(cacheName);
071        } catch (Exception ex) {
072            LOG.error(ex.toString(), ex);
073        }
074    }
075
076    public static <T> T get(String cacheName, Object key) {
077        try {
078            return getAopCache().get(cacheName, key);
079        } catch (Exception ex) {
080            LOG.error(ex.toString(), ex);
081            remove(cacheName, key);
082        }
083        return null;
084    }
085
086
087    public static <T> T get(String cacheName, Object key, IDataLoader dataLoader) {
088        try {
089            return getAopCache().get(cacheName, key, dataLoader);
090        } catch (Exception ex) {
091            LOG.error(ex.toString(), ex);
092            remove(cacheName, key);
093        }
094        return null;
095    }
096
097
098    public static <T> T get(String cacheName, Object key, IDataLoader dataLoader, int liveSeconds) {
099        try {
100            return getAopCache().get(cacheName, key, dataLoader, liveSeconds);
101        } catch (Exception ex) {
102            LOG.error(ex.toString(), ex);
103            remove(cacheName, key);
104        }
105        return null;
106    }
107
108
109    public static Integer getTtl(String cacheName, Object key) {
110        try {
111            return getAopCache().getTtl(cacheName, key);
112        } catch (Exception ex) {
113            LOG.error(ex.toString(), ex);
114        }
115        return null;
116    }
117
118
119    public static void setTtl(String cacheName, Object key, int seconds) {
120        try {
121            getAopCache().setTtl(cacheName, key, seconds);
122        } catch (Exception ex) {
123            LOG.error(ex.toString(), ex);
124        }
125    }
126
127
128    private static final AopCacheConfig CONFIG = AopCacheConfig.getInstance();
129
130   public static void putDataToCache(String cacheName, String cacheKey, Object data, int liveSeconds) {
131        liveSeconds = liveSeconds > 0 ? liveSeconds : CONFIG.getLiveSeconds();
132        if (liveSeconds > 0) {
133            put(cacheName, cacheKey, data, liveSeconds);
134        }
135        //永久有效
136        else if (liveSeconds == 0){
137            put(cacheName, cacheKey, data);
138        }
139        // -1 负数,取消 AOP 缓存
140        else {
141            // do nothing
142        }
143    }
144}