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如何从客户端一次发送多个命令,服务端到客户端如何一次 ...
随机推荐
- Access denied for user ‘root‘@‘localhost‘ (using password:YES)解决方法
修改jdbc.properties文件的密码
- 09 - Shell流程控制语句
1. if-else语句 能够使用if条件语句进行条件判断 1.1 if 语法 if 条件 then 命令 fi if 条件; then 命令; fi 1.2 if-else 语法 if 条件 the ...
- c++学习,和友元函数
第一友元函数访问私有元素时不会显示,但是是可以调用的(我使用的是gcc10.3版本的)友元函数可以访问任何元素.就是语法你别写错了. 继承如果父类已经写了构造函数,子类一定要赋值给构造函数,要么父类就 ...
- 开发AI量化策略所遇到的坑
AI只是工具,想要驾驭AI还得自身有点功底,不然反而会被工具所害,甚至从信仰AI变为抵制AI.本文简单介绍开发AI量化选股策略中所遇到的各种坑,希望大家有所收获,少走弯路. 本文为BigQuant用户 ...
- 数字孪生技术结合GIS系统能在农业领域作出什么改变?
数字孪生技术和地理信息系统(GIS)是两个独立但高度互补的领域,它们的结合在农业领域具有巨大的潜力,可以带来巨大的改变.在这篇文章中,我们将讨论数字孪生技术和GIS系统如何协同作用,为农业带来创新和可 ...
- Json Schema简介和Json Schema的.net实现库 LateApexEarlySpeed.Json.Schema
什么是Json Schema ? Json Schema是一种声明式语言,它可以用来标识Json的结构,数据类型和数据的具体限制,它提供了描述期望Json结构的标准化方法. 利用Json Schema ...
- Vue3节流指令封装
节流指令 import { ObjectDirective } from 'vue' interface ThrottleEl extends HTMLElement { throttleEvent: ...
- NC65二开经验总结
公式相关 1.显示公式没执行 列表界面显示,卡片界面不显示: Handler的onBoCard执行: getBillCardPanel().execHeadLoadFormulas(); Contro ...
- 有意思,我的GitHub账号值$23806.2,快来试试你的?
睡不着,看到一个有意思的网站:Estimate Github Worth Generator. 它可以用来估算 GitHub 账号的价值.马上试了一下. 我的账号估值:$23806.2 操作很简单,点 ...
- Asp .Net Core 集成 FluentValidation 强类型验证规则库
目录 入门程序 安装 案例:登录 验证器 内置验证器 自定义验证器 编写自定义验证器 可重复使用的属性验证器 本地化 DI 自动验证 官网:https://docs.fluentvalidation. ...