執行一個Redis命令,Redis客戶端和Redis伺服器就需要執行以下步驟:
客戶端發送命令到伺服器;
伺服器接受命令請求,執行命令,產生相應的結果;
public class RedisPipelineStudy extends BaseTest { @Autowired private StringRedisTemplate stringRedisTemplate; private static final String PREFIX = "test0:"; @Test public void test() { StopWatch stopWatch = new StopWatch(); stopWatch.start("test0"); for (int times = 0; times #使用單一的increment 指令,處理200w個key:<p></p><pre class="brush:php;toolbar:false">public class RedisPipelineStudy extends BaseTest { @Autowired private StringRedisTemplate stringRedisTemplate; private static final String PREFIX = "test1:"; @Test public void test() { StopWatch stopWatch = new StopWatch(); stopWatch.start("test1"); List<integer> recordList = new ArrayList(); for (int times = 0; times 300) { incrByPipeline(recordList); recordList = new ArrayList(); } } catch (Exception e) { System.out.println(e); } } if (!CollectionUtils.isEmpty(recordList)) { incrByPipeline(recordList); recordList = new ArrayList(); } } stopWatch.stop(); System.out.println(stopWatch.prettyPrint()); } private void incrByPipeline(List<integer> recordList) { stringRedisTemplate.executePipelined(new RedisCallback<object>() { @Override public Object doInRedis(RedisConnection connection) throws DataAccessException { try { for (Integer record : recordList) { byte[] key = (PREFIX + record).getBytes(); connection.incrBy(key, 1); } } catch (Exception e) { System.out.println(e); } return null; } }); } }</object></integer></integer>
rrreee
耗用時間: 11 位,單位:ns,是單一指令耗時的1/6。
以上是SpringBoot中如何透過整合Redis來實現管道的詳細內容。更多資訊請關注PHP中文網其他相關文章!