• 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. }

  • 相关阅读:
    安卓最佳实践之内存优化
    卷起来了 手把手带你写一个中高级程序员必会的分布式RPC框架
    7. TTL 延迟队列
    生成删除数据库所有表的外检脚本
    WebDAV之π-Disk派盘 + 纸间书摘
    企业数字化建设有哪些路线可以选择?
    数据的4个等级
    微信小程序商城搭建,微信小程序商城源码,微信小程序商城项目
    【细读经典】springBoot源码(一)创建SpringApplication
    对称加密与非对称加密有什么区别,敏感数据怎么加解密和传输?
  • 原文地址:https://blog.csdn.net/qq_41712271/article/details/138193916