Java Code Examples for redis.clients.jedis.Jedis#hdel()

The following examples show how to use redis.clients.jedis.Jedis#hdel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JedisSessionDAO.java    From easyweb with Apache License 2.0 6 votes vote down vote up
@Override
public void delete(Session session) {
	if (session == null || session.getId() == null) {
		return;
	}
	
	Jedis jedis = null;
	try {
		jedis = JedisUtils.getResource();
		
		jedis.hdel(JedisUtils.getBytesKey(sessionKeyPrefix), JedisUtils.getBytesKey(session.getId().toString()));
		jedis.del(JedisUtils.getBytesKey(sessionKeyPrefix + session.getId()));

		logger.debug("delete {} ", session.getId());
	} catch (Exception e) {
		logger.error("delete {} ", session.getId(), e);
	} finally {
		JedisUtils.returnResource(jedis);
	}
}
 
Example 2
Source File: JedisForbiddenQueueStore.java    From vscrawler with Apache License 2.0 6 votes vote down vote up
@Override
public ResourceItem remove(String queueID, String key) {
    if (!lockQueue(queueID)) {
        throw new RuntimeException("failed to acquire redis lock");
    }
    Jedis jedis = jedisPool.getResource();
    try {
        String data = jedis.hget(makeDataKey(queueID), key);
        if (StringUtils.isBlank(data)) {
            return null;
        }
        jedis.hdel(makeDataKey(queueID), key);
        return JSONObject.toJavaObject(JSONObject.parseObject(data), ResourceItem.class);
    } finally {
        IOUtils.closeQuietly(jedis);
        unLockQueue(queueID);
    }
}
 
Example 3
Source File: JedisSessionDAO.java    From NutzSite with Apache License 2.0 6 votes vote down vote up
@Override
public void delete(Session session) {
    if (session == null || session.getId() == null) {
        return;
    }

    Jedis jedis = null;
    try {
        jedis = jedisAgent.getResource();

        jedis.hdel(JedisUtils.getBytesKey(sessionKeyPrefix), JedisUtils.getBytesKey(session.getId().toString()));
        jedis.del(JedisUtils.getBytesKey(sessionKeyPrefix + session.getId()));

        logger.debug("delete {} ", session.getId());
    } catch (Exception e) {
        logger.error("delete {} ", session.getId(), e);
    } finally {
       Streams.safeClose(jedis);
    }
}
 
Example 4
Source File: RedisClient.java    From apollo with GNU General Public License v2.0 6 votes vote down vote up
public boolean hdel(String key, String field) throws Exception {
    Jedis jedis = null;
    try {
        jedis = this.jedisPool.getResource();
        long value = jedis.hdel(SafeEncoder.encode(key), SafeEncoder.encode(field));
        logger.info("hget key:" + key + ", field:" + field);

        return value == 1;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        this.jedisPool.returnBrokenResource(jedis);
        throw e;
    } finally {
        if (jedis != null) {
            this.jedisPool.returnResource(jedis);
        }
    }
}
 
Example 5
Source File: RedisLockCoordinator.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
private void checkActiveNode(){
	Jedis redisClient = getRedisClient();
	try {
		long currentTime = System.currentTimeMillis();
		Map<String, String> map = redisClient.hgetAll(REDIS_LOCK_ACTIVE_NODES_KEY);
		Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
		while(iterator.hasNext()){
			Entry<String, String> entry = iterator.next();
			if(currentTime - Long.parseLong(entry.getValue()) > HEALTH_CHECK_PERIOD){
				redisClient.hdel(REDIS_LOCK_ACTIVE_NODES_KEY, entry.getKey());
			}else{
				redisClient.hset(REDIS_LOCK_ACTIVE_NODES_KEY, entry.getKey(), String.valueOf(currentTime));
			}
		}
		if(!map.containsKey(EVENT_NODE_ID)){
			redisClient.hset(REDIS_LOCK_ACTIVE_NODES_KEY, EVENT_NODE_ID, String.valueOf(currentTime));
		}
	} finally {
		release(redisClient);
	}
}
 
Example 6
Source File: WispKvStoreCodisKvImpl.java    From wisp with Apache License 2.0 6 votes vote down vote up
@Override
public void delete(String tableId, String key) throws WispProcessorException {
    Jedis jedis = null;
    try {

        LOGGER.debug("jedis get: {} {}", tableId, key);

        jedis = jedisPool.getResource();

        String hKey = redisPrefix + tableId;
        jedis.hdel(hKey, key);

    } catch (Exception e) {

        throw new WispProcessorException(e);
    } finally {

        if (jedis != null) {
            jedis.close();
        }
    }
}
 
Example 7
Source File: JedisCacheManager.java    From easyweb with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public V remove(K key) throws CacheException {
	V value = null;
	Jedis jedis = null;
	try {
		jedis = JedisUtils.getResource();
		value = (V)JedisUtils.toObject(jedis.hget(JedisUtils.getBytesKey(cacheKeyName), JedisUtils.getBytesKey(key)));
		jedis.hdel(JedisUtils.getBytesKey(cacheKeyName), JedisUtils.getBytesKey(key));
		logger.debug("remove {} {}", cacheKeyName, key);
	} catch (Exception e) {
		logger.warn("remove {} {}", cacheKeyName, key, e);
	} finally {
		JedisUtils.returnResource(jedis);
	}
	return value;
}
 
Example 8
Source File: JedisBlockingQueueStore.java    From vscrawler with Apache License 2.0 6 votes vote down vote up
@Override
public ResourceItem remove(String queueID, String key) {
    lockQueue(queueID);
    Jedis jedis = jedisPool.getResource();
    try {
        String data = jedis.hget(makeDataKey(queueID), key);
        if (StringUtils.isBlank(data)) {
            VSCrawlerMonitor.recordOne(queueID + "_find_meta_data_failed");
            return null;
        }
        ResourceItem ret = JSONObject.toJavaObject(JSON.parseObject(data), ResourceItem.class);
        jedis.hdel(makePoolQueueKey(queueID), key);
        jedis.zrem(makePoolQueueKey(queueID), key);
        return ret;
    } finally {
        IOUtils.closeQuietly(jedis);
        unLockQueue(queueID);
    }
}
 
Example 9
Source File: JedisCacheManager.java    From easyweb with Apache License 2.0 5 votes vote down vote up
@Override
public void clear() throws CacheException {
	Jedis jedis = null;
	try {
		jedis = JedisUtils.getResource();
		jedis.hdel(JedisUtils.getBytesKey(cacheKeyName));
		logger.debug("clear {}", cacheKeyName);
	} catch (Exception e) {
		logger.error("clear {}", cacheKeyName, e);
	} finally {
		JedisUtils.returnResource(jedis);
	}
}
 
Example 10
Source File: RedisRegistry.java    From light-task-scheduler with Apache License 2.0 5 votes vote down vote up
private void clean(Jedis jedis) {
    // /LTS/{集群名字}/NODES/
    Set<String> nodeTypePaths = jedis.keys(NodeRegistryUtils.getRootPath(appContext.getConfig().getClusterName()) + "/*");
    if (CollectionUtils.isNotEmpty(nodeTypePaths)) {
        for (String nodeTypePath : nodeTypePaths) {
            // /LTS/{集群名字}/NODES/JOB_TRACKER
            Set<String> nodePaths = jedis.keys(nodeTypePath);
            if (CollectionUtils.isNotEmpty(nodePaths)) {
                for (String nodePath : nodePaths) {
                    Map<String, String> nodes = jedis.hgetAll(nodePath);
                    if (CollectionUtils.isNotEmpty(nodes)) {
                        boolean delete = false;
                        long now = SystemClock.now();
                        for (Map.Entry<String, String> entry : nodes.entrySet()) {
                            String key = entry.getKey();
                            long expire = Long.parseLong(entry.getValue());
                            if (expire < now) {
                                jedis.hdel(nodePath, key);
                                delete = true;
                                if (LOGGER.isWarnEnabled()) {
                                    LOGGER.warn("Delete expired key: " + nodePath + " -> value: " + entry.getKey() + ", expire: " + new Date(expire) + ", now: " + new Date(now));
                                }
                            }
                        }
                        if (delete) {
                            jedis.publish(nodePath, Constants.UNREGISTER);
                        }
                    }
                }
            }
        }
    }
}
 
Example 11
Source File: RedisClient.java    From flycache with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * 通过key 删除指定的 field
 * </p>
 *
 * @param key
 * @param fields 可以是 一个 field 也可以是 一个数组
 * @return
 */
public Long hdel(String key, String... fields) {
    Jedis jedis = null;
    Long res = null;
    try {
        jedis = jedisPool.getResource();
        res = jedis.hdel(key, fields);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        closeJedis(jedis);
    }
    return res;
}
 
Example 12
Source File: JedisUtils.java    From Shop-for-JavaWeb with MIT License 5 votes vote down vote up
/**
 * 移除Map缓存中的值
 * @param key 键
 * @param value 值
 * @return
 */
public static long mapRemove(String key, String mapKey) {
	long result = 0;
	Jedis jedis = null;
	try {
		jedis = getResource();
		result = jedis.hdel(key, mapKey);
		logger.debug("mapRemove {}  {}", key, mapKey);
	} catch (Exception e) {
		logger.warn("mapRemove {}  {}", key, mapKey, e);
	} finally {
		returnResource(jedis);
	}
	return result;
}
 
Example 13
Source File: JedisClientPool.java    From blog-sample with Apache License 2.0 5 votes vote down vote up
@Override
public Long hdel(String key, String... field) {
    Jedis jedis = jedisPool.getResource();
    Long result = jedis.hdel(key, field);
    jedis.close();
    return result;
}
 
Example 14
Source File: RedisRegistry.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void clean(Jedis jedis) {
    Set<String> keys = jedis.keys(root + Constants.ANY_VALUE);
    if (keys != null && keys.size() > 0) {
        for (String key : keys) {
            Map<String, String> values = jedis.hgetAll(key);
            if (values != null && values.size() > 0) {
                boolean delete = false;
                long now = System.currentTimeMillis();
                for (Map.Entry<String, String> entry : values.entrySet()) {
                    URL url = URL.valueOf(entry.getKey());
                    if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
                        long expire = Long.parseLong(entry.getValue());
                        if (expire < now) {
                            jedis.hdel(key, entry.getKey());
                            delete = true;
                            if (logger.isWarnEnabled()) {
                                logger.warn("Delete expired key: " + key + " -> value: " + entry.getKey() + ", expire: " + new Date(expire) + ", now: " + new Date(now));
                            }
                        }
                    }
                }
                if (delete) {
                    jedis.publish(key, Constants.UNREGISTER);
                }
            }
        }
    }
}
 
Example 15
Source File: JedisCacheManager.java    From Shop-for-JavaWeb with MIT License 5 votes vote down vote up
@Override
public void clear() throws CacheException {
	Jedis jedis = null;
	try {
		jedis = JedisUtils.getResource();
		jedis.hdel(JedisUtils.getBytesKey(cacheKeyName));
		logger.debug("clear {}", cacheKeyName);
	} catch (Exception e) {
		logger.error("clear {}", cacheKeyName, e);
	} finally {
		JedisUtils.returnResource(jedis);
	}
}
 
Example 16
Source File: JedisUtil.java    From scaffold-cloud with MIT License 5 votes vote down vote up
/**
 * 移除Map缓存中的值
 *
 * @param key    键
 * @param mapKey
 * @return
 */
public static long mapObjectRemove(String key, String mapKey) {
    long result = 0;
    Jedis jedis = null;
    try {
        jedis = getResource();
        result = jedis.hdel(getBytesKey(key), getBytesKey(mapKey));
    } catch (Exception e) {
        logger.warn("mapObjectRemove {}  {}", key, mapKey, e);
    } finally {
        close(jedis);
    }
    return result;
}
 
Example 17
Source File: RedisCacheProvider.java    From xiaoyaoji with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void remove(String table, String key) {
    Jedis jedis = null;
    try {
        jedis = pool.getResource();
        jedis.hdel(table,key);
    } finally {
        if (jedis != null)
            jedis.close();
    }
}
 
Example 18
Source File: RedisClient.java    From star-zone with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * 通过key 删除指定的 field
 * </p>
 * 
 * @param key
 * @param fields
 *            可以是 一个 field 也可以是 一个数组
 * @return
 */
public Long hdel(String key, String... fields) {
	Jedis jedis = null;
	Long res = null;
	try {
		jedis = jedisPool.getResource();
		res = jedis.hdel(key, fields);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		closeJedis(jedis);
	}
	return res;
}
 
Example 19
Source File: JedisClientPool.java    From BigDataPlatform with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Long hdel(String key, String... field) {
	Jedis jedis = jedisPool.getResource();
	Long result = jedis.hdel(key, field);
	jedis.close();
	return result;
}
 
Example 20
Source File: JedisSegmentScoredQueueStore.java    From vscrawler with Apache License 2.0 4 votes vote down vote up
@Override
public ResourceItem pop(String queueID) {
    if (!lockQueue(queueID)) {
        return null;
    }
    @Cleanup Jedis jedis = jedisPool.getResource();
    try {
        String sliceQueueKey = makeSliceQueueKey(queueID);
        String firstResourceKey;
        String firstSliceID = null;
        while (true) {
            String sliceID = jedis.lpop(sliceQueueKey);
            if (isNil(sliceID)) {
                // empty queue
                return null;
            }
            if (firstSliceID == null) {
                firstSliceID = sliceID;
            } else if (firstSliceID.equals(sliceID)) {
                // 这个条件下,数据紊乱了?
                jedis.lpush(sliceQueueKey, firstSliceID);
                return null;
            }
            firstResourceKey = jedis.lpop(makePoolQueueKey(queueID, sliceID));
            if (!isNil(firstResourceKey)) {
                jedis.lpush(sliceQueueKey, sliceID);
                break;
            }
            jedis.rpush(sliceQueueKey, sliceID);
        }

        String dataJson = jedis.hget(makeDataKey(queueID), firstResourceKey);
        if (isNil(dataJson)) {
            throw new IllegalStateException(
                    "this is no meta data for key queue :" + queueID + " ,for resourceKey :" + firstResourceKey);
        }
        jedis.hdel(makeDataKey(queueID), firstResourceKey);
        return JSONObject.toJavaObject(JSON.parseObject(dataJson), ResourceItem.class);
    } finally {
        unLockQueue(queueID);
    }

}