博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
redis
阅读量:4043 次
发布时间:2019-05-24

本文共 2423 字,大约阅读时间需要 8 分钟。

pring针对Redis

Spring针对Redis的使用,封装了一个比较强大的Template以方便使用;

RedisTemplate访问Redis数据结构(一)——String/List/Hash/Set/ZSet

首先初始化spring工厂获得redisTemplate和opsForHash

常用集合:https://www.iteye.com/blog/357029540-2389150
//建立map集合
Map<String, String> map=new HashMap<>();

private RedisTemplate
redisTemplate;private HashOperations
opsForHash;param.put("key1", "a");param.put("key2", "b");param.put("key3", "c");param.put("key4", 4);opsForHash.putAll("he3", param); //存储hash对象redisTemplate.opsForSet().add("set_eq_settings",motorId);System.out.println(opsForHash.increment("he3", "key4", 1));//5System.out.println(opsForHash.hasKey("he", "key2"));//false hasKeySystem.out.println(opsForHash.get("he5", "key1"));//aopsForHash.delete("he3", "key1");opsForHash.put("he1", "key3", "c");Map
entries = opsForHash.entries("he1");keys.add("key1");//multiGetkeys.add("key");keys.add("key2");System.out.println(opsForHash.multiGet("he6", keys))System.out.println(opsForHash.putIfAbsent("he10", "key1", "a"));//true

————————————————使用

System.out.println(opsForHash.size("he9"));//5Set
keys = opsForHash.keys("he8");for (Object key : keys) {
Object value = redisMap.get("eq_status_"+motorId, key); System.out.println((String) key+":"+(String) value); switch ((String)key){
case "speed" : {
if(value!=null) motorStatus.setSpeed(Integer.parseInt(((String)value).trim())); break; } Set
keys = redisMap.keys("eq_status_"+motorId); System.out.println("keys:"+keys.toString()); Set motors = redisTemplate.opsForSet().members("set_eq_status"); #根据key寻找#删除 Set keys = redisMap.keys("eq_status_"+motorId);for (Object key:keys){
redisMap.delete("eq_status_"+motorId,key);}就redisTemplate.opsForSet().remove("set_eq_status",motorId);

redis

pom.xml:    
org.springframework.boot
spring-boot-starter-data-redis
redis.properties:设置redis配置spring-redis.xml

通过pipeline方式当有大批量的操作时候,我们可以节省很多原来浪费在网络延迟的时间,需要注意到是用pipeline方式打包命令发送,redis必须在处理完所有命令前先缓存起所有命令的处理结果。service调用:

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

@Resourcepublic JedisConnectionFactory jedisConnectionFactory;private static Jedis jedis;    if (jedis == null) {        jedis = (Jedis)jedisConnectionFactory.getConnection().getNativeConnection();    }

转载地址:http://tlhdi.baihongyu.com/

你可能感兴趣的文章
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
让我做你的下一行Code
查看>>
浅析:setsockopt()改善程序的健壮性
查看>>
关于对象赋值及返回临时对象过程中的构造与析构
查看>>
VS 2005 CRT函数的安全性增强版本
查看>>
SQL 多表联合查询
查看>>
Visual Studio 2010:C++0x新特性
查看>>
drwtsn32.exe和adplus.vbs进行dump文件抓取
查看>>
cppcheck c++静态代码检查
查看>>
在C++中使用Lua
查看>>