编程日记 更改redis存储默认序列化器
package com.haole.usercenter.service;import com.haole.usercenter.model.domain.User;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;/*** Redis 测试**/
@SpringBootTest
public class RedisTest {@Resourceprivate RedisTemplate redisTemplate;@Testvoid test() {ValueOperations valueOperations = redisTemplate.opsForValue();// 增valueOperations.set("yupiString", "dog");valueOperations.set("yupiInt", 1);valueOperations.set("yupiDouble", 2.0);User user = new User();user.setId(1L);user.setUsername("yupi");valueOperations.set("yupiUser", user);// 查Object yupi = valueOperations.get("yupiString");Assertions.assertTrue("dog".equals((String) yupi));yupi = valueOperations.get("yupiInt");Assertions.assertTrue(1 == (Integer) yupi);yupi = valueOperations.get("yupiDouble");Assertions.assertTrue(2.0 == (Double) yupi);System.out.println(valueOperations.get("yupiUser"));valueOperations.set("yupiString", "dog");redisTemplate.delete("yupiString");}
}
由于redis的增数据操作中使用java默认的序列化器jdk序列化器,会导致在QuickRedis界面显示的数据是乱码
解决方案:重新定义redis对象,指定序列化器为redisTemplate.setKeySerializer(RedisSerializer.string());
package com.haole.usercenter.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;/*** RedisTemplate 配置**/
@Configuration
public class RedisTemplateConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(connectionFactory);redisTemplate.setKeySerializer(RedisSerializer.string());return redisTemplate;}
}
由于传递的是对象,还是有乱码,不过序列化问题解决了
以下代码为使用redis缓存加快recommend主页推荐的速度(如果有缓存直接查缓存,没有的话就写入缓存)注意redis缓存要设置过期时间valueOperations.set(redisKey, userPage, 30000, TimeUnit.MILLISECONDS);表示缓存过期时间设置为30秒
@GetMapping("/recommend")public BaseResponse<Page<User>> recommendUsers(long pageSize, long pageNum, HttpServletRequest request) {User loginUser = userService.getLoginUser(request);String redisKey = String.format("yupao:user:recommend:%s", loginUser.getId());ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();// 如果有缓存,直接读缓存Page<User> userPage = (Page<User>) valueOperations.get(redisKey);if (userPage != null) {return ResultUtils.success(userPage);}// 无缓存,查数据库QueryWrapper<User> queryWrapper = new QueryWrapper<>();userPage = userService.page(new Page<>(pageNum, pageSize), queryWrapper);// 写缓存try {valueOperations.set(redisKey, userPage, 30000, TimeUnit.MILLISECONDS);} catch (Exception e) {log.error("redis set key error", e);}return ResultUtils.success(userPage);}