redis管道技术pipeline一 ——api
import java.io.UnsupportedEncodingException;
import java.util.Set; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; /**
* 封装redis 缓存服务器服务接口 */
@Service(value = "redisService")
public class RedisServiceImpl implements RedisService { private static String redisCode = "utf-8"; /**
* @param key
*/
public long del(final String... keys) {
return redisTemplate.execute(new RedisCallback() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
long result = 0;
for (int i = 0; i < keys.length; i++) {
result = connection.del(keys[i].getBytes());
}
return result;
}
});
} /**
* @param key
* @param value
* @param liveTime
*/
public void set(final byte[] key, final byte[] value, final long liveTime) {
redisTemplate.execute(new RedisCallback() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
connection.set(key, value);
if (liveTime > 0) {
connection.expire(key, liveTime);
}
return 1L;
}
});
} /**
* @param key
* @param value
* @param liveTime
*/
public void set(String key, String value, long liveTime) {
this.set(key.getBytes(), value.getBytes(), liveTime);
} /**
* @param key
* @param value
*/
public void set(String key, String value) {
this.set(key, value, 0L);
} /**
* @param key
* @param value
*/
public void set(byte[] key, byte[] value) {
this.set(key, value, 0L);
} /**
* @param key
* @return
*/
public String get(final String key) {
return redisTemplate.execute(new RedisCallback() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
try {
return new String(connection.get(key.getBytes()), redisCode);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
});
} /**
* @param pattern
* @return
*/
public Setkeys(String pattern) {
return redisTemplate.keys(pattern); } /**
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.execute(new RedisCallback() {
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.exists(key.getBytes());
}
});
} /**
* @return
*/
public String flushDB() {
return redisTemplate.execute(new RedisCallback() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
connection.flushDb();
return "ok";
}
});
} /**
* @return
*/
public long dbSize() {
return redisTemplate.execute(new RedisCallback() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
return connection.dbSize();
}
});
} /**
* @return
*/
public String ping() {
return redisTemplate.execute(new RedisCallback() {
public String doInRedis(RedisConnection connection) throws DataAccessException { return connection.ping();
}
});
} }
测试:
package com.aswatson.applicaton; import com.alibaba.fastjson.JSONObject;
import com.aswatson.client.ChangeDataListener;
import com.aswatson.csc.member.vo.OptionVO;
import com.aswatson.csc.member.vo.ResponseMessages;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate; import java.util.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@Slf4j
public class PipelineRedisController { @Autowired
private RedisTemplate redisTemplate; @Autowired
private ChangeDataListener changeDataListener; // @RequestMapping(value = "/testException" )
// public void testException(){
// changeDataListener.processChangeData();
// } /**
* redis 批量操作其中一种方式
* redis pipeline 管道技术
*/
@RequestMapping(value = "/redisPipeline" )
public void redisPipeline(){ List<Object> list = new ArrayList();
JSONObject json = new JSONObject();
json.put("rr", "值rr");
list.add(json); redisTemplate.executePipelined(new RedisCallback<Object>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
connection.openPipeline();
Map<byte[],byte[]> tuple = new HashMap<>(); for (Object obj : list) {
JSONObject objName = JSONObject.parseObject(obj.toString());
Set<String> keys = objName.keySet();
for (String redisKey : keys) {
String value = objName.get(redisKey).toString();
tuple.put(redisKey.getBytes(), value.getBytes());
//设置过期时间
connection.mSet(tuple);
connection.expire(redisKey.getBytes(), 5);
System.out.println("set time=" + LocalDateTime.now());
}
}
connection.closePipeline();
return null;
}
});
} @GetMapping("/testValue/{key}")
public Object getMemberOptionType(@PathVariable(value="key") String key) {
System.out.println("get time=" + LocalDateTime.now() + "/" + testExpeireTime(key));
return LocalDateTime.now() + "/" + testExpeireTime(key);
} private Object testExpeireTime(String key) { return redisTemplate.execute(new RedisCallback() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
try {
return new String(connection.get(key.getBytes()), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
});
} // public static void main(String[] args) {
//
// List<Object> list = new ArrayList();
// JSONObject json = new JSONObject();
// json.put("kk", "测试kk");
// json.put("tt", "测试tt");
// list.add(json);
// for (Object obj : list) {
// System.out.println(obj);
// String [] arr = obj.toString().replace("{", "").replace("}", "").split("=");
// System.out.println(Arrays.asList(arr).get(0));
// System.out.println(Arrays.asList(arr).get(1));
//
// JSONObject objName = JSONObject.parseObject(obj.toString());
// System.out.println(objName.get("kk"));
// }
// } // public static void main(String[] args) {
//
// List list = new ArrayList();
// JSONObject json = new JSONObject();
// json.put("kk", "测试kk");
// list.add(json);
// for (Object obj : list) {
// //用json
// JSONObject objName = JSONObject.parseObject(obj.toString());
// Set<String> keys = objName.keySet();
// for (String redisKey : keys) {
// System.out.println(redisKey);
// System.out.println(objName.get(redisKey));
// }
//
// }
// } }
redis管道技术pipeline一 ——api的更多相关文章
- redis 管道技术 pipeline 简介
redis数据库的主要瓶颈是网络速度,其次是内存与cpu.在应用允许的情况下,优先使用pipeline批量操作.pipeline批量发出请求/一次性获取响应:不是发出多个请求,每个请求都阻塞等待响应, ...
- Redis 管道技术
Redis是一种基于客户端-服务端模型以及请求/响应协议的TCP服务.这意味着通常情况下一个请求会遵循以下步骤: 客户端向服务端发送一个查询请求,并监听Socket返回,通常是以阻塞模式,等待服务端响 ...
- redis管道技术
1.redis管道pipeline解决的问题: 由于redis通信是通过tcp协议基础,并且是堵塞的处理方式,在第一个请求没有执行并返回前,无法处理第二个请求.所以事件浪费在了网络传输和堵塞请求中. ...
- Redis 管道(pipeline)
- Redis 管道pipeline
Redis是一个cs模式的tcp server,使用和http类似的请求响应协议. 一个client可以通过一个socket连接发起多个请求命令. 每个请求命令发出后client通常会阻塞并等待red ...
- Redis 数据备份与恢复,安全,性能测试,客户端连接,管道技术,分区(四)
Redis 数据备份与恢复 Redis SAVE 命令用于创建当前数据库的备份. 语法 redis Save 命令基本语法如下: redis 127.0.0.1:6379> SAVE 实例 re ...
- 缓存数据库-redis(管道)
一:Redis 管道技术 Redis是一种基于客户端-服务端模型以及请求/响应协议的TCP服务.这意味着通常情况下一个请求会遵循以下步骤: 客户端向服务端发送一个查询请求,并监听Socket返回,通常 ...
- Redis 新特性---pipeline(管道)
转载自http://weipengfei.blog.51cto.com/1511707/1215042 Redis本身是一个cs模式的tcp server, client可以通过一个socket连续发 ...
- .NET客户端实现Redis中的管道(PipeLine)与事物(Transactions)
序言 Redis中的管道(PipeLine)特性:简述一下就是,Redis如何从客户端一次发送多个命令,服务端到客户端如何一次性响应多个命令. Redis使用的是客户端-服务器模型和请求/响应协议的T ...
- Redis中的管道(PipeLine)与事物(Transactions)
Redis中的管道(PipeLine)与事物(Transactions) 序言 Redis中的管道(PipeLine)特性:简述一下就是,Redis如何从客户端一次发送多个命令,服务端到客户端如何一次 ...
随机推荐
- 【结对作业】第一周 | 学习体会day02
今天我们想要实现线路的查询 发现了几个错误 1 <%-- 下拉表单的命名使用错误,导致无法接收前端数值--%> 首先我们很少使用下拉表单,之前用的也忘了,然后格式出现了错误 2 遇到typ ...
- .NET开源的处理分布式事务的解决方案
前言 在分布式系统中,由于各个系统服务之间的独立性和网络通信的不确定性,要确保跨系统的事务操作的最终一致性是一项重大的挑战.今天给大家推荐一个.NET开源的处理分布式事务的解决方案基于 .NET St ...
- PyTorch 中自定义数据集的读取方法
显然我们在学习深度学习时,不能只局限于通过使用官方提供的MNSIT.CIFAR-10.CIFAR-100这样的数据集,很多时候我们还是需要根据自己遇到的实际问题自己去搜集数据,然后制作数据集(收集数据 ...
- Redis工具类及Redis序列化
导入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- MySQL搭建主从集群详细步骤~
一. Docker安装MySQL搭建主从 docker run [OPTIONS] IMAGE [COMMAND] [ARG...] docker run -p 3306:3306 很多 -d --n ...
- 封装RabbitTemplate,使用自定义消息转换器
前面创建项目我就省了...活不多说直接上代码! 核心代码 RabbitMQConfig import lombok.extern.slf4j.Slf4j; import org.springframe ...
- 【算法】Java版
二分查找算法 二分查找算法(Binary Search Algorithm)是一种在有序数组中查找特定元素的搜索算法.该算法的基本思想是将数组从中间分成两部分,然后与目标元素进行比较,进而确定目标元素 ...
- 从零玩转文件上传之七牛云-qiniufileupload
title: 从零玩转文件上传之七牛云 date: 2022-03-27 02:21:00.478 updated: 2022-04-10 14:13:35.426 url: https://www. ...
- Springboot3核心特性
一.简介 1. 前置知识 Java17 Spring.SpringMVC.MyBatis Maven.IDEA 2. 环境要求 环境&工具 版本(or later) SpringBoot 3. ...
- java中的数据库连接池
常见的连接池的优缺点: HikariCP 优点: 性能出色,尤其在高并发负载下表现良好 内存消耗低,占用系统资源较少 具有自动化的连接池维护和统计功能 缺点: 需要 JDK7 或以上版本支持 配置选项 ...