1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
| package com.hngd.config;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer; import com.hngd.annotation.CacheExpire; import lombok.extern.log4j.Log4j2; import org.springframework.beans.BeansException; import org.springframework.beans.factory.InitializingBean; import org.springframework.cache.Cache; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Caching; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.data.redis.cache.RedisCache; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheWriter; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.util.ReflectionUtils;
import java.time.Duration; import java.util.*; import java.util.concurrent.Callable;
@Log4j2 public class TedisCacheManager extends RedisCacheManager implements ApplicationContextAware, InitializingBean {
private ApplicationContext applicationContext;
private Map<String, RedisCacheConfiguration> initialCacheConfiguration = new LinkedHashMap<>();
public static final StringRedisSerializer STRING_SERIALIZER = new StringRedisSerializer();
public static final GenericFastJsonRedisSerializer FASTJSON_SERIALIZER = new GenericFastJsonRedisSerializer();
public static final RedisSerializationContext.SerializationPair<String> STRING_PAIR = RedisSerializationContext .SerializationPair.fromSerializer(STRING_SERIALIZER);
public static final RedisSerializationContext.SerializationPair<Object> FASTJSON_PAIR = RedisSerializationContext .SerializationPair.fromSerializer(FASTJSON_SERIALIZER);
public TedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) { super(cacheWriter, defaultCacheConfiguration); }
@Override public Cache getCache(String name) { Cache cache = super.getCache(name); return new RedisCacheWrapper(cache); }
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; }
@Override public void afterPropertiesSet() { String[] beanNames = applicationContext.getBeanNamesForType(Object.class); for (String beanName : beanNames) { final Class clazz = applicationContext.getType(beanName); add(clazz); } super.afterPropertiesSet(); }
@Override protected Collection<RedisCache> loadCaches() { List<RedisCache> caches = new LinkedList<>(); for (Map.Entry<String, RedisCacheConfiguration> entry : initialCacheConfiguration.entrySet()) { caches.add(super.createRedisCache(entry.getKey(), entry.getValue())); } return caches; }
private void add(final Class clazz) { ReflectionUtils.doWithMethods(clazz, method -> { ReflectionUtils.makeAccessible(method); CacheExpire cacheExpire = AnnotationUtils.findAnnotation(method, CacheExpire.class); if (cacheExpire == null) { return; } Cacheable cacheable = AnnotationUtils.findAnnotation(method, Cacheable.class); if (cacheable != null) { add(cacheable.cacheNames(), cacheExpire); return; } Caching caching = AnnotationUtils.findAnnotation(method, Caching.class); if (caching != null) { Cacheable[] cs = caching.cacheable(); if (cs.length > 0) { for (Cacheable c : cs) { if (cacheExpire != null && c != null) { add(c.cacheNames(), cacheExpire); } } } } else { CacheConfig cacheConfig = AnnotationUtils.findAnnotation(clazz, CacheConfig.class); if (cacheConfig != null) { add(cacheConfig.cacheNames(), cacheExpire); } } }, method -> null != AnnotationUtils.findAnnotation(method, CacheExpire.class)); }
private void add(String[] cacheNames, CacheExpire cacheExpire) { for (String cacheName : cacheNames) { if (cacheName == null || "".equals(cacheName.trim())) { continue; } long expire = cacheExpire.expire(); log.info("cacheName: {}, expire: {}", cacheName, expire); if (expire >= 0) { RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofSeconds(expire)) .disableCachingNullValues() .serializeKeysWith(STRING_PAIR) .serializeValuesWith(FASTJSON_PAIR); initialCacheConfiguration.put(cacheName, config); } else { log.warn("{} use default expiration.", cacheName); } } }
protected static class RedisCacheWrapper implements Cache { private final Cache cache;
RedisCacheWrapper(Cache cache) { this.cache = cache; }
@Override public String getName() { try { return cache.getName(); } catch (Exception e) { log.error("getName ---> errmsg: {}", e.getMessage(), e); return null; } }
@Override public Object getNativeCache() { try { return cache.getNativeCache(); } catch (Exception e) { log.error("getNativeCache ---> errmsg: {}", e.getMessage(), e); return null; } }
@Override public ValueWrapper get(Object o) { try { return cache.get(o); } catch (Exception e) { log.error("get ---> o: {}, errmsg: {}", o, e.getMessage(), e); return null; } }
@Override public <T> T get(Object o, Class<T> aClass) { try { return cache.get(o, aClass); } catch (Exception e) { log.error("get ---> o: {}, clazz: {}, errmsg: {}", o, aClass, e.getMessage(), e); return null; } }
@Override public <T> T get(Object o, Callable<T> callable) { try { return cache.get(o, callable); } catch (Exception e) { log.error("get ---> o: {}, errmsg: {}", o, e.getMessage(), e); return null; } }
@Override public void put(Object o, Object o1) { try { cache.put(o, o1); } catch (Exception e) { log.error("put ---> o: {}, o1: {}, errmsg: {}", o, o1, e.getMessage(), e); } }
@Override public ValueWrapper putIfAbsent(Object o, Object o1) { try { return cache.putIfAbsent(o, o1); } catch (Exception e) { log.error("putIfAbsent ---> o: {}, o1: {}, errmsg: {}", o, o1, e.getMessage(), e); return null; } }
@Override public void evict(Object o) { try { cache.evict(o); } catch (Exception e) { log.error("evict ---> o: {}, errmsg: {}", o, e.getMessage(), e); } }
@Override public void clear() { try { cache.clear(); } catch (Exception e) { log.error("clear ---> errmsg: {}", e.getMessage(), e); } } }
}
|