JedisPool单节点及Cluster通道单条/批量插入,读

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32447321/article/details/74452522

@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


展开阅读全文

没有更多推荐了,返回首页