• redis中使用pipeline批量执行命令,提升性能


    注意:此操作非原子性 
           将一批要执行的redis命令提交到pipeline中,pipeline一次性的将数据发送给服务器,服务器再逐条执行命令。

            redisTemplate中已经提供了对应方法executePipelined()可以直接调用,它支持两个类型的参数:RedisCallback更接近redis原生命令,但是需要自己将键和值都转换为字节码传递过去;SessionCallback对操作进行了封装,可以根据操作不同的数据类型进行转换,方便api使用

    代码示例

    1. import lombok.extern.slf4j.Slf4j;
    2. import org.example.service_a.service_a_App;
    3. import org.junit.jupiter.api.Test;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.boot.test.context.SpringBootTest;
    6. import org.springframework.dao.DataAccessException;
    7. import org.springframework.data.redis.connection.RedisConnection;
    8. import org.springframework.data.redis.core.*;
    9. import java.nio.charset.StandardCharsets;
    10. import java.util.List;
    11. @SpringBootTest(classes={service_a_App.class})
    12. @Slf4j
    13. public class Test_Pipelined {
    14. @Autowired
    15. private StringRedisTemplate redisTemplate;
    16. @Test
    17. void executePipelined_RedisCallback() {
    18. List<Object> datas = redisTemplate.executePipelined(new RedisCallback<Object>() {
    19. @Override
    20. public Object doInRedis(RedisConnection connection) throws DataAccessException {
    21. connection.set("key1".getBytes(StandardCharsets.UTF_8), "value1".getBytes(StandardCharsets.UTF_8));
    22. connection.set("key2".getBytes(StandardCharsets.UTF_8), "value2".getBytes(StandardCharsets.UTF_8));
    23. connection.set("key3".getBytes(StandardCharsets.UTF_8), "value3".getBytes(StandardCharsets.UTF_8));
    24. connection.set("key4".getBytes(StandardCharsets.UTF_8), "value4".getBytes(StandardCharsets.UTF_8));
    25. connection.set("key5".getBytes(StandardCharsets.UTF_8), "value5".getBytes(StandardCharsets.UTF_8));
    26. connection.set("key6".getBytes(StandardCharsets.UTF_8), "value6".getBytes(StandardCharsets.UTF_8));
    27. connection.get("key1".getBytes(StandardCharsets.UTF_8));
    28. // 这里必须返回null,在 connection.closePipeline() 时覆盖原来的返回值,所以返回值没有必要设置,设置会报错
    29. return null;
    30. }
    31. });
    32. System.out.println("datas = " + datas);
    33. }
    34. @Test
    35. void executePipelined_SessionCallback() {
    36. List<Object> datas = redisTemplate.executePipelined(new SessionCallback<Object>() {
    37. @Override
    38. public <K, V> Object execute(RedisOperations<K, V> operations) throws DataAccessException {
    39. ValueOperations<String, String> op1 = (ValueOperations<String, String>) operations.opsForValue();
    40. op1.set("key7", "value7");
    41. op1.set("key8", "value8");
    42. op1.get("key2");
    43. SetOperations<String, String> op2 = (SetOperations<String, String>) operations.opsForSet();
    44. op2.add("set_demo", "value1", "value2", "value3");
    45. op2.randomMember("set_demo");
    46. return null;
    47. }
    48. });
    49. System.out.println("datas = " + datas);
    50. }
    51. }

  • 相关阅读:
    雷军:穿越人生低谷的感悟(节选)
    解决nginx: [emerg] unknown directive “stream“ in /etc/nginx/nginx.conf问题
    分库分表解决300亿记录存储的三个方案方法
    Cesium加载离线地图和离线地形
    第三章《数组与循环》第7节:break与continue关键字
    维持原判,链家程序员删除9TB数据二审判7年;AirPods或将换用USB-C接口;马斯克被推特指责违反保密协议|极客头条
    【回眸】安装Hightec后如何导入源码及相关环境配置
    Java毕业设计-校园活动赞助与宣传管理系统
    Kubernetes技术--k8s核心技术 configMap
    基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
  • 原文地址:https://blog.csdn.net/qq_41712271/article/details/138193916