十一、Redis使用管道(Pipeline)提升性能

Redis服务是一种C/S模型,即客户端发起请求,服务端处理并返回结果给客户端,如果Redis客户端要发送很多条请求,后面的请求需要等待前面的请求处理完后才能进行处理,而且每个请求都存在往返时间,即使redis性能极高,当数据量足够大,也会极大影响性能,所以Redis为了改进该问题,引入了管道技术:可以在服务端未及时响应的时候,客户端也可以继续发送命令请求,做到客户端和服务端互不影响,服务端并最终返回所有服务端的响应,大大提高了C/S模型交互的响应速度;(比如批量对Redis操作)

1、在redisTemplate使用管道Pipeline

package com.gaia.test.redisTest.Pipeline;

import com.gaia.user.model.UserDO;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

import java.util.List;

/**
 * Description:
 *
 * @author xuzhiqiang
 * Date 2020/12/15
 */
public class JedisPipeline {

   
    private static ApplicationContext context;

    public static void main(String[] args) {
        context = new ClassPathXmlApplicationContext("classpath:spring-base-redis.xml");
        RedisTemplate<String, Object> redisTemplate = context.getBean(RedisTemplate.class);
        
        //系列化有坑
        RedisSerializer keySerializer = redisTemplate.getKeySerializer();
        RedisSerializer valueSerializer = redisTemplate.getValueSerializer();
        List<Object> list = redisTemplate.executePipelined(new RedisCallback<Long>() {
            @Override
            public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
                redisConnection.openPipeline();
                for (int i = 0; i<1000; i++) {
                    String key = "pipValue"+i;
                    redisConnection.set(keySerializer.serialize(key),valueSerializer.serialize(key));
                }
                return null;
            }
        });
        
        list.forEach((Object obj)->{
            System.out.println("value="+obj.toString());
        });

    }

}

2、在jedis中使用管道Pipeline

package com.gaia.test.redisTest.Pipeline;

import com.gaia.user.model.UserDO;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

import java.util.List;

/**
 * Description:
 *
 * @author xuzhiqiang
 * Date 2020/12/15
 */
public class JedisPipeline {


    private static ApplicationContext context;

    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        //jedis.auth("123456");
        Pipeline pipeline = jedis.pipelined();
        //批量操作
        for (int i=0; i<10; i++) {
            pipeline.incr("ppKey");
        }
        pipeline.sync();

        System.out.println(jedis.get("ppKey"));


    }

}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值