Redis——流水線(pipelined)


一、流水線:

  redis的讀寫速度十分快,所以系統的瓶頸往往是在網絡通信中的延遲。

  redis可能會在很多時候處於空閑狀態而等待命令的到達。

  為了解決這個問題,可以使用redis的流水線,流水線是一種通訊協議,類似一個隊列批量執行一組命令。

二、流水線使用/對比:

  1、未使用流水線處理10000次請求:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:redis.xml")
public class RedisTest {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void test(){
        // 開始時間
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            redisTemplate.opsForValue().set("k"+i,"v"+i);
        }
        // 結束時間
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }
}

  //耗時:5692;

  2、使用流水線:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:redis.xml")
public class RedisTest {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void test(){
        // 開始時間
        long start = System.currentTimeMillis();
        redisTemplate.executePipelined(new SessionCallback() {
            //執行流水線
            public Object execute(RedisOperations operations) throws DataAccessException {
                //批量處理的內容
                for (int i = 0; i < 20000; i++) {
                    operations.opsForValue().set("k"+i,"v"+i);
                }
                return null;
            }
        });
        // 結束時間
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }
}

  //耗時:309;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM