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 ActionCache { 010 011 private static final Log LOG = Log.getLog(ActionCache.class); 012 013 private static JbootCache actionCache; 014 015 public static JbootCache setThreadCacheNamePrefix(String cacheNamePrefix) { 016 return getActionCache().setThreadCacheNamePrefix(cacheNamePrefix); 017 } 018 019 public static void clearThreadCacheNamePrefix() { 020 getActionCache().clearThreadCacheNamePrefix(); 021 } 022 023 static JbootCache getActionCache() { 024 if (actionCache == null) { 025 actionCache = JbootCacheManager.me().getCache(AopCacheConfig.getInstance().getUseCacheName()); 026 } 027 return actionCache; 028 } 029 030 public static void setActionCache(JbootCache actionCache) { 031 ActionCache.actionCache = actionCache; 032 } 033 034 035 public static void put(String cacheName, Object key, Object value) { 036 try { 037 getActionCache().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 getActionCache().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 getActionCache().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 getActionCache().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 getActionCache().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 getActionCache().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 getActionCache().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 getActionCache().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 getActionCache().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 getActionCache().setTtl(cacheName, key, seconds); 122 } catch (Exception ex) { 123 LOG.error(ex.toString(), ex); 124 } 125 } 126 127 private static final ActionCacheConfig CONFIG = ActionCacheConfig.getInstance(); 128 129 public static void putDataToCache(String cacheName, String cacheKey, Object data, int liveSeconds) { 130 liveSeconds = liveSeconds > 0 ? liveSeconds : CONFIG.getLiveSeconds(); 131 if (liveSeconds > 0) { 132 put(cacheName, cacheKey, data, liveSeconds); 133 } else { 134 put(cacheName, cacheKey, data); 135 } 136 } 137}