redis 缓存 @class: 会有 $hibernateproxy_Spring Boot学习09_整合Redis

一、环境搭建

1、在前面的Demo的基础上在pom文件上添加上redis的starter

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

2、在全局配置文件中指定配置

spring.redis.host=ip
spring.redis.port=6379
#如果有密码
spring.redis.password=
#连接超时时间(毫秒)
spring.redis.timeout=10000
​
#连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
#连接池中最大空闲数
spring.redis.jedis.pool.max-idle=8
#连接池中最小空闲连接
spring.redis.jedis.pool.min-idle=3

3、启动项目RedisCacheConfiguration也是匹配的,说明这次使用的缓存配置会使用Redis缓存的配置

1ef73d8e531776991a8f4b5c37b33a21.png

二、RedisTemplate&序列化机制

1、在RedisAutoConfiguraion自动配置类中会自动注册RedisTemplate组件和StringRedisTemplate组件

二者区别:

RedisTemplate:k-v都是Object类型

StringRedisTemplate:k-v都是String类型

Spring给用户提供了两个可用的模板,就像使用Hibernate会有HibernateTemplate,使用jdbc会有JdbcTemplate

025cd1495ad229a65ab2dca96ec813dd.png

2、所以我们使用的话就直接注入两个模板(注:使用RedisTemplate存储对象到缓存中,该对象所属的类一定要实现Serializable接口

Redis五大数据类型:String(字符串)、Hash(哈希表)、Set(集合)、List(链表)、Zset(有序集合)对应操作的方法如下

  • opsForValue[String]
  • opsForHash[Hash]
  • opsForSet[Set]
  • opsForList[List]
  • opsForZset[Zset]
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot09CacheApplicationTests {
    
​
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
​
    
    @Test
    public void test01() {
    
        ValueOperations<String, String> string = stringRedisTemplate.opsForValue();
        string.set("k1", "Hello");
    }
​
    @Test
    public void test02() {
    
        //源码可以找到默认使用JdkSerializationRedisSerializer
        ValueOperations<Object, Object> string = redisTemplate.opsForValue();
        Employee employee = employeeMapper.selectEmployeeById(1);
        string.set("emp01", employee);
    }

3、执行上述方法测试,查看redis缓存中的数据对象是被序列化成了二进制

ad5ee15ea2369a94ff2cca6ae3fa2123.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Java中使用注解操作Redis的示例: 首先,我们需要在pom.xml文件中添加Redis的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 接下来,我们需要在Spring Boot的Application类上添加@EnableCaching注解来开启缓存: ```java @SpringBootApplication @EnableCaching public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 然后,我们需要在Redis的配置文件application.properties中添加Redis的连接信息: ```properties spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= ``` 接下来,我们就可以使用注解来操作Redis了。下面是一个使用注解操作Redis缓存的示例: ```java @Service public class UserService { @Autowired private UserRepository userRepository; @Cacheable(value = "userCache", key = "#id") public User getUserById(Long id) { System.out.println("从数据库中获取用户信息"); return userRepository.findById(id).orElse(null); } @CachePut(value = "userCache", key = "#user.id") public User saveUser(User user) { System.out.println("保存用户信息到数据库"); return userRepository.save(user); } @CacheEvict(value = "userCache", key = "#id") public void deleteUserById(Long id) { System.out.println("从数据库中删除用户信息"); userRepository.deleteById(id); } } ``` 上面的示例中,我们使用了三个注解来操作Redis缓存: - @Cacheable:表示方法的结果可以被缓存,如果缓存中有数据,则直接返回缓存数据,否则执行方法并将结果放入缓存中。 - @CachePut:表示方法的结果需要被缓存,每次都执行方法,并将结果放入缓存中。 - @CacheEvict:表示方法缓存中删除数据。 在这个示例中,我们使用了value属性来指定缓存的名称,key属性来指定缓存的键,#id和#user.id是SpEL表达式,用于获取方法参数中的值。 以上就是一个使用注解操作Redis缓存的示例。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值