springboot基本使用九(redis和springcache缓存)

为什么使用缓存:

    减少数据库访问次数,从而提高应用程序的性能

redis可以缓存为啥要和spring cache一起使用?

    redis缓存:是内存级的缓存。它是使用单纯的内存来进行缓存

    spring cache缓存:使用JVM的内存来缓存对象的,这势必会造成大量的内存消耗。但好处是显然的:使用方便

    集群环境下,每台服务器的spring cache是不同步的,这样会出问题的,spring cache只适合单机环境

     Redis是设置单独的缓存服务器,所有集群服务器统一访问redis,不会出现缓存不同步的情况

使用springboot 使用redis:

     首先需要下载redis,下载window版本的,解压就行

     启动redis:

redis依赖:

<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

数据库表信息:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for city
-- ----------------------------
DROP TABLE IF EXISTS `city`;
CREATE TABLE `city`  (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `cname` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `pid` int NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT =
  • 25
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Spring Boot中使用Redis缓存,你需要进行以下几个步骤: 1. 添加依赖:在pom.xml文件中添加Spring Data Redis依赖。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 配置Redis连接信息:在application.properties或application.yml文件中配置Redis连接信息,如下所示: ```yaml spring.redis.host=your_redis_host spring.redis.port=your_redis_port spring.redis.password=your_redis_password (如果有密码的话) ``` 3. 创建Redis配置类:创建一个Redis配置类,用于配置RedisTemplate和CacheManager。可以参考下面的示例: ```java @Configuration @EnableCaching public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); // 设置key和value的序列化器 template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } @Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() .disableCachingNullValues() .entryTtl(Duration.ofMinutes(10)) // 设置缓存过期时间为10分钟 .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); return RedisCacheManager.builder(redisConnectionFactory) .cacheDefaults(cacheConfiguration) .build(); } } ``` 4. 使用缓存注解:在需要使用缓存的方法上添加缓存注解,如@Cacheable、@CachePut、@CacheEvict等。例如: ```java @Service public class UserService { @Autowired private UserRepository userRepository; @Cacheable(value = "users", key = "#id") public User getUserById(Integer id) { return userRepository.findById(id).orElse(null); } @CachePut(value = "users", key = "#user.id") public User saveUser(User user) { return userRepository.save(user); } @CacheEvict(value = "users", key = "#id") public void deleteUser(Integer id) { userRepository.deleteById(id); } } ``` 以上就是在Spring Boot中使用Redis缓存基本步骤。你可以根据实际需求进行配置和使用不同的缓存注解。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

twx95

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值