缓存+redis+容器

序言

    夏日炎炎,无风。。。从空调房间出来,再到接近四十度的高温,这个过程。。。缓存预热了解一下。。。


    为什么要用缓存?因为追求性能,因为要追求极致的用户体验。


    缓存基本上不需要持久化,从而是无状态的,那么使用容器是最好的方法了,而缓存系统,redis相当合适。。不要问为什么不用memcache。。。因为redis的功能比memcache多,因为redis还能持久化,因为redis还能支持各种数据结构,并且两者的性能相差无几,都能支持8万左右的TPS。

缓存理论

    1、 缓存适合的场景

    缓存,就是将一些需要读取数据放在磁盘或者内存中,由于是追求速度,从而一般放在内存中。

    在读取数据的时候,一般是从关系型数据库中读取数据,在数据库层面也可以进行各种优化,例如读性能不足,那么可以添加几个从库,从而数据库的一主多从;例如写性能不足,那么可以分库分表。

    在有些场景中,要使用缓存,是因为无法解决读的速度,例如count(*)的操作,无论从数据库的层面如何优化,都不可能提高;还有一种就是sql的执行本身就必须消耗很多资源和时间,例如各种关联查询子查询,这些时候,都可以将这些数据放在缓存当中,从而大大的减轻数据库的压力。


    2、 缓存穿透问题

    在使用缓存的时候,第一个问题就是缓存穿透的问题,就是使用了缓存和没使用缓存是一样的,应用程序到缓存中查询数据,发现数据么有,那么就去数据库查询,在这里,穿透了缓存,缓存没起到保护数据库的作用。

    在爬虫爬取网页的时候,每个分页都会爬取,而对于缓存的分页数据,一般只缓存了热点数据,也就是前几页的数据在缓存当中,如果全部爬取,那么就会造成缓存不命中,导致查询数据库;还有一种就是故意搜索一些不存在的关键字&

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用SpringBoot结合Redis实现缓存功能的步骤如下: 1. 在pom.xml文件中添加Redis依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 配置Redis连接信息,在application.properties文件中添加以下配置: ``` # Redis连接信息 spring.redis.host=<redis服务器IP> spring.redis.port=<redis服务器端口> spring.redis.password=<redis密码> ``` 3. 创建一个Redis配置类,用于将RedisTemplate注入到Spring容器中: ``` @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(lettuceConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } } ``` 4. 编写Cacheable注解,用于对需要缓存的方法进行标注: ``` @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Cacheable { // 缓存key的前缀 String prefix() default ""; // 缓存时间,默认为30分钟 long expireTime() default 1800L; } ``` 5. 编写缓存切面,对被Cacheable注解标注的方法进行缓存: ``` @Aspect @Component public class CacheAspect { @Autowired private RedisTemplate<String, Object> redisTemplate; @Around("@annotation(com.example.demo.annotation.Cacheable)") public Object cache(ProceedingJoinPoint joinPoint) throws Throwable { // 获取方法参数 Object[] args = joinPoint.getArgs(); // 获取方法名 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); String methodName = method.getName(); // 获取注解信息 Cacheable cacheable = method.getAnnotation(Cacheable.class); String prefix = cacheable.prefix(); long expireTime = cacheable.expireTime(); // 构造缓存key StringBuilder keyBuilder = new StringBuilder(); keyBuilder.append(prefix); for (Object arg : args) { keyBuilder.append(":").append(arg); } String key = keyBuilder.toString(); // 从缓存中获取数据 Object value = redisTemplate.opsForValue().get(key); if (value != null) { return value; } // 缓存中不存在则调用方法,将返回值存入缓存 Object result = joinPoint.proceed(args); redisTemplate.opsForValue().set(key, result, expireTime, TimeUnit.SECONDS); return result; } } ``` 6. 在需要进行缓存的方法上加上Cacheable注解,即可实现缓存功能: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override @Cacheable(prefix = "user", expireTime = 3600L) public User getUserById(Integer id) { return userDao.getUserById(id); } } ``` 这样,在调用getUserById方法时,如果缓存中已经存在数据,则直接返回缓存中的数据;否则调用方法,将返回值存入缓存,并返回结果。这样可以有效地减少数据库的访问次数,提高系统的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值