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

The following examples show how to use redis.clients.jedis.Jedis#exists() . 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: CommonServiceDaoImpl.java    From MicroCommunity with Apache License 2.0 6 votes vote down vote up
/**
 * 根据域查询对应的映射关系
 *
 * @param codeMapping
 * @return
 */

@Override
@Cacheable(key= "CodeMappingByDomain")
public List<CodeMapping> getCodeMappingByDomain(CodeMapping codeMapping)  throws Exception{

    Jedis jedis = this.getJedis();
    List<CodeMapping> codeMappings = null;
    if(jedis.exists("CodeMappingByDomain".getBytes())){
        codeMappings = SerializeUtil.unserializeList(jedis.get("CodeMappingByDomain".getBytes()),CodeMapping.class);
    }else{
        codeMappings = sqlSessionTemplate.selectList("commonServiceDaoImpl.getCodeMappingByDomain", codeMapping);

        jedis.set("CodeMappingByDomain".getBytes(),SerializeUtil.serializeList(codeMappings));
    }
    return codeMappings;
}
 
Example 2
Source File: CommonServiceDaoImpl.java    From MicroCommunity with Apache License 2.0 6 votes vote down vote up
/**
 * 查询所有有效的映射数据
 *
 * @return
 */
@Override
@Cacheable(key= "CodeMappingAll")
public List<CodeMapping> getCodeMappingAll() throws Exception{
   Jedis jedis = this.getJedis();

   List<CodeMapping> codeMappings = null;
   if(jedis.exists("CodeMappingAll".getBytes())){
       codeMappings = SerializeUtil.unserializeList(jedis.get("CodeMappingAll".getBytes()),CodeMapping.class);
   }else{
       codeMappings = sqlSessionTemplate.selectList("commonServiceDaoImpl.getCodeMappingAll");

       jedis.set("CodeMappingAll".getBytes(),SerializeUtil.serializeList(codeMappings));
   }
    return codeMappings;
}
 
Example 3
Source File: JedisUtils.java    From easyweb with Apache License 2.0 6 votes vote down vote up
/**
 * 获取List缓存
 * @param key 键
 * @return 值
 */
public static List<String> getList(String key) {
	List<String> value = null;
	Jedis jedis = null;
	try {
		jedis = getResource();
		if (jedis.exists(key)) {
			value = jedis.lrange(key, 0, -1);
			logger.debug("getList {} = {}", key, value);
		}
	} catch (Exception e) {
		logger.warn("getList {} = {}", key, value, e);
	} finally {
		returnResource(jedis);
	}
	return value;
}
 
Example 4
Source File: JedisUtil.java    From scaffold-cloud with MIT License 6 votes vote down vote up
/**
 * 设置Map缓存
 *
 * @param key          键
 * @param value        值
 * @param cacheSeconds 超时时间,0为不超时
 * @return
 */
public static String setMap(String key, Map<String, String> value, int cacheSeconds) {
    String result = null;
    Jedis jedis = null;
    try {
        jedis = getResource();
        if (jedis.exists(key)) {
            jedis.del(key);
        }
        result = jedis.hmset(key, value);
        if (cacheSeconds != 0) {
            jedis.expire(key, cacheSeconds);
        }
    } catch (Exception e) {
        logger.warn("setMap {} = {}", key, value, e);
    } finally {
        close(jedis);
    }
    return result;
}
 
Example 5
Source File: RedisClientImpl.java    From khan-session with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public <T> void put(String key, T value, long secondsToExpire)
        throws IOException {
    Jedis jedis = pool.getResource();
    try {
        jedis.select(redisServer.getDatabase());

        if (jedis.exists(key)) {
            jedis.set(key.getBytes(), marshaller.objectToBytes(value), "XX".getBytes(), "EX".getBytes(), secondsToExpire);
        } else {
            jedis.set(key.getBytes(), marshaller.objectToBytes(value), "NX".getBytes(), "EX".getBytes(), secondsToExpire);
        }
    } finally {
        pool.returnResource(jedis);
    }
    //cache.put(key, value, secondsToExpire, TimeUnit.SECONDS);
    //logger.debug("@@@@@@@@@@@@@ cache.size=" + cache.size());
}
 
Example 6
Source File: JedisUtils.java    From easyweb with Apache License 2.0 6 votes vote down vote up
/**
 * 设置List缓存
 * @param key 键
 * @param value 值
 * @param cacheSeconds 超时时间,0为不超时
 * @return
 */
public static long setList(String key, List<String> value, int cacheSeconds) {
	long result = 0;
	Jedis jedis = null;
	try {
		jedis = getResource();
		if (jedis.exists(key)) {
			jedis.del(key);
		}
		result = jedis.rpush(key, (String[])value.toArray());
		if (cacheSeconds != 0) {
			jedis.expire(key, cacheSeconds);
		}
		logger.debug("setList {} = {}", key, value);
	} catch (Exception e) {
		logger.warn("setList {} = {}", key, value, e);
	} finally {
		returnResource(jedis);
	}
	return result;
}
 
Example 7
Source File: JedisUtils.java    From Shop-for-JavaWeb with MIT License 6 votes vote down vote up
/**
 * 获取缓存
 * @param key 键
 * @return 值
 */
public static Set<Object> getObjectSet(String key) {
	Set<Object> value = null;
	Jedis jedis = null;
	try {
		jedis = getResource();
		if (jedis.exists(getBytesKey(key))) {
			value = Sets.newHashSet();
			Set<byte[]> set = jedis.smembers(getBytesKey(key));
			for (byte[] bs : set){
				value.add(toObject(bs));
			}
			logger.debug("getObjectSet {} = {}", key, value);
		}
	} catch (Exception e) {
		logger.warn("getObjectSet {} = {}", key, value, e);
	} finally {
		returnResource(jedis);
	}
	return value;
}
 
Example 8
Source File: JedisUtils.java    From Shop-for-JavaWeb with MIT License 6 votes vote down vote up
/**
 * 设置List缓存
 * @param key 键
 * @param value 值
 * @param cacheSeconds 超时时间,0为不超时
 * @return
 */
public static long setList(String key, List<String> value, int cacheSeconds) {
	long result = 0;
	Jedis jedis = null;
	try {
		jedis = getResource();
		if (jedis.exists(key)) {
			jedis.del(key);
		}
		result = jedis.rpush(key, (String[])value.toArray());
		if (cacheSeconds != 0) {
			jedis.expire(key, cacheSeconds);
		}
		logger.debug("setList {} = {}", key, value);
	} catch (Exception e) {
		logger.warn("setList {} = {}", key, value, e);
	} finally {
		returnResource(jedis);
	}
	return result;
}
 
Example 9
Source File: JedisUtil.java    From scaffold-cloud with MIT License 6 votes vote down vote up
/**
 * 设置List缓存
 *
 * @param <T>
 * @param key          键
 * @param value        值
 * @param cacheSeconds 超时时间,0为不超时
 * @return
 */
public static <T> long setObjectList(String key, List<T> value, int cacheSeconds) {
    long result = 0;
    Jedis jedis = null;
    try {
        jedis = getResource();
        if (jedis.exists(getBytesKey(key))) {
            jedis.del(key);
        }
        for (Object o : value) {
            result += jedis.rpush(getBytesKey(key), toBytes(o));
        }
        if (cacheSeconds != 0) {
            jedis.expire(key, cacheSeconds);
        }
    } catch (Exception e) {
        logger.warn("setObjectList {} = {}", key, value, e);
    } finally {
        close(jedis);
    }
    return result;
}
 
Example 10
Source File: JedisUtils.java    From easyweb with Apache License 2.0 6 votes vote down vote up
/**
 * 获取缓存
 * @param key 键
 * @return 值
 */
public static Set<Object> getObjectSet(String key) {
	Set<Object> value = null;
	Jedis jedis = null;
	try {
		jedis = getResource();
		if (jedis.exists(getBytesKey(key))) {
			value = Sets.newHashSet();
			Set<byte[]> set = jedis.smembers(getBytesKey(key));
			for (byte[] bs : set){
				value.add(toObject(bs));
			}
			logger.debug("getObjectSet {} = {}", key, value);
		}
	} catch (Exception e) {
		logger.warn("getObjectSet {} = {}", key, value, e);
	} finally {
		returnResource(jedis);
	}
	return value;
}
 
Example 11
Source File: RedisClient.java    From springboot-learn with MIT License 6 votes vote down vote up
/**
 * 读取缓存,若不存在则设置读取
 *
 * @param key
 * @param initValue
 * @return
 */
public void setOrElse(final String key, final String initValue) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();

        if (jedis.exists(key)) {
            logger.info("{}={}已存在", key, jedis.get(key));
        } else {
            jedis.set(key, initValue);
            logger.info("{}不存在,已成功设置初始值{}", key, jedis.get(key));
        }
    } catch (Exception e) {
        logger.error("[RedisClient] setOrElse e,", e);
    } finally {
        close(jedis);
    }
}
 
Example 12
Source File: RedisClient.java    From springboot-learn with MIT License 6 votes vote down vote up
/**
 * 判断缓存中是否有对应的value
 *
 * @param keys
 * @return
 */
public List<String> exists(String... keys) {
    Jedis jedis = null;
    try {
        List<String> containKeys = Lists.newArrayList();
        jedis = getJedis();
        for (String key : keys) {
            if (jedis.exists(key)) {
                containKeys.add(key);
            }
        }
        return containKeys;
    } catch (Exception e) {
        logger.error("[RedisClient] exists e,", e);
        return Collections.emptyList();
    } finally {
        close(jedis);
    }
}
 
Example 13
Source File: JedisUtil.java    From scaffold-cloud with MIT License 6 votes vote down vote up
/**
 * 获取缓存
 *
 * @param key 键
 * @return 值
 */
public static String get(String key) {
    String value = null;
    Jedis jedis = null;
    try {
        jedis = getResource();
        if (jedis.exists(key)) {
            value = jedis.get(key);
            value = StrUtil.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null;
        }
    } catch (Exception e) {
        logger.warn("get {} = {}", key, value, e);
    } finally {
        close(jedis);
    }
    return value;
}
 
Example 14
Source File: JedisUtils.java    From Shop-for-JavaWeb with MIT License 6 votes vote down vote up
/**
 * 设置List缓存
 * @param key 键
 * @param value 值
 * @param cacheSeconds 超时时间,0为不超时
 * @return
 */
public static long setObjectList(String key, List<Object> value, int cacheSeconds) {
	long result = 0;
	Jedis jedis = null;
	try {
		jedis = getResource();
		if (jedis.exists(getBytesKey(key))) {
			jedis.del(key);
		}
		List<byte[]> list = Lists.newArrayList();
		for (Object o : value){
			list.add(toBytes(o));
		}
		result = jedis.rpush(getBytesKey(key), (byte[][])list.toArray());
		if (cacheSeconds != 0) {
			jedis.expire(key, cacheSeconds);
		}
		logger.debug("setObjectList {} = {}", key, value);
	} catch (Exception e) {
		logger.warn("setObjectList {} = {}", key, value, e);
	} finally {
		returnResource(jedis);
	}
	return result;
}
 
Example 15
Source File: JedisUtils.java    From easyweb with Apache License 2.0 5 votes vote down vote up
/**
 * 缓存是否存在
 * @param key 键
 * @return
 */
public static boolean exists(String key) {
	boolean result = false;
	Jedis jedis = null;
	try {
		jedis = getResource();
		result = jedis.exists(key);
		logger.debug("exists {}", key);
	} catch (Exception e) {
		logger.warn("exists {}", key, e);
	} finally {
		returnResource(jedis);
	}
	return result;
}
 
Example 16
Source File: JedisClientPool.java    From BigDataPlatform with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Boolean exists(String key) {
	Jedis jedis = jedisPool.getResource();
	Boolean result = jedis.exists(key);
	jedis.close();
	return result;
}
 
Example 17
Source File: RedisJobStore.java    From redis-quartz with MIT License 5 votes vote down vote up
/**
 * Retrieves trigger from redis.
 *
 * @param triggerKey the trigger key
 * @param jedis thread-safe redis connection
 * @return the operable trigger
 * @throws JobPersistenceException
 */
private OperableTrigger retrieveTrigger(TriggerKey triggerKey, Jedis jedis) throws JobPersistenceException {
	String triggerHashKey = createTriggerHashKey(triggerKey.getGroup(), triggerKey.getName());
	Map<String, String> trigger = jedis.hgetAll(triggerHashKey);
	if (!jedis.exists(triggerHashKey)) {
		log.debug("trigger does not exist for key: " + triggerHashKey);
		return null;
	}			
	
     if (!jedis.exists(trigger.get(JOB_HASH_KEY))) {
        log.warn("job: " + trigger.get(JOB_HASH_KEY) + " does not exist for trigger: " + triggerHashKey);
        return null;
     }
	return toOperableTrigger(triggerKey, trigger);
}
 
Example 18
Source File: RedisService.java    From SecKillShop with MIT License 5 votes vote down vote up
/**
 * 判断当前键值对是否存在
 * @param prefix
 * @param key
 * @return
 */
public boolean exist(KeyPrefix prefix, String key) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        String realKey = prefix.getPrefix() + key;

        return jedis.exists(realKey);

    } finally {
        returnToPool(jedis);
    }

}
 
Example 19
Source File: RedisStorage.java    From quartz-redis-jobstore with Apache License 2.0 4 votes vote down vote up
/**
 * Store a trigger in redis
 * @param trigger the trigger to be stored
 * @param replaceExisting true if an existing trigger with the same identity should be replaced
 * @param jedis a thread-safe Redis connection
 * @throws JobPersistenceException
 * @throws ObjectAlreadyExistsException
 */
@Override
public void storeTrigger(OperableTrigger trigger, boolean replaceExisting, Jedis jedis) throws JobPersistenceException {
    final String triggerHashKey = redisSchema.triggerHashKey(trigger.getKey());
    final String triggerGroupSetKey = redisSchema.triggerGroupSetKey(trigger.getKey());
    final String jobTriggerSetKey = redisSchema.jobTriggersSetKey(trigger.getJobKey());

    if(!(trigger instanceof SimpleTrigger) && !(trigger instanceof CronTrigger)){
        throw new UnsupportedOperationException("Only SimpleTrigger and CronTrigger are supported.");
    }
    final boolean exists = jedis.exists(triggerHashKey);
    if(exists && !replaceExisting){
        throw new ObjectAlreadyExistsException(trigger);
    }

    Map<String, String> triggerMap = mapper.convertValue(trigger, new TypeReference<HashMap<String, String>>() {});
    triggerMap.put(TRIGGER_CLASS, trigger.getClass().getName());

    Pipeline pipe = jedis.pipelined();
    pipe.hmset(triggerHashKey, triggerMap);
    pipe.sadd(redisSchema.triggersSet(), triggerHashKey);
    pipe.sadd(redisSchema.triggerGroupsSet(), triggerGroupSetKey);
    pipe.sadd(triggerGroupSetKey, triggerHashKey);
    pipe.sadd(jobTriggerSetKey, triggerHashKey);
    if(trigger.getCalendarName() != null && !trigger.getCalendarName().isEmpty()){
        final String calendarTriggersSetKey = redisSchema.calendarTriggersSetKey(trigger.getCalendarName());
        pipe.sadd(calendarTriggersSetKey, triggerHashKey);
    }
    if (trigger.getJobDataMap() != null && !trigger.getJobDataMap().isEmpty()) {
        final String triggerDataMapHashKey = redisSchema.triggerDataMapHashKey(trigger.getKey());
        pipe.hmset(triggerDataMapHashKey, getStringDataMap(trigger.getJobDataMap()));
    }
    pipe.sync();

    if(exists){
        // We're overwriting a previously stored instance of this trigger, so clear any existing trigger state.
        unsetTriggerState(triggerHashKey, jedis);
    }

    pipe = jedis.pipelined();
    Response<Boolean> triggerPausedResponse = pipe.sismember(redisSchema.pausedTriggerGroupsSet(), triggerGroupSetKey);
    Response<Boolean> jobPausedResponse = pipe.sismember(redisSchema.pausedJobGroupsSet(), redisSchema.jobGroupSetKey(trigger.getJobKey()));
    pipe.sync();
    final String jobHashKey = redisSchema.jobHashKey(trigger.getJobKey());
    final long nextFireTime = trigger.getNextFireTime() != null ? trigger.getNextFireTime().getTime() : -1;
    if (triggerPausedResponse.get() || jobPausedResponse.get()){
        if (isBlockedJob(jobHashKey, jedis)) {
            setTriggerState(RedisTriggerState.PAUSED_BLOCKED, (double) nextFireTime, triggerHashKey, jedis);
        } else {
            setTriggerState(RedisTriggerState.PAUSED, (double) nextFireTime, triggerHashKey, jedis);
        }
    } else if(trigger.getNextFireTime() != null){
        if (isBlockedJob(jobHashKey, jedis)) {
            setTriggerState(RedisTriggerState.BLOCKED, nextFireTime, triggerHashKey, jedis);
        } else {
            setTriggerState(RedisTriggerState.WAITING, (double) trigger.getNextFireTime().getTime(), triggerHashKey, jedis);
        }
    }
}
 
Example 20
Source File: RuleDaoImpl.java    From MicroCommunity with Apache License 2.0 4 votes vote down vote up
/**
 * 初始化业务规则分组和编码映射关联关系信息
 * @return
 * @throws Exception
 */
@Override
@Cacheable(key=RuleDomain.REDIS_KEY_RULE_GROUP)
public List getRuleGroupRelaList() throws Exception {
    List saopRuleGroupRelaList = new ArrayList();

    Jedis jedis = getJedis();

    if(jedis.exists(RuleDomain.REDIS_KEY_RULE_ENTRANCE.getBytes())){
        saopRuleGroupRelaList =  SerializeUtil.unserializeList(jedis.get(RuleDomain.REDIS_KEY_RULE_GROUP.getBytes()),Map.class);
    }else {
        //查询全部业务规则分组信息
        List allSaopRuleGroupInfoList = sqlSessionTemplate.selectList("ruleDaoImpl.querySaopRuleGroupMap");

        //业务分组和编码映射关系集合
        List allsaopRuleGroupRelaInfoList = sqlSessionTemplate.selectList("ruleDaoImpl.querySaopRuleGroupRelaMap");

        int allSaopRuleGroupInfoListSize = allSaopRuleGroupInfoList.size();
        int allsaopRuleGroupRelaInfoListSize = allsaopRuleGroupRelaInfoList.size();

        if (allSaopRuleGroupInfoListSize > 0 && allsaopRuleGroupRelaInfoListSize > 0) {
            for (int i = 0; i < allSaopRuleGroupInfoListSize; i++) {
                //每个业务规则分组项
                Map saopRuleGroupMap = (Map) allSaopRuleGroupInfoList.get(i);
                if (null == saopRuleGroupMap || null == saopRuleGroupMap.get("groupId")) {
                    continue;
                }

                //当前规则分组编码
                String curRuleGroupId = String.valueOf(saopRuleGroupMap.get("groupId"));

                //当前业务规则分组下的规则编码集合
                List ruleIdList = new ArrayList();
                saopRuleGroupMap.put("ruleIdList", ruleIdList);

                for (int j = 0; j < allsaopRuleGroupRelaInfoListSize; j++) {
                    Map saopRuleGroupRelaMap = (Map) allsaopRuleGroupRelaInfoList.get(j);
                    if (null == saopRuleGroupRelaMap || null == saopRuleGroupRelaMap.get("groupId")) {
                        continue;
                    }

                    //当前规则分组编码
                    String ruleRelaGroupId = String.valueOf(saopRuleGroupRelaMap.get("groupId"));

                    if (curRuleGroupId.equals(ruleRelaGroupId)) {
                        //获取规则编码
                        String ruleId = "";
                        if (null != saopRuleGroupRelaMap.get("rule_id")) {
                            ruleId = String.valueOf(saopRuleGroupRelaMap.get("rule_id"));
                        }

                        if (!StringUtils.isEmpty(ruleId)) {
                            ruleIdList.add(ruleId);
                        }
                    }
                }
            }

            saopRuleGroupRelaList = allSaopRuleGroupInfoList;

            jedis.set(RuleDomain.REDIS_KEY_RULE_ENTRANCE.getBytes(),SerializeUtil.serializeList(saopRuleGroupRelaList));
        }
    }


    return saopRuleGroupRelaList;
}