当前位置: 首页 > news >正文

redistemplate怎么修改数据_redisTemplate一opsForValue操作

当我们的数据存储到Redis的时候,我们的键(key)和值(value)都是通过Spring提供的Serializer序列化到数据库的。RedisTemplate默认使用的是JdkSerializationRedisSerializer,StringRedisTemplate默认使用的是StringRedisSerializer。

Spring-data-redis的序列化类有下面这几个:GenericToStringSerializer: 可以将任何对象泛化为字符串并序列化;Jackson2JsonRedisSerializer: 跟JacksonJsonRedisSerializer实际上是一样的;JacksonJsonRedisSerializer: 序列化object对象为json字符串;JdkSerializationRedisSerializer: 序列化java对象(被序列化的对象必须实现Serializable接口);StringRedisSerializer: 简单的字符串序列化;GenericToStringSerializer:类似StringRedisSerializer的字符串序列化;GenericJackson2JsonRedisSerializer:类似Jackson2JsonRedisSerializer,但使用时构造函数不用特定的类参考以上序列化,自定义序列化类;

Spring Data JPA为我们提供了下面的Serializer:GenericToStringSerializer、Jackson2JsonRedisSerializer、JacksonJsonRedisSerializer、JdkSerializationRedisSerializer、OxmSerializer、StringRedisSerializer。

JdkSerializationRedisSerializer: 使用JDK提供的序列化功能。优点是反序列化时不需要提供类型信息(class),但缺点是需要实现Serializable接口,还有序列化后的结果非常庞大,是JSON格式的5倍左右,这样就会消耗redis服务器的大量内存。

Jackson2JsonRedisSerializer: 使用Jackson库将对象序列化为JSON字符串。优点是速度快,序列化后的字符串短小精悍,不需要实现Serializable接口。但缺点也非常致命,那就是此类的构造函数中有一个类型参数,必须提供要序列化对象的类型信息(.class对象)。 通过查看源代码,发现其只在反序列化过程中用到了类型信息。

key和hashKey:推荐使用     StringRedisSerializer: 简单的字符串序列化

hashValue:推荐使用     GenericJackson2JsonRedisSerializer:类似Jackson2JsonRedisSerializer,但使用时构造函数不用特定的类

/**

* redis配置类

*/

@Configuration

public class RedisConfig {

@Bean

@SuppressWarnings("all")

public RedisTemplate redisTemplate(RedisConnectionFactory factory) {

RedisTemplate template = new RedisTemplate();

template.setConnectionFactory(factory);

Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

ObjectMapper om = new ObjectMapper();

om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

jackson2JsonRedisSerializer.setObjectMapper(om);

StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

// key采用String的序列化方式

template.setKeySerializer(stringRedisSerializer);

// hash的key也采用String的序列化方式

template.setHashKeySerializer(stringRedisSerializer);

// value也采用String的序列化方式

template.setValueSerializer(stringRedisSerializer);

// hash的value序列化方式采用jackson

template.setHashValueSerializer(jackson2JsonRedisSerializer);

template.afterPropertiesSet();

return template;

}

}

//redisTemplate string类型数据读取 V get(Object key);

redisTemplate.delete("hello");

redisTemplate.opsForValue().set("hello","hello");

System.out.println(redisTemplate.opsForValue().get("hello"));  //hello

//加入失效机制 void set(K key, V value, long timeout, TimeUnit unit);

redisTemplate.delete("world");

redisTemplate.opsForValue().set("world", "world", 2, TimeUnit.SECONDS);

Thread.sleep(1000);

System.out.println(redisTemplate.opsForValue().get("world"));  //world

Thread.sleep(1000);

System.out.println(redisTemplate.opsForValue().get("world"));  //null

//k v值是否与数据匹配 Boolean setIfAbsent(K key, V value);

System.out.println(redisTemplate.opsForValue().setIfAbsent("hello","hello")); //true

System.out.println(redisTemplate.opsForValue().setIfAbsent("world","world")); //false

//为多个键赋值,取值

//void multiSet(Map < ? extends K, ? extends V > map);

//List multiGet(Collection < K > keys);

//Boolean multiSetIfAbsent(Map< ? extends K, ? extends V > map);

Map map = new HashMap();

map.put("hello1","hello1");

map.put("hello2","hello2");

map.put("hello3","hello3");

redisTemplate.opsForValue().multiSet(map);

List keys = new ArrayList();

keys.add("hello1");

keys.add("hello2");

keys.add("hello3");

List results = redisTemplate.opsForValue().multiGet(keys);

for(String result :results){

System.out.println(result);

}

map.put("hello4","hello4");

System.out.println(redisTemplate.opsForValue().multiSetIfAbsent(map));  //false

//设置新值并返回旧值 V getAndSet(K key, V value);

System.out.println(redisTemplate.opsForValue().getAndSet("hello1","hello world")); //hello1

System.out.println(redisTemplate.opsForValue().get("hello1")); //hello world

redisTemplate.delete("hello6");

//数据追加 Integer append(K key, String value);

redisTemplate.opsForValue().set("hello6","hello");

System.out.println(redisTemplate.opsForValue().get("hello6")); //hello

Integer append = redisTemplate.opsForValue().append("hello6", "world");

System.out.println(redisTemplate.opsForValue().get("hello6")); //helloworld

//数据读取 String get(K key, long start, long end);

System.out.println(redisTemplate.opsForValue().get("hello6",0,2)); //hel

//获取字符串长度

System.out.println(redisTemplate.opsForValue().size("hello6")); //10

}

/** 输出结果

hello

world

null

false

true

hello1

hello2

hello3

false

hello1

hello world

hello

helloworld

hel

10

*/

但是这里有一个坑

由于选择序列化器的原因 不能测试这段代码,需要测试的话可以将

// value序列化方式采用jackson

template.setValueSerializer(jackson2JsonRedisSerializer);

//整形数据增加 Long increment(K key, long delta);

//浮点型数据增加 Double increment(K key, double delta);

redisTemplate.opsForValue().set("hello5",1);

redisTemplate.opsForValue().increment("hello5",2);

System.out.println(redisTemplate.opsForValue().get("hello5")); //3

redisTemplate.opsForValue().increment("hello5",2.1);

System.out.println(redisTemplate.opsForValue().get("hello5")); //5.1

然而将序列化器修改之后,又会出来一个新的坑

redisTemplate.delete("hello6");

//数据追加 Integer append(K key, String value);

redisTemplate.opsForValue().set("hello6","hello");

System.out.println(redisTemplate.opsForValue().get("hello6")); //hello

Integer append = redisTemplate.opsForValue().append("hello6", "world");

System.out.println(redisTemplate.opsForValue().get("hello6")); //hello

//数据读取 String get(K key, long start, long end);

System.out.println(redisTemplate.opsForValue().get("hello6",0,2)); //"he

//获取字符串长度

System.out.println(redisTemplate.opsForValue().size("hello6")); //12

而且这里的append之后立刻读取的数据仍然是第一次设置的值。

而且如上图,hello6的值也会加上两个引号

相关文章:

  • linux命令deploy_Linux deploy 超详细入门教程
  • word文档怎么到下一页去写_word文档怎么把下一页的内容移到上一页?
  • 绞车拆装实训报告_千斤顶实训报告.doc
  • incrby redis 最大值_Redis Incrby 命令
  • 包装函数 python_Python 在函数上添加包装器
  • kirin710f是什么处理器_麒麟710F处理器怎么样
  • epoll编程实例客户端_socket采用epoll编程demo
  • pythonsvd内存不足_python – 有没有办法防止numpy.linalg.svd内存不足?
  • python 统计分析apache日志_Apache 日志分析(一)
  • mysql中groupby会用到索引吗_MySQL优化GROUP BY方案
  • php5.6的apaches的dll_win7(64位)php5.6-Apache2.4-mysql5.6环境安装
  • freemarker反向取数_freemarker 取值(插值)(转)
  • miui 谷歌框架_谷歌和高通正式联手,加强安卓系统掌控,华为:鸿蒙正全面超越...
  • python 随机打乱列表_python打乱列表
  • 图片画圈画箭头用什么软件_什么软件可以在编辑图片中画圈圈,如裁图时需要特..._网络编辑_帮考网...
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • 「面试题」如何实现一个圣杯布局?
  • 「译」Node.js Streams 基础
  • 【Linux系统编程】快速查找errno错误码信息
  • Angular2开发踩坑系列-生产环境编译
  • Cookie 在前端中的实践
  • Flex布局到底解决了什么问题
  • gitlab-ci配置详解(一)
  • JAVA_NIO系列——Channel和Buffer详解
  • laravel with 查询列表限制条数
  • React 快速上手 - 07 前端路由 react-router
  • 基于Vue2全家桶的移动端AppDEMO实现
  • 如何选择开源的机器学习框架?
  • 腾讯视频格式如何转换成mp4 将下载的qlv文件转换成mp4的方法
  • 想使用 MongoDB ,你应该了解这8个方面!
  • 由插件封装引出的一丢丢思考
  • Java性能优化之JVM GC(垃圾回收机制)
  • SAP CRM里Lead通过工作流自动创建Opportunity的原理讲解 ...
  • ​ubuntu下安装kvm虚拟机
  • #[Composer学习笔记]Part1:安装composer并通过composer创建一个项目
  • #ifdef 的技巧用法
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • $redis-setphp_redis Set命令,php操作Redis Set函数介绍
  • (4) PIVOT 和 UPIVOT 的使用
  • (6)STL算法之转换
  • (6)设计一个TimeMap
  • (C语言)球球大作战
  • (LNMP) How To Install Linux, nginx, MySQL, PHP
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (第二周)效能测试
  • (二)WCF的Binding模型
  • (二)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (每日持续更新)jdk api之StringBufferInputStream基础、应用、实战
  • (一)Thymeleaf用法——Thymeleaf简介
  • (转)Java socket中关闭IO流后,发生什么事?(以关闭输出流为例) .
  • .NET C#版本和.NET版本以及VS版本的对应关系
  • .Net core 6.0 升8.0
  • .Net Web窗口页属性
  • .net6Api后台+uniapp导出Excel
  • .NET建议使用的大小写命名原则