您好,今天小周来为大家解答以上的问题。aliasfor注解相信很多小伙伴还不知道,现在让我们一起来看看吧!

aliasfor注解(@aliasfor用法)aliasfor注解(@aliasfor用法)


aliasfor注解(@aliasfor用法)


1、RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()@BeanObject key = keyGenerator.generate(target, mod, point.getArgs());public CacheMar cacheMar(RedisConnectionFactory redisConnectionFactory) {// 配置序列化// 统一默认配public CustomCacheAspect(RedisTemplate redisTemplate) {置,TTL为60秒.entryTtl(Duration.ofSeconds(60));// 针对不同key可以个性化设置Set cacheNames = new HashSet<>();cacheNames.add("account");// 对每个缓存空间应用不同的配置Map configMap = new HashMap<>();configMap.put("t", config);return RedisCacheMar.builder(redisConnectionFactory).cacheDefaults(config).initialCacheNames(cacheNames).withInitialCacheConfigurations(configMap).build();}自定义每个缓存的TTL和自动刷新上面的设置虽然可以提前设定每个cacheName的ttl,但是不够细,虽然Cacheable的sync可以防止缓存击穿,但是无法防止缓存雪崩。

2、代码写的比较糙,只是个例子,可以自己修改。

3、自己写TTL注解import org.springframework.core.annotation.AliasFor;import ja.lang.annotation.;@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)@Inheritedpublic @intece CacheTTL {long value() default 60;@AliasFor("value")long ttl() default 60;long preExpireRefresh() default 10;}AOP实现import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.reflect.ModSignature;import org.springframework.cache.interceptor.SimpleKeyGenerator;import org.springframework.core.annotation.AnnotationUtils;import org.springframework.core.annotation.Order;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;import ja.lang.reflect.Mod;import ja.util.Arrays;import ja.util.concurrent.TimeUnit;import ja.util.concurrent.atomic.AtomicInteger;//这里必须要比Cacheable的Aspect优先级要高@Order(value=1)@Aspect@Componentpublic class CustomCacheAspect {private final RedisTemplate redisTemplate;private final SimpleKeyGenerator keyGenerator = new SimpleKeyGenerator();private final AtomicInteger asyncLock = new AtomicInteger(0);this.redisTemplate = redisTemplate;}@After(value="execution( ...(..))")public void after(JoinPoint point){Object target = point.getTarget();Mod mod = signature.getMod();try {if (mod.isAnnotationPresent(CacheTTL.class) && mod.isAnnotationPresent(Cacheable.class)) {CacheTTL ttlData = AnnotationUtils.getAnnotation(mod, CacheTTL.class);Cacheable cacheAbleData = AnnotationUtils.getAnnotation(mod, Cacheable.class);long ttl = ttlData.ttl();long preExpireRefresh = ttlData.preExpireRefresh();String[] cacheNames = cacheAbleData.cacheNames();updateExpire(cacheNames,key,preExpireRefresh,ttl);}} catch (Exception e) {e.printStackTrace();public void updateExpire(String[] cacheNames,Object key,long preExpireRefresh,long ttl){if (asyncLockpareAndSet(0,1)){cacheName = cacheName + "::" + key;long expire = redisTemplate.getExpire(cacheName,TimeUnit.SECONDS);if (expire>0 && expire ttl || expire == -1){}});asyncLock.set(0);}使用@Cacheable(cacheNames = "account",key="#id",sync = true)@CacheTTL(ttl = 60,preExpireRefresh = 10)public Account findAccountByID(int id) {}。

本文到这结束,希望上面文章对大家有所帮助。