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的更多相关文章

  1. redis 管道技术 pipeline 简介

    redis数据库的主要瓶颈是网络速度,其次是内存与cpu.在应用允许的情况下,优先使用pipeline批量操作.pipeline批量发出请求/一次性获取响应:不是发出多个请求,每个请求都阻塞等待响应, ...

  2. Redis 管道技术

    Redis是一种基于客户端-服务端模型以及请求/响应协议的TCP服务.这意味着通常情况下一个请求会遵循以下步骤: 客户端向服务端发送一个查询请求,并监听Socket返回,通常是以阻塞模式,等待服务端响 ...

  3. redis管道技术

    1.redis管道pipeline解决的问题: 由于redis通信是通过tcp协议基础,并且是堵塞的处理方式,在第一个请求没有执行并返回前,无法处理第二个请求.所以事件浪费在了网络传输和堵塞请求中. ...

  4. Redis 管道(pipeline)

  5. Redis 管道pipeline

    Redis是一个cs模式的tcp server,使用和http类似的请求响应协议. 一个client可以通过一个socket连接发起多个请求命令. 每个请求命令发出后client通常会阻塞并等待red ...

  6. Redis 数据备份与恢复,安全,性能测试,客户端连接,管道技术,分区(四)

    Redis 数据备份与恢复 Redis SAVE 命令用于创建当前数据库的备份. 语法 redis Save 命令基本语法如下: redis 127.0.0.1:6379> SAVE 实例 re ...

  7. 缓存数据库-redis(管道)

    一:Redis 管道技术 Redis是一种基于客户端-服务端模型以及请求/响应协议的TCP服务.这意味着通常情况下一个请求会遵循以下步骤: 客户端向服务端发送一个查询请求,并监听Socket返回,通常 ...

  8. Redis 新特性---pipeline(管道)

    转载自http://weipengfei.blog.51cto.com/1511707/1215042 Redis本身是一个cs模式的tcp server, client可以通过一个socket连续发 ...

  9. .NET客户端实现Redis中的管道(PipeLine)与事物(Transactions)

    序言 Redis中的管道(PipeLine)特性:简述一下就是,Redis如何从客户端一次发送多个命令,服务端到客户端如何一次性响应多个命令. Redis使用的是客户端-服务器模型和请求/响应协议的T ...

  10. Redis中的管道(PipeLine)与事物(Transactions)

    Redis中的管道(PipeLine)与事物(Transactions) 序言 Redis中的管道(PipeLine)特性:简述一下就是,Redis如何从客户端一次发送多个命令,服务端到客户端如何一次 ...

随机推荐

  1. GZY.Quartz.MUI(基于Quartz的UI可视化操作组件) 2.6.0发布 兼容.Net8.0

    前言 为了迎接.Net8.0 2.6.0终于发布了~ 更新内容: 兼容.NET8.0 新增界面按分组名称排序功能 优化本地持久化时文件路径异常的问题 优化数据库持久化时偶现的异常问题 新增简易授权,增 ...

  2. [.NET开发者的福音]一个方便易用的在线.NET代码编辑工具.NET Fiddle

    前言 今天给大家分享一个方便易用的.NET在线代码编辑工具,能够帮助.NET开发人员快速完成代码编写.测试和分享的需求(.NET开发者的福音):.NET Fiddle. .NET Fiddle介绍 我 ...

  3. 换热站数字孪生 | 图扑智慧供热 3D 可视化

    前言 换热站作为供热系统不可或缺的一部分,其能源消耗对城市环保至关重要.在双碳目标下,供热企业可通过搭建智慧供热系统,实现供热方式的低碳.高效.智能化,从而减少碳排放和能源浪费.通过应用物联网.大数据 ...

  4. MySQL运维9-Mycat分库分表之枚举分片

    一.枚举分片 通过在配置文件中配置可能的枚举值,指定数据分布到不同数据节点上,这种方式就是枚举分片规则,本规则适用于按照省份,性别,状态拆分数据等业务 二.枚举分片案例 枚举分片需求:现有 tb_en ...

  5. 组合式api-ref引用子组件、dom元素, defineExpose的使用

    和vue2一样,我们有时候希望父组件能够调用子组件中的方法.属性.那么就要用到ref. 然后你会发现,根本调用不了子组件中的方法"sonSayHi",如下图: 原因: 使用

  6. 【2016】CloneCD和IsoBuster配合使用以提取VCD中的文件

    **笔记记录于:2016-11-24 ** 本文章仅供用于技术研究用途,请勿利用文章内容操作用于违反法律的事情. 起因: 公司老总让我提取下VCD中的文件以备份下,但是把光碟放进DVD光驱中发现只有几 ...

  7. RIPEMD加密算法:原理、应用与安全性

    一.引言 在信息时代,数据安全愈发受到重视,加密算法作为保障信息安全的关键技术,其性能和安全性备受关注.RIPEMD(RACE Integrity Primitives Evaluation Mess ...

  8. 【UniApp】-uni-app-打包成网页

    前言 经过上一篇文章的介绍,已经将这个计算器的计算功能实现了,接下来就是我们项目当中的一个发包上线阶段,我模拟一下,目的就是为了给大家介绍一下,uni-app是如何打包成网页的. 除了可以打包成网页, ...

  9. ElasticSearch之Delete index API

    删除指定的索引. 同时删除索引关联的数据.分片.元数据等相关的资源,因此执行前需要慎重. 命令样例如下: curl -X DELETE "https://localhost:9200/tes ...

  10. Asp .Net Core系列:基于MySQL的DBHelper帮助类和SQL Server的DBHelper帮助类

    目录 MySQLDBHelper MSSQLDBHelper MySQLDBHelper app.config中添加配置 <connectionStrings> <add name= ...