javascript
redis spring 切面缓存_今日份学习: Spring中使用AOP并实现redis缓存?
筆記
在Spring中如何使用AOP?
Spring是如何切換JDK動(dòng)態(tài)代理和CGLIB的?
spring.aop.proxy-target-class=true (在下方第二個(gè)鏈接中,原生doc中提到過)
@Aspect生命切面
@Before
@After
@Around
Redis
廣泛使用的內(nèi)存緩存
常見的數(shù)據(jù)結(jié)構(gòu):
String
List
Set
Hash
ZSet
Redis為什么快?
完全基于內(nèi)存
優(yōu)秀的數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)
單一線程,避免上下文切換開銷
事件驅(qū)動(dòng),非阻塞
瀏覽的一些學(xué)習(xí)資料
2020年2月8日 更新:
如何使用再spring boot中redis?
這里我使用了docker容器
首先引入pom.xml
org.springframework.boot
spring-boot-starter-data-redis
docker中運(yùn)行redis,端口為6379
docker run -p 6379:6379 -d redis
創(chuàng)建了一個(gè)名為config的package,并創(chuàng)建了config/AppConfig.java
@Configuration
public class AppConfig {
@Bean
RedisTemplate redisTemplate(RedisConnectionFactory factory) {
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
}
使用AOP實(shí)現(xiàn)redis的緩存(代碼)
@Aspect
@Configuration
public class CacheAspect {
// Map cache = new HashMap<>();
@Autowired
RedisTemplate redisTemplate;
@Around("@annotation(emmm.anno.Cache)")
public Object cache(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String methodName = signature.getName();
Object cachedValue = redisTemplate.opsForValue().get(methodName);
// Object cachedValue = cache.get(methodName);
if (cachedValue != null) {
System.out.println("from cache");
return cachedValue;
} else {
System.out.println("from db");
Object realValue = joinPoint.proceed();
redisTemplate.opsForValue().set(methodName, realValue);
// cache.put(methodName, realValue);
return realValue;
}
}
}
補(bǔ):參考到的網(wǎng)址:
總結(jié)
以上是生活随笔為你收集整理的redis spring 切面缓存_今日份学习: Spring中使用AOP并实现redis缓存?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在没有光纤宽带的场所在没有光纤宽带的场所
- 下一篇: gson生成jsonobject_使用G