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如何从客户端一次发送多个命令,服务端到客户端如何一次 ...
随机推荐
- Windows风格的个人网盘,支持文档在线编辑
这是一个Windows页面风格的个人网盘,支持普通文本.文档.表格.xmind等在线编辑,支持Windows的小组件,例如计算器.WhiteBoard.python 等,其他更多功能待你来发现,也待你 ...
- CoreFlex框架发布 `0.1.1`
CoreFlex框架发布 0.1.1 框架描述 CoreFlex是一个支持.NET 6,.NET 7,.NET 8的快速开发框架,也提供MasaFramework相关框架的集成提供更多功能模块, 集成 ...
- 写入数据或者通过EXCEl批量导入到数据库时报类型转换异常问题
报错日志如下(此处我用的是达梦,实际MySQL和oracle也会有类似的问题): Cause: org.apache.ibatis.type.TypeException: Error setting ...
- 《深入理解 FFmpeg》第一章彩色插图汇总
layout: post title: "<深入理解 FFmpeg>第一章彩色插图" tags: - "FFmpeg" 这是<深入理解 FFm ...
- 华企盾DSC发送白名单提示“解密发送失败”(被中转服务器判定为垃圾邮箱)
解决方法:用DebugView监控,发现含有550错误,说明中转服务器被判定为垃圾邮箱,换一个中转邮箱或者设置为无中转发送 还有一种可能:邮箱设置了收到邮箱后自动删除 DebugView中显示上图 ...
- DEDECMS 后台系统用户授权目录更改为无限级(默认为二级授权)
在做一个学校的项目,分类有四级分类,总共一百多个分类,因为每个分类对应不同的老师,用于上传资料作为考核,但是添加系统用户的时候发现DEDECMS只有两级分类,所以修改啦一些代码,目前不知道是否修改完全 ...
- MySQL 的开窗函数
开窗函数 (Window Function)提供了行集之间的计算能力,在现代的主流关系型数据库中,基本都提供了相似的功能,这些功能在一些业务开发的过程中很有用,本文将简要介绍这些常用的开窗函数 ROW ...
- MySQL 基础(三)事务与 MVCC
事务 事务是一组原子性的 SQL 操作,或者被称为一个独立的工作单元,如果数据库引擎能够成功地对数据库应用该组的全部 SQL 语句,那么就会全部执行,否则全部不执行. 事务的特性 在关系数据库管理系统 ...
- 【OpenVINO 】在 MacOS 上编译 OpenVINO C++ 项目
前言 英特尔公司发行的模型部署工具OpenVINO模型部署套件,可以实现在不同系统环境下运行,且发布的OpenVINO 2023最新版目前已经支持MacOS系统并同时支持在苹果M系列芯片上部署模型.在 ...
- Mybatis源码3 CachingExecutor, 二级缓存,缓存的实现
Mybatis CachingExecutor, 二级缓存,缓存的实现 一丶二级缓存概述 上一章节,我们知道mybaits在构造SqlSession的时候,需要让SqlSession持有一个执行器,如 ...