当前位置: 首页 > news >正文

网站建设设计合同书个人网站主页设计模板

网站建设设计合同书,个人网站主页设计模板,北京新情况最新消息今天,艺术品网站模板Redis 批量处理 在开发中,有时需要对Redis 进行大批量的处理。 比如Redis批量查询多个Hash。如果是在for循环中逐个查询,那性能会很差。 这时,可以使用 Pipeline (管道)。 Pipeline (管道) Pipeline (管道) 可以一次性发送多条命令并在执…

Redis 批量处理

在开发中,有时需要对Redis 进行大批量的处理。

比如Redis批量查询多个Hash。如果是在for循环中逐个查询,那性能会很差。

这时,可以使用 Pipeline (管道)。

Pipeline (管道)

Pipeline (管道) 可以一次性发送多条命令并在执行完后一次性将结果返回,pipeline 通过减少客户端与 redis 的通信次数来实现降低往返延时时间,而且 Pipeline 实现的原理是队列,而队列的原理是时先进先出,这样就保证数据的顺序性。

RedisTemplate的管道操作

RedisTemplate的管道操作,使用**executePipelined()**方法。
org.springframework.data.redis.core.RedisTemplate#executePipelined(org.springframework.data.redis.core.SessionCallback<?>)

	@Overridepublic List<Object> executePipelined(SessionCallback<?> session, @Nullable RedisSerializer<?> resultSerializer) {Assert.isTrue(initialized, "template not initialized; call afterPropertiesSet() before using it");Assert.notNull(session, "Callback object must not be null");RedisConnectionFactory factory = getRequiredConnectionFactory();// 绑定连接RedisConnectionUtils.bindConnection(factory, enableTransactionSupport);try {return execute((RedisCallback<List<Object>>) connection -> {//打开管道connection.openPipeline();boolean pipelinedClosed = false;try {Object result = executeSession(session);//callback的返回值,只能返回null,否则会报错。if (result != null) {throw new InvalidDataAccessApiUsageException("Callback cannot return a non-null value as it gets overwritten by the pipeline");}//关闭管道,并获取返回值List<Object> closePipeline = connection.closePipeline();pipelinedClosed = true;return deserializeMixedResults(closePipeline, resultSerializer, hashKeySerializer, hashValueSerializer);} finally {if (!pipelinedClosed) {connection.closePipeline();}}});} finally {RedisConnectionUtils.unbindConnection(factory);}}
  • 注意:
    (1) executePipelined()的callback参数,实现execute() 方法只能返回null,否则会报错。
    报错: InvalidDataAccessApiUsageException: Callback cannot return a non-null value as it gets overwritten by the pipeline
    源码如下:
Object result = executeSession(session);
if (result != null) {
throw new InvalidDataAccessApiUsageException(
"Callback cannot return a non-null value as it gets overwritten by the pipeline");
}

(2)在executePipelined()中, 使用 get()方法 得到的value会是null。
在 redisTemplate进行管道操作,结果值只能通过 executePipelined()方法的返回值List获取.

	/*** Get the value of {@code key}.** @param key must not be {@literal null}.* 在管道(pipeline)中使用 get()方法 得到的value会是null* @return {@literal null} when used in pipeline / transaction.*/@NullableV get(Object key);
  • redisTemplate获取管道返回值:
List<Object> list = stringRedisTemplate.executePipelined(new SessionCallback<String>() {@Overridepublic  String execute(@NonNull RedisOperations operations) throws DataAccessException {//idList是多个id的集合for (String id : idList) {//key由前缀加唯一id组成String key = KEY_PREFIX + id;//在管道中使用 get()方法 得到的value会是null。value通过executePipelined()的返回值List<Object>获取。operations.opsForHash().get(key, field);}//Callback只能返回null, 否则报错:// InvalidDataAccessApiUsageException: Callback cannot return a non-null value as it gets overwritten by the pipelinereturn null;}});list.forEach(System.out::println);

Jedis操作管道

RedisTemplate操作管道比较方便,但如果要组装key和value的map,就会比较麻烦。
在这种情况下,可以使用Jedis。
比如,Jedis批量查询多个Hash,可以使用 Pipeline (管道)。
源码见: redis.clients.jedis.PipelineBase#hget(java.lang.String, java.lang.String)
hget()方法,跟普通hash的hget()有点类似,不过返回值是 Response。

    public Response<String> hget(String key, String field) {this.getClient(key).hget(key, field);return this.getResponse(BuilderFactory.STRING);}

示例:

public void testPipLine() {Map<String, Response<String>> responseMap = new HashMap<>();//try-with-resources, 自动关闭资源//先连接jedis,再拿到 pipelinetry (Jedis jedis = getJedis();Pipeline pipeline = jedis.pipelined()) {for (String id : idList) {//前缀加唯一idString key = KEY_PREFIX +  id;//使用pipeline.hget查询hash的数据Response<String> response = pipeline.hget(key, field);responseMap.put(id, response);}pipeline.sync();} catch (Exception ex) {log.error("responses error.", ex);}Map<String, String> map = new HashMap<>();//组装map。response.get()在pipeline关闭后才能执行,否则拿到的value都是nullresponseMap.forEach((k,response) -> map.put(k, response.get()));map.forEach((k,v)-> System.out.println(k+",val:"+v));}private static Pool<Jedis> jedisPool = null;/*** 连接redis,获取jedisPool* @return*/public Jedis getJedis() {if (jedisPool == null) {JedisPoolConfig poolConfig = new JedisPoolConfig();poolConfig.setMaxTotal(maxTotal);poolConfig.setMaxIdle(maxIdle);poolConfig.setMaxWaitMillis(maxWaitMillis);poolConfig.setTestOnBorrow(testOnBorrow);//配置可以写在配置中心/文件jedisPool = new JedisPool(poolConfig, host, port, timeout, password, database);}return jedisPool.getResource();}

参考资料

https://redis.io/docs/manual/pipelining/
https://www.cnblogs.com/expiator/p/11127719.html

http://www.yayakq.cn/news/749551/

相关文章:

  • 网站建设需求说明文档网络销售型网站有哪些内容
  • 沈阳市城市建设局网站icp查询系统
  • 虚拟主机 2个网站如何做防水网站
  • 网站后台为什么传不上图片wordpress可以放视频吗
  • 博罗网站建设网站上的图标怎么改
  • 石家庄无极网站建设网络软文
  • 做黎川旅游网站的目的重庆要做网站推广
  • 注册德国网站域名无水印视频素材下载网站
  • 做网站该读啥网站开发费的会计处理
  • 山东川畅科技联系 网站设计一般用什么做网站首页
  • Asp.net 手机网站制作上海公共招聘网app下载
  • 给企业做网站的公司西安为什么要建设学校网站
  • 广安市国土资源局网站建设银川网站开发制作
  • ui设计工具青岛seo网络推广
  • 网站建设时间怎么查青岛建设公司网站费用
  • 海洋专业做网站房产网签流程图
  • 南通网站建设机构济南网站建设方案报价
  • 网站建设需要实现哪些目标做网站不会P图怎么办
  • 做网站教程 简书photoshop手机版免费破解版
  • 吉安高端网站建设公司搜索引擎的设计与实现
  • 网站做seo外链如何做网站推广方式
  • 首次登陆建设银行网站图文解说360网站怎么做网址链接
  • 湖南茶叶品牌网站建设企业管理app软件
  • 一般网站后台都是哪里做贵阳手机银行app下载
  • 青岛企业建站系统企业网站定制开发流程
  • 广州建站推广做印刷去哪个网站找工作
  • 组合图片可以用在网站做链接吗阿里wordpress
  • 厦门做点击付费网站文创产品设计作品图片
  • 建设部网站法律法规网站建设哪些是需要外援的问题
  • 知名做网站价格广州英铭网站建设