redis spring 切面缓存_今日份学习: Spring中使用AOP并实现redis缓存?

笔记

在Spring中如何使用AOP?

Spring是如何切换JDK动态代理和CGLIB的?

spring.aop.proxy-target-class=true (在下方第二个链接中,原生doc中提到过)

@Aspect生命切面

@Before

@After

@Around

Redis

广泛使用的内存缓存

常见的数据结构:

String

List

Set

Hash

ZSet

Redis为什么快?

完全基于内存

优秀的数据结构设计

单一线程,避免上下文切换开销

事件驱动,非阻塞

浏览的一些学习资料

2020年2月8日 更新:

如何使用再spring boot中redis?

这里我使用了docker容器

首先引入pom.xml

org.springframework.boot

spring-boot-starter-data-redis

docker中运行redis,端口为6379

docker run -p 6379:6379 -d redis

创建了一个名为config的package,并创建了config/AppConfig.java

@Configuration

public class AppConfig {

@Bean

RedisTemplate redisTemplate(RedisConnectionFactory factory) {

RedisTemplate redisTemplate = new RedisTemplate<>();

redisTemplate.setConnectionFactory(factory);

return redisTemplate;

}

}

使用AOP实现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;

}

}

}

补:参考到的网址:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值