redis muti get筆記


RedisTemplate:通過RedisTemplate獲取字節數組(byte[])

原文:https://www.codeleading.com/article/27755177535/

 

public byte[] get(byte[] key) {
    // 使用了lambda表達式
    return redisTemplate.execute((RedisConnection redisConnection) -> redisConnection.get(key));
}

 

public byte[] get(byte[] key) {
    return redisTemplate.execute(new RedisCallback<byte[]>() {
        @Override
        public byte[] doInRedis(RedisConnection redisConnection) throws DataAccessException {
            // 傳入byte[]類型的key,獲取byte[]類型的value
            byte[] bytes = redisConnection.get(key);
            return bytes;
        }
    });
}

 

muti-get

    @Autowired
    private StringRedisTemplate redisTemplate;
  
    public List<String> mget(List<String> keys) {
        return redisTemplate.opsForValue().multiGet(keys);
    }

 

connection /callback的 muti-get

List<byte[]> bytesList = redisTemplate.execute((RedisConnection conn) -> conn.mGet(realKeys.toArray(rawKeys)));

 

還有一個pipeline方式,可以單獨處理沒有命中時的結果,速度介於mget 和  batch-loop-get。

原文:Redis 批量查詢優化 https://www.jianshu.com/p/a2e9f03cd34c

   /**
     * 批量查詢 pipeline
     * @param keys
     * @return
     */
    @GetMapping("/batchGet")
    public List<Object> batchGet(String... keys){
        List<String> keysList = Arrays.asList(keys);
        return redisOperator.batchGet(keysList);
    }
@Component
public class RedisOperator {


    @Autowired
    private StringRedisTemplate redisTemplate;
  
     /**
     * 批量查詢 管道 pipeline
     *
     * @param keys
     * @return value
     */
    public List<Object> batchGet(List<String> keys) {
        // nginx -> keepalive
        // redis -> pipeline
        List<Object> result = redisTemplate.executePipelined(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                StringRedisConnection src = (StringRedisConnection) connection;
                for (String key : keys) {
                    src.get(key);
                }
                return null;
            }
        });
        return result;
    }
  
}

 

 

Redis 批量查詢優化


免責聲明!

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



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