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

The following examples show how to use redis.clients.jedis.Jedis#sismember() . 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: Node.java    From XRTB with Apache License 2.0 6 votes vote down vote up
boolean jedisIsMember(String key, String member) {
	boolean t = false;
	if (Configuration.getInstance().jedisPool == null)
		return false;

	try {

		if (member.equals("842AAB10FBA04247B3A9CE00C9172350")) {
			System.out.println("$$$$$$$$$$$$$$$$$$$ KEYTEST on " + key);
		}
		// Jedis jedis =
		// Configuration.getInstance().jedisPool.getResource();
		Jedis jedis = Configuration.getInstance().jedisPool.borrowObject();
		t = jedis.sismember(key, member);

		if (member.equals("842AAB10FBA04247B3A9CE00C9172350")) {
			System.out.println("$$$$$$$$$$$$$$$$$$$ TEST RETURNS: " + t);
		}
		// Configuration.getInstance().jedisPool.returnResource(jedis);
		Configuration.getInstance().jedisPool.returnObject(jedis);
		// t = Configuration.getInstance().jedisPool.sismember(key, member);
	} catch (Exception error) {
		error.printStackTrace();
	}
	return t;
}
 
Example 2
Source File: Node.java    From bidder with Apache License 2.0 6 votes vote down vote up
boolean jedisIsMember(String key, String member) {
	boolean t = false;
	if (Configuration.getInstance().jedisPool == null)
		return false;

	try {

		if (member.equals("842AAB10FBA04247B3A9CE00C9172350")) {
			System.out.println("$$$$$$$$$$$$$$$$$$$ KEYTEST on " + key);
		}
		// Jedis jedis =
		// Configuration.getInstance().jedisPool.getResource();
		Jedis jedis = Configuration.getInstance().jedisPool.borrowObject();
		t = jedis.sismember(key, member);

		if (member.equals("842AAB10FBA04247B3A9CE00C9172350")) {
			System.out.println("$$$$$$$$$$$$$$$$$$$ TEST RETURNS: " + t);
		}
		// Configuration.getInstance().jedisPool.returnResource(jedis);
		Configuration.getInstance().jedisPool.returnObject(jedis);
		// t = Configuration.getInstance().jedisPool.sismember(key, member);
	} catch (Exception error) {
		error.printStackTrace();
	}
	return t;
}
 
Example 3
Source File: RedisJobStore.java    From redis-quartz with MIT License 6 votes vote down vote up
/**
 * Resume a trigger in redis.
 *
 * @param trigger the trigger
 * @param jedis thread-safe redis connection
 * @throws JobPersistenceException
 */
private void resumeTrigger(OperableTrigger trigger, Jedis jedis) throws JobPersistenceException {
	String triggerHashKey = createTriggerHashKey(trigger.getKey().getGroup(), trigger.getKey().getName());
	if (!jedis.sismember(TRIGGERS_SET, triggerHashKey))
		throw new JobPersistenceException("trigger: " + trigger + " does not exist");
	
	if (jedis.zscore(RedisTriggerState.PAUSED.getKey(), triggerHashKey) == null && jedis.zscore(RedisTriggerState.PAUSED_BLOCKED.getKey(), triggerHashKey) == null)
		throw new JobPersistenceException("trigger: " + trigger + " is not paused");
			
	String jobHashKey = createJobHashKey(trigger.getJobKey().getGroup(), trigger.getJobKey().getName());
	Date nextFireTime = trigger.getNextFireTime();
	if (nextFireTime != null) {
		if (jedis.sismember(BLOCKED_JOBS_SET, jobHashKey))
			setTriggerState(RedisTriggerState.BLOCKED, (double)nextFireTime.getTime(), triggerHashKey);
		else
			setTriggerState(RedisTriggerState.WAITING, (double)nextFireTime.getTime(), triggerHashKey);
	}
}
 
Example 4
Source File: SeckillServiceImpl.java    From jseckill with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param seckillId
 * @param userPhone
 * @return 0: 排队中; 1: 秒杀成功; 2: 秒杀失败
 */
@Override
public int isGrab(long seckillId, long userPhone) {
    int result = 0 ;

    Jedis jedis = jedisPool.getResource();
    try {
        String boughtKey = RedisKeyPrefix.BOUGHT_USERS + seckillId;
        result = jedis.sismember(boughtKey, String.valueOf(userPhone)) ? 1 : 0;
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
        result = 0;
    }

    if (result == 0) {
        if (!jedis.sismember(RedisKey.QUEUE_PRE_SECKILL, seckillId + "@" + userPhone)) {
            result =2;
        }
    }
    return result;
}
 
Example 5
Source File: SeckillServiceImpl.java    From jseckill with Apache License 2.0 6 votes vote down vote up
/**
 * 在Redis中真正进行秒杀操作
 * @param seckillId
 * @param userPhone
 * @throws SeckillException
 */
@Override
public void handleInRedis(long seckillId, long userPhone) throws SeckillException {
    Jedis jedis = jedisPool.getResource();

    String inventoryKey = RedisKeyPrefix.SECKILL_INVENTORY + seckillId;
    String boughtKey = RedisKeyPrefix.BOUGHT_USERS + seckillId;

    String inventoryStr = jedis.get(inventoryKey);
    int inventory = Integer.valueOf(inventoryStr);
    if (inventory <= 0) {
        logger.info("handleInRedis SECKILLSOLD_OUT. seckillId={},userPhone={}", seckillId, userPhone);
        throw new SeckillException(SeckillStateEnum.SOLD_OUT);
    }
    if (jedis.sismember(boughtKey, String.valueOf(userPhone))) {
        logger.info("handleInRedis SECKILL_REPEATED. seckillId={},userPhone={}", seckillId, userPhone);
        throw new SeckillException(SeckillStateEnum.REPEAT_KILL);
    }
    jedis.decr(inventoryKey);
    jedis.sadd(boughtKey, String.valueOf(userPhone));
    logger.info("handleInRedis_done");
}
 
Example 6
Source File: GroupDB.java    From VileBot with MIT License 5 votes vote down vote up
/**
 * Checks if the given nick is in the given group.
 * 
 * @param group The group name to check
 * @param nick The nick to check
 * @return true iff the nick is in the Op group
 */
private static boolean isInGroup( String group, String nick )
{
    Jedis jedis = pool.getResource();
    try
    {
        return nick != null && jedis.sismember( keyOfGroupSetsPrefix + group, nick );
    }
    finally
    {
        pool.returnResource( jedis );
    }
}
 
Example 7
Source File: JedisUtils.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * 通过key判断value是否是set中的元素
 * </p>
 *
 * @param key
 * @param member
 * @return
 */
public Boolean sismember(String key, String member) {
    Jedis jedis = null;
    Boolean res = null;
    try {
        jedis = jedisPool.getResource();
        res = jedis.sismember(key, member);
    } catch (Exception e) {

        log.error(e.getMessage());
    } finally {
        returnResource(jedisPool, jedis);
    }
    return res;
}
 
Example 8
Source File: RedisJobStore.java    From redis-quartz with MIT License 5 votes vote down vote up
/**
 * Resume a job in redis.
 *
 * @param jobKey the job key
 * @param jedis thread-safe redis connection
 * @throws JobPersistenceException
 */
private void resumeJob(JobKey jobKey, Jedis jedis) throws JobPersistenceException {
	String jobHashKey = createJobHashKey(jobKey.getGroup(),jobKey.getName());
	if (!jedis.sismember(JOBS_SET, jobHashKey))
		throw new JobPersistenceException("job: " + jobHashKey + " des not exist");
		
	List<OperableTrigger> triggers = getTriggersForJob(jobKey);
	for (OperableTrigger trigger : triggers)
		resumeTrigger(trigger, jedis);
}
 
Example 9
Source File: RedisJobStore.java    From redis-quartz with MIT License 5 votes vote down vote up
/**
 * Pause job.
 *
 * @param jobKey the job key
 * @param jedis thread-safe redis connection
 * @throws JobPersistenceException
 */
private void pauseJob(String jobHashKey, Jedis jedis) throws JobPersistenceException {
	if (!jedis.sismember(JOBS_SET, jobHashKey))
		throw new JobPersistenceException("job: " + jobHashKey + " des not exist");
	
	String jobTriggerSetkey = createJobTriggersSetKey(jobHashKey.split(":")[1], jobHashKey.split(":")[2]);
	List<OperableTrigger> triggers = getTriggersForJob(jobTriggerSetkey, jedis);
	for (OperableTrigger trigger : triggers)
		pauseTrigger(trigger.getKey(), jedis);
}
 
Example 10
Source File: RedisServiceImpl.java    From ace-cache with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean sismember(String key, String member) {
    Jedis jedis = null;
    Boolean res = null;
    try {
        jedis = pool.getResource();
        res = jedis.sismember(key, member);
    } catch (Exception e) {

        LOGGER.error(e.getMessage());
    } finally {
        returnResource(pool, jedis);
    }
    return res;
}
 
Example 11
Source File: RedisScheduler.java    From ScriptSpider with Apache License 2.0 5 votes vote down vote up
/**
 * 写进去url种子
 *
 * @param urlSeed 种子
 */
@Override
public void push(UrlSeed urlSeed) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        if (urlSeed.getUrl() == null
                || urlSeed.getUrl().trim().equals("")
                || urlSeed.getUrl().trim().equals("#")
                || urlSeed.getUrl().trim().toLowerCase().contains("javascript:"))
            return;
        if (jedis.sismember(Prefix_Set, urlSeed.getUrl())) {
            //            logger.info("UrlSeed重复:" + urlSeed.getUrl());
        } else {
            jedis.sadd(Prefix_Set, urlSeed.getUrl());
            Gson gson = new Gson();
            String urlSeedJson = gson.toJson(urlSeed);
            if (urlSeed.getPriority() == 5l) {
                jedis.rpush(Prefix_Queue_default, urlSeedJson);
            } else if (urlSeed.getPriority() > 5l) {
                jedis.rpush(Prefix_Queue_high, urlSeedJson);
            } else {
                jedis.rpush(Prefix_Queue_low, urlSeedJson);
            }
        }
    } catch (Exception e) {
        logger.error("连接获取失败!", e);
    } finally {
        if (null != jedis) {
            jedis.close();
        }
    }
}
 
Example 12
Source File: JedisClientPool.java    From paas with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean sismember(String key, String member) {
    Jedis jedis = jedisPool.getResource();
    Boolean result = jedis.sismember(key, member);
    jedis.close();
    return result;
}
 
Example 13
Source File: JedisClientPool.java    From blog-sample with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean sismember(String key, String member) {
    Jedis jedis = jedisPool.getResource();
    Boolean result = jedis.sismember(key, member);
    jedis.close();
    return result;
}
 
Example 14
Source File: JedisCache.java    From charging_pile_cloud with MIT License 5 votes vote down vote up
/**
 * 集合中是否存在某种元素
 *
 * @param dbIndex
 * @param key
 * @param value
 * @return
 */
public static boolean existInSet(int dbIndex, String key, String value) {
    boolean result = false;
    Jedis jedis = null;
    try {
        jedis = jedisManager.getJedis();
        jedis.select(dbIndex);
        result = jedis.sismember(key, value);
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
    } finally {
        jedis.close();
    }
    return result;
}
 
Example 15
Source File: SeckillServiceImpl.java    From jseckill with Apache License 2.0 4 votes vote down vote up
/**
 * @param seckillId
 * @param userPhone
 * @param md5
 * @return
 * @throws SeckillException
 * @TODO 先在redis里处理,然后发送到mq,最后减库存到数据库
 */
private SeckillExecution handleSeckillAsync(long seckillId, long userPhone, String md5)
        throws SeckillException {
    if (md5 == null || !md5.equals(getMD5(seckillId))) {
        // 遇到黑客攻击,检测到了数据篡改
        logger.info("seckill_DATA_REWRITE!!!. seckillId={},userPhone={}", seckillId, userPhone);
        throw new SeckillException(SeckillStateEnum.DATA_REWRITE);
    }

    long threadId = Thread.currentThread().getId();

    Jedis jedis = jedisPool.getResource();
    String inventoryKey = RedisKeyPrefix.SECKILL_INVENTORY + seckillId;
    String boughtKey = RedisKeyPrefix.BOUGHT_USERS + seckillId;

    String inventoryStr = jedis.get(inventoryKey);
    int inventory = Integer.valueOf(inventoryStr);
    if (inventory <= 0) {
        jedis.close();
        logger.info("SECKILLSOLD_OUT. seckillId={},userPhone={}", seckillId, userPhone);
        throw new SeckillException(SeckillStateEnum.SOLD_OUT);
    }
    if (jedis.sismember(boughtKey, String.valueOf(userPhone))) {
        jedis.close();
        //重复秒杀
        logger.info("SECKILL_REPEATED. seckillId={},userPhone={}", seckillId, userPhone);
        throw new SeckillException(SeckillStateEnum.REPEAT_KILL);
    } else {
        jedis.close();

        // 进入待秒杀队列,进行后续串行操作
        SeckillMsgBody msgBody = new SeckillMsgBody();
        msgBody.setSeckillId(seckillId);
        msgBody.setUserPhone(userPhone);
        mqProducer.send(msgBody);

        // 立即返回给客户端,说明秒杀成功了
        PayOrder payOrder = new PayOrder();
        payOrder.setUserPhone(userPhone);
        payOrder.setSeckillId(seckillId);
        payOrder.setState(SeckillStateEnum.ENQUEUE_PRE_SECKILL.getState());
        logger.info("ENQUEUE_PRE_SECKILL>>>seckillId={},userPhone={}", seckillId, userPhone);
        return new SeckillExecution(seckillId, SeckillStateEnum.ENQUEUE_PRE_SECKILL, payOrder);
    }
}
 
Example 16
Source File: SeckillServiceImpl.java    From jseckill with Apache License 2.0 4 votes vote down vote up
/**
 * @param seckillId
 * @param userPhone
 * @param md5
 * @return
 * @throws SeckillException
 * @TODO 先在redis里处理,然后发送到mq,最后减库存到数据库
 */
private SeckillExecution handleSeckillAsync(long seckillId, long userPhone, String md5)
        throws SeckillException {
    if (md5 == null || !md5.equals(getMD5(seckillId))) {
        logger.info("seckill_DATA_REWRITE!!!. seckillId={},userPhone={}", seckillId, userPhone);
        throw new SeckillException(SeckillStateEnum.DATA_REWRITE);
    }

    Jedis jedis = jedisPool.getResource();
    String inventoryKey = RedisKeyPrefix.SECKILL_INVENTORY + seckillId;
    if (jedis.sismember(RedisKey.SECKILLED_USER, String.valueOf(userPhone))) {
        //重复秒杀
        logger.info("seckill REPEATED. seckillId={},userPhone={}", seckillId, userPhone);
        throw new SeckillException(SeckillStateEnum.REPEAT_KILL);
    } else {
        String inventoryStr = jedis.get(inventoryKey);
        int inventory = Integer.valueOf(inventoryStr);
        if (inventory <= 0) {
            throw new SeckillException(SeckillStateEnum.SOLD_OUT);
        }
        jedis.watch(inventoryKey);
        Transaction tx = jedis.multi();
        tx.decr(inventoryKey);
        tx.sadd(RedisKey.SECKILLED_USER, String.valueOf(userPhone));
        List<Object> resultList = tx.exec();
        jedis.unwatch();
        if (resultList != null && resultList.size() == 2) {
            // 秒杀成功,后面异步更新到数据库中
            // 发送消息到消息队列
            SeckillMsgBody msgBody = new SeckillMsgBody();
            msgBody.setSeckillId(seckillId);
            msgBody.setUserPhone(userPhone);
            mqProducer.send(JSON.toJSONString(msgBody));

            // 立即返回给客户端,说明秒杀成功了
            SuccessKilled successKilled = new SuccessKilled();
            successKilled.setUserPhone(userPhone);
            successKilled.setSeckillId(seckillId);
            successKilled.setState(SeckillStateEnum.SUCCESS.getState());
            return new SeckillExecution(seckillId, SeckillStateEnum.SUCCESS, successKilled);
        } else {
            throw new SeckillException(SeckillStateEnum.RUSH_FAILED);
        }
    }
}
 
Example 17
Source File: RedisJobStore.java    From redis-quartz with MIT License 4 votes vote down vote up
/**
 * Stores trigger in redis.
 *
 * @param newTrigger the new trigger
 * @param replaceExisting replace existing
 * @param jedis thread-safe redis connection
 * @throws JobPersistenceException
 * @throws ObjectAlreadyExistsException
 */
private void storeTrigger(OperableTrigger newTrigger, boolean replaceExisting, Jedis jedis)
          throws JobPersistenceException {
	String triggerHashKey = createTriggerHashKey(newTrigger.getKey().getGroup(), newTrigger.getKey().getName());
	String triggerGroupSetKey = createTriggerGroupSetKey(newTrigger.getKey().getGroup());
	String jobHashkey = createJobHashKey(newTrigger.getJobKey().getGroup(), newTrigger.getJobKey().getName());
	String jobTriggerSetkey = createJobTriggersSetKey(newTrigger.getJobKey().getGroup(), newTrigger.getJobKey().getName());
	
     if (jedis.exists(triggerHashKey) && !replaceExisting) {
        ObjectAlreadyExistsException ex = new ObjectAlreadyExistsException(newTrigger);
        log.warn(ex.toString());
     }
     Map<String, String> trigger = new HashMap<>();
	trigger.put(JOB_HASH_KEY, jobHashkey);
	trigger.put(DESCRIPTION, newTrigger.getDescription() != null ? newTrigger.getDescription() : "");
	trigger.put(NEXT_FIRE_TIME, newTrigger.getNextFireTime() != null ? Long.toString(newTrigger.getNextFireTime().getTime()) : "");
	trigger.put(PREV_FIRE_TIME, newTrigger.getPreviousFireTime() != null ? Long.toString(newTrigger.getPreviousFireTime().getTime()) : "");
	trigger.put(PRIORITY, Integer.toString(newTrigger.getPriority()));
	trigger.put(START_TIME, newTrigger.getStartTime() != null ? Long.toString(newTrigger.getStartTime().getTime()) : "");
	trigger.put(END_TIME, newTrigger.getEndTime() != null ? Long.toString(newTrigger.getEndTime().getTime()) : "");
	trigger.put(FINAL_FIRE_TIME, newTrigger.getFinalFireTime() != null ? Long.toString(newTrigger.getFinalFireTime().getTime()) : "");
	trigger.put(FIRE_INSTANCE_ID, newTrigger.getFireInstanceId() != null ?  newTrigger.getFireInstanceId() : "");
	trigger.put(MISFIRE_INSTRUCTION, Integer.toString(newTrigger.getMisfireInstruction()));
	trigger.put(CALENDAR_NAME, newTrigger.getCalendarName() != null ? newTrigger.getCalendarName() : "");
	if (newTrigger instanceof SimpleTrigger) {
		trigger.put(TRIGGER_TYPE, TRIGGER_TYPE_SIMPLE);
		trigger.put(REPEAT_COUNT, Integer.toString(((SimpleTrigger) newTrigger).getRepeatCount()));
		trigger.put(REPEAT_INTERVAL, Long.toString(((SimpleTrigger) newTrigger).getRepeatInterval()));
		trigger.put(TIMES_TRIGGERED, Integer.toString(((SimpleTrigger) newTrigger).getTimesTriggered()));
	} else if (newTrigger instanceof CronTrigger) {
		trigger.put(TRIGGER_TYPE, TRIGGER_TYPE_CRON);
		trigger.put(CRON_EXPRESSION, ((CronTrigger) newTrigger).getCronExpression() != null ? ((CronTrigger) newTrigger).getCronExpression() : "");
		trigger.put(TIME_ZONE_ID, ((CronTrigger) newTrigger).getTimeZone().getID() != null ? ((CronTrigger) newTrigger).getTimeZone().getID() : "");
	} else { // other trigger types are not supported
		 throw new UnsupportedOperationException();
	}		
	
	jedis.hmset(triggerHashKey, trigger);
	jedis.sadd(TRIGGERS_SET, triggerHashKey);
	jedis.sadd(TRIGGER_GROUPS_SET, triggerGroupSetKey);			
	jedis.sadd(triggerGroupSetKey, triggerHashKey);
	jedis.sadd(jobTriggerSetkey, triggerHashKey);
	if (newTrigger.getCalendarName() != null && !newTrigger.getCalendarName().isEmpty()) { // storing the trigger for calendar, if exists
		String calendarTriggersSetKey = createCalendarTriggersSetKey(newTrigger.getCalendarName());
		jedis.sadd(calendarTriggersSetKey, triggerHashKey);
	}
	
	if (jedis.sismember(PAUSED_TRIGGER_GROUPS_SET, triggerGroupSetKey) || jedis.sismember(PAUSED_JOB_GROUPS_SET, createJobGroupSetKey(newTrigger.getJobKey().getGroup()))) {	
		long nextFireTime = newTrigger.getNextFireTime() != null ? newTrigger.getNextFireTime().getTime() : -1;
		if (jedis.sismember(BLOCKED_JOBS_SET, jobHashkey))
			setTriggerState(RedisTriggerState.PAUSED_BLOCKED, (double)nextFireTime, triggerHashKey);
		else
			setTriggerState(RedisTriggerState.PAUSED, (double)nextFireTime, triggerHashKey);
	} else if (newTrigger.getNextFireTime() != null) {			
		setTriggerState(RedisTriggerState.WAITING, (double)newTrigger.getNextFireTime().getTime(), triggerHashKey);
	}
}
 
Example 18
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private Boolean sismember0(Jedis j, String key, String member) {
	return j.sismember(key, member);
}
 
Example 19
Source File: JedisUtil.java    From BigData with GNU General Public License v3.0 3 votes vote down vote up
/**
 * 确定一个给定的值是否存在
 * 
 * @param String
 *            key
 * @param String
 *            member 要判断的值
 * @return 存在返回1,不存在返回0
 * **/
public boolean sismember(String key, String member) {
	// ShardedJedis sjedis = getShardedJedis();
	Jedis sjedis = getJedis();
	boolean s = sjedis.sismember(key, member);
	returnJedis(sjedis);
	return s;
}
 
Example 20
Source File: JedisUtil.java    From Project with Apache License 2.0 3 votes vote down vote up
/**
 * 确定一个给定的值是否存在
 * 
 * @param  key
 * @param  member 要判断的值
 * @return 存在返回1,不存在返回0
 **/
public boolean sismember(String key, String member) {
	Jedis sjedis = getJedis();
	boolean s = sjedis.sismember(key, member);
	sjedis.close();
	return s;
}