企业数据查询网站,wordpress 时区 8小时,网站建设的进度计划书,建材类网站建设方案文章目录 一、关于spring-redis二、springboot引入Redis及其使用案例三、封装redistemplate操作工具类 一、关于spring-redis
spring-data-redis针对jedis提供了如下功能#xff1a; 连接池自动管理#xff0c;提供了一个高度封装的“RedisTemplate”类 针对jedis客户端中大… 文章目录 一、关于spring-redis二、springboot引入Redis及其使用案例三、封装redistemplate操作工具类 一、关于spring-redis
spring-data-redis针对jedis提供了如下功能 连接池自动管理提供了一个高度封装的“RedisTemplate”类 针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口 ValueOperations简单K-V操作 SetOperationsset类型数据操作 ZSetOperationszset类型数据操作 HashOperations针对map类型的数据操作 ListOperations针对list类型的数据操作 提供了对key的“bound”(绑定)便捷化操作API可以通过bound封装指定的key然后进行一系列的操作而无须“显式”的再次指定Key即BoundKeyOperations BoundValueOperations BoundSetOperations BoundListOperations BoundSetOperations BoundHashOperations 将事务操作封装有容器控制。 针对数据的“序列化/反序列化”提供了多种可选择策略(RedisSerializer) JdkSerializationRedisSerializerPOJO对象的存取场景使用JDK本身序列化机制将pojo类通过ObjectInputStream/ObjectOutputStream进行序列化操作最终redis-server中将存储字节序列。是目前最常用的序列化策略。 StringRedisSerializerKey或者value为字符串的场景根据指定的charset对数据的字节序列编码成string是“new String(bytes, charset)”和“string.getBytes(charset)”的直接封装。是最轻量级和高效的策略。 JacksonJsonRedisSerializerjackson-json工具提供了javabean与json之间的转换能力可以将pojo实例序列化成json格式存储在redis中也可以将json格式的数据转换成pojo实例。因为jackson工具在序列化和反序列化时需要明确指定Class类型因此此策略封装起来稍微复杂。【需要jackson-mapper-asl工具支持】 OxmSerializer提供了将javabean与xml之间的转换能力目前可用的三方支持包括jaxbapache-xmlbeansredis存储的数据将是xml工具。不过使用此策略编程将会有些难度而且效率最低不建议使用。【需要spring-oxm模块的支持】
如果你的数据需要被第三方工具解析那么数据应该使用StringRedisSerializer而不是JdkSerializationRedisSerializer。
二、springboot引入Redis及其使用案例
springboot引入Redis及其使用案例 参考URL: https://www.cnblogs.com/yuqingya/p/12881712.html Springboot项目引入依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId
/dependency配置 spring:redis:host: 192.168.10.134port: 6379或 spring.redis.host192.168.10.134
spring.redis.port6379自定义redis配置类 由于Springboot-data-redis帮我们自动装载了RedisTemplate对象所以我们无需注册该bean。但是如果用默认的 RedisTemplate 那么在序列化存到redis中就会发现key 就变的“不正常”了。 比如存之前key为test 进入redis看key就变成了\xac\xed\x00\x05t\x00\x04test 。这与RedisTemplate默认提供的序列化协议有关。 Configuration
public class RedisConfiguration {Beanpublic RedisTemplateObject, Object redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplateObject, Object redisTemplate new RedisTemplate();redisTemplate.setConnectionFactory(redisConnectionFactory);// 使用Jackson2JsonRedisSerialize 替换默认序列化Jackson2JsonRedisSerializer jackson2JsonRedisSerializer new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper new ObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(objectMapper);// 设置value的序列化规则和 key的序列化规则redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);redisTemplate.setKeySerializer(jackson2JsonRedisSerializer);redisTemplate.afterPropertiesSet();return redisTemplate;}
}测试使用 这里使用SpringRunner.class 是Spring环境进行测试 RunWith(SpringRunner.class)
SpringBootTest(classes EdApplication.class)
public class FenCiUtilText {Autowiredprivate RedisTemplate redisTemplate;//测试放入Testpublic void testRedisSet() {try {redisTemplate.opsForValue().set(test,This is a Springboot-Redis test!);} catch (Exception e){System.out.println(e.toString());}}//测试拿出Testpublic void testRedisGet() {try {String keytest;Boolean isHas redisTemplate.hasKey(key);if (isHas){Object test redisTemplate.opsForValue().get(key);System.out.println(test);}else {System.out.println(抱歉不存在key值为key);}} catch (Exception e){System.out.println(e.toString());}}
}三、封装redistemplate操作工具类
springboot之使用redistemplate优雅地操作redis 参考URL: https://www.cnblogs.com/superfj/p/9232482.html
Service
public class RedisService {private static final Logger logger LoggerFactory.getLogger(RedisService.class);Autowiredprivate RedisTemplateString, String redisTemplate;/*** 默认过期时长单位秒*/public static final long DEFAULT_EXPIRE 60 * 60 * 24;/*** 不设置过期时长*/public static final long NOT_EXPIRE -1;public boolean set(final String key, String value){boolean result false;try {ValueOperations operations redisTemplate.opsForValue();operations.set(key, value);result true;} catch (Exception e) {logger.error(写入redis缓存失败错误信息为 e.getMessage());}return result;}public boolean set(final String key, String value, Long expire){boolean result false;try {ValueOperations operations redisTemplate.opsForValue();operations.set(key, value);redisTemplate.expire(key, expire, TimeUnit.SECONDS);result true;} catch (Exception e) {logger.error(写入redis缓存设置expire存活时间失败错误信息为 e.getMessage());}return result;}public String get(String key) {return key null ? null : redisTemplate.opsForValue().get(key);}public boolean exists(String key) {return redisTemplate.hasKey(key);}/*** 重名名key如果newKey已经存在则newKey的原值被覆盖** param oldKey* param newKey*/public void renameKey(String oldKey, String newKey) {redisTemplate.rename(oldKey, newKey);}/*** newKey不存在时才重命名** param oldKey* param newKey* return 修改成功返回true*/public boolean renameKeyNotExist(String oldKey, String newKey) {return redisTemplate.renameIfAbsent(oldKey, newKey);}/*** 删除key** param key*/public void deleteKey(String key) {redisTemplate.delete(key);}/*** 删除多个key** param keys*/public void deleteKey(String... keys) {SetString kSet Stream.of(keys).map(k - k).collect(Collectors.toSet());redisTemplate.delete(kSet);}/*** 删除Key的集合** param keys*/public void deleteKey(CollectionString keys) {SetString kSet keys.stream().map(k - k).collect(Collectors.toSet());redisTemplate.delete(kSet);}/*** 设置key的生命周期** param key* param time* param timeUnit*/public void expireKey(String key, long time, TimeUnit timeUnit) {redisTemplate.expire(key, time, timeUnit);}/*** 指定key在指定的日期过期** param key* param date*/public void expireKeyAt(String key, Date date) {redisTemplate.expireAt(key, date);}/*** 查询key的生命周期** param key* param timeUnit* return*/public long getKeyExpire(String key, TimeUnit timeUnit) {return redisTemplate.getExpire(key, timeUnit);}/*** 将key设置为永久有效** param key*/public void persistKey(String key) {redisTemplate.persist(key);}}