1. 程式人生 > >JedisPool單節點及Cluster通道單條/批量插入,讀

JedisPool單節點及Cluster通道單條/批量插入,讀

@Autowired
private JedisPool jedisPool;

/**

*一、 單節點通道批量插入
*/
   Jedis jedis = jedisPool.getResource();
 Pipeline jcp = jedis.pipelined();

   Map<String, String> map = new HashMap<String, String>();
   for (int i = 0; i < list.size(); i++) {
   String key = RedisKeysDefine.RT_HASH_KEY + infols.get(i).getConnectorID();
   map.put(RedisKeysDefine.RT_HASH_COLUMN_STATION_ID, stationId);
   map.put( RedisKeysDefine.RT_HASH_COLUMN_STATUS, infols.get(i).getStatus()+"");
   map.put(RedisKeysDefine.RT_HASH_COLUMN_PARKSTATUS, infols.get(i).getParkStatus() + "");
   map.put(RedisKeysDefine.RT_HASH_COLUMN_LOCKSTATUS, infols.get(i).getLockStatus() + "");
   map.put(RedisKeysDefine.RT_HASH_COLUMN_DATATIME, dataTime + "");
   jcp.hmset(key, map);

}
   jcp.sync();

   jedis.close();

-------------------------------------------------------------------------------------------------

二、單節點redis快取最新一條資料:

Jedis jedis = jedisPool.getResource();
        String key = RedisKeysDefine.RT_HASH_KEY + info.getConnectorID();
        if(jedis.hexists(key, RedisKeysDefine.RT_HASH_COLUMN_DATATIME)){
        /**
        * 判斷當前資料是否比快取更新
        */
        Long lastDataTime = Long.parseLong(jedis.hget(key, RedisKeysDefine.RT_HASH_COLUMN_DATATIME));
        if(dataTime<lastDataTime) {
        jedis.close();


        return 1;}

        }
        jedis.hset(key, RedisKeysDefine.RT_HASH_COLUMN_STATION_ID, stationId);
        jedis.hset(key, RedisKeysDefine.RT_HASH_COLUMN_STATUS, info.getStatus() + "");
        jedis.hset(key, RedisKeysDefine.RT_HASH_COLUMN_PARKSTATUS, info.getParkStatus() + "");
        jedis.hset(key, RedisKeysDefine.RT_HASH_COLUMN_LOCKSTATUS, info.getLockStatus() + "");
        jedis.hset(key, RedisKeysDefine.RT_HASH_COLUMN_DATATIME, dataTime + "");
        jedis.close();

======================在叢集Cluster模式下===============================

@Autowired
private JedisCluster jedisCluster;//在叢集Cluster模式下利用通道批量查詢

List<StakeRealInfo> list ;//獲取存入Cluster的key

JedisClusterPipeline jcp = JedisClusterPipeline.pipelined(jedisCluster);
jcp.refreshCluster();
//重新整理叢集資訊,當叢集資訊發生變更時呼叫

List<Object> batchResult = null;
batchResult = jcp.syncAndReturnAll();//同步讀取所有資料 並按命令順序返回一個列表

jcp.close();

參考的文章:http://lxw1234.com/archives/2015/05/224.htm