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

The following examples show how to use redis.clients.jedis.Jedis#srem() . 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: RedisClient.java    From apollo with GNU General Public License v2.0 6 votes vote down vote up
public boolean sRem(String key, String member) throws Exception {
    Jedis jedis = null;
    try {
        jedis = this.jedisPool.getResource();
        Long r = jedis.srem(key, member);
        return r == 1;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        this.jedisPool.returnBrokenResource(jedis);
        throw e;
    } finally {
        if (jedis != null) {
            this.jedisPool.returnResource(jedis);
        }
    }
}
 
Example 2
Source File: RedisClient.java    From Mykit with Apache License 2.0 6 votes vote down vote up
public <T> long srem(String key, List<T> ts) {
	Jedis client = jedisPool.getResource();
	try {
		if (ts == null || ts.size() == 0) {
			return 0l;
		}
		String[] values = new String[ts.size()];
		for (int i = 0; i < ts.size(); i++) {
			values[i] = ts.get(i).toString();
		}
		return client.srem(key, values);
	} finally {
		// 向连接池“归还”资源
		jedisPool.returnResourceObject(client);
	}
}
 
Example 3
Source File: JedisUtil.java    From scaffold-cloud with MIT License 5 votes vote down vote up
/**
 * Set缓存中删除值
 *
 * @param key   键
 * @param value 值
 * @return
 */
public static long srem(String key, String... value) {
    long result = 0;
    Jedis jedis = null;
    try {
        jedis = getResource();
        result = jedis.srem(key, value);
    } catch (Exception e) {
        logger.warn("setSetAdd {} = {}", key, value, e);
    } finally {
        close(jedis);
    }
    return result;
}
 
Example 4
Source File: GroupDB.java    From VileBot with MIT License 5 votes vote down vote up
/**
 * Removes the given nick to the given group.
 * 
 * @param group The group name to use
 * @param nick The nick to remove
 * @return true iff an element was removed
 */
private static boolean remFromGroup( String group, String nick )
{
    Jedis jedis = pool.getResource();
    try
    {
        long reply = jedis.srem( keyOfGroupSetsPrefix + group, nick );
        return reply == 1;
    }
    finally
    {
        pool.returnResource( jedis );
    }
}
 
Example 5
Source File: RedisUtil.java    From RedisBungee with Eclipse Public License 1.0 5 votes vote down vote up
public static void cleanUpPlayer(String player, Jedis rsc) {
    rsc.srem("proxy:" + RedisBungee.getApi().getServerId() + ":usersOnline", player);
    rsc.hdel("player:" + player, "server",  "ip", "proxy");
    long timestamp = System.currentTimeMillis();
    rsc.hset("player:" + player, "online", String.valueOf(timestamp));
    rsc.publish("redisbungee-data", RedisBungee.getGson().toJson(new DataManager.DataManagerMessage<>(
            UUID.fromString(player), DataManager.DataManagerMessage.Action.LEAVE,
            new DataManager.LogoutPayload(timestamp))));
}
 
Example 6
Source File: RedisJobStore.java    From redis-quartz with MIT License 5 votes vote down vote up
/**
 * Resume triggers in redis.
 *
 * @param triggerGroupHashKey the trigger group hash key
 * @param jedis thread-safe redis connection
 * @return resumed trigger groups set 
 * @throws JobPersistenceException the job persistence exception
 */
private Set<String> resumeTriggers(String triggerGroupHashKey, Jedis jedis) throws JobPersistenceException {
     Set<String> resumedTriggerdGroups = new HashSet<>();
	jedis.srem(PAUSED_TRIGGER_GROUPS_SET, triggerGroupHashKey);
	Set<String> triggerHashKeys = jedis.smembers(triggerGroupHashKey);
	for(String triggerHashKey : triggerHashKeys) {
		OperableTrigger trigger = retrieveTrigger(new TriggerKey(triggerHashKey.split(":")[2], triggerHashKey.split(":")[1]), jedis);
		resumeTrigger(trigger, jedis);
		resumedTriggerdGroups.add(trigger.getKey().getGroup());	// as we currently support only EQUALS matcher's operator, the paused group set will consist of one paused group only.			
	}
	
	return resumedTriggerdGroups;
}
 
Example 7
Source File: RedisJobStore.java    From redis-quartz with MIT License 5 votes vote down vote up
/**
 * Removes the calendar from redis.
 *
 * @param calName the calendar name
 * @param jedis thread-safe redis connection
 * @throws JobPersistenceException
 */
private void removeCalendar(String calName, Jedis jedis)
		throws JobPersistenceException {
	String calendarHashKey = createCalendarHashKey(calName);
	
	// checking if there are triggers pointing to this calendar
	String calendarTriggersSetkey = createCalendarTriggersSetKey(calName);
	if (jedis.scard(calendarTriggersSetkey) > 0)
		throw new JobPersistenceException("there are triggers pointing to: " + calendarHashKey + ", calendar can't be removed");			
			
	// removing the calendar
	jedis.del(calendarHashKey);
	jedis.srem(CALENDARS_SET, calendarHashKey);
}
 
Example 8
Source File: RedisJobStore.java    From redis-quartz with MIT License 5 votes vote down vote up
/**
 * Removes the trigger from redis.
 *
 * @param triggerKey the trigger key
 * jedis thread-safe redis connection
 * @throws JobPersistenceException
 */
private void removeTrigger(TriggerKey triggerKey, Jedis jedis) 
		throws JobPersistenceException {
	String triggerHashKey = createTriggerHashKey(triggerKey.getGroup(), triggerKey.getName());
	String triggerGroupSetKey = createTriggerGroupSetKey(triggerKey.getGroup());
	
	jedis.srem(TRIGGERS_SET, triggerHashKey);
	jedis.srem(triggerGroupSetKey, triggerHashKey);
	
	String jobHashkey = jedis.hget(triggerHashKey, JOB_HASH_KEY);
	String jobTriggerSetkey = createJobTriggersSetKey(jobHashkey.split(":")[1], jobHashkey.split(":")[2]); 
	jedis.srem(jobTriggerSetkey, triggerHashKey);
	
	if (jedis.scard(triggerGroupSetKey) == 0) {
		jedis.srem(TRIGGER_GROUPS_SET, triggerGroupSetKey);
	}
	
	// handling orphaned jobs
	if (jedis.scard(jobTriggerSetkey) == 0 && jedis.exists(jobHashkey)) {
		if (!Boolean.parseBoolean(jedis.hget(jobHashkey, IS_DURABLE))) {
			JobKey jobKey = new JobKey(jobHashkey.split(":")[2], jobHashkey.split(":")[1]);
			removeJob(jobKey, jedis);
			signaler.notifySchedulerListenersJobDeleted(jobKey);
		}				
	}
	
	String calendarName = jedis.hget(triggerHashKey, CALENDAR_NAME);
	if (!calendarName.isEmpty()) {
		String calendarTriggersSetKey = createCalendarTriggersSetKey(calendarName);
		jedis.srem(calendarTriggersSetKey, triggerHashKey);			
	}			
		
	// removing trigger state
	unsetTriggerState(triggerHashKey);		
	jedis.del(triggerHashKey);
}
 
Example 9
Source File: RedisJobStore.java    From redis-quartz with MIT License 5 votes vote down vote up
/**
 * Removes the job from redis.
 *
 * @param jobKey the job key
 * @param jedis thread-safe redis connection
 */
private void removeJob(JobKey jobKey, Jedis jedis) {
	String jobHashKey = createJobHashKey(jobKey.getGroup(), jobKey.getName());
	String jobDataMapHashKey = createJobDataMapHashKey(jobKey.getGroup(), jobKey.getName());
	String jobGroupSetKey = createJobGroupSetKey(jobKey.getGroup());
	
	jedis.del(jobHashKey);
	jedis.del(jobDataMapHashKey);
	jedis.srem(JOBS_SET, jobHashKey);
	jedis.srem(BLOCKED_JOBS_SET, jobHashKey);
	jedis.srem(jobGroupSetKey, jobHashKey);
	if (jedis.scard(jobGroupSetKey) == 0) {
		jedis.srem(JOB_GROUPS_SET, jobGroupSetKey);
	}
}
 
Example 10
Source File: RedisServiceImpl.java    From ace-cache with Apache License 2.0 5 votes vote down vote up
@Override
public Long srem(String key, String... members) {
    Jedis jedis = null;
    Long res = null;
    try {
        jedis = pool.getResource();
        res = jedis.srem(key, members);
    } catch (Exception e) {

        LOGGER.error(e.getMessage());
    } finally {
        returnResource(pool, jedis);
    }
    return res;
}
 
Example 11
Source File: JedisClientPool.java    From paas with Apache License 2.0 5 votes vote down vote up
@Override
public Long srem(String key, String... members) {
    Jedis jedis = jedisPool.getResource();
    Long result = jedis.srem(key, members);
    jedis.close();
    return result;
}
 
Example 12
Source File: RedisClient.java    From Mykit with Apache License 2.0 5 votes vote down vote up
public long srem(String key, String... values) {
	Jedis client = jedisPool.getResource();
	try {
		return client.srem(key, values);
	} finally {
		// 向连接池“归还”资源
		jedisPool.returnResourceObject(client);
	}
}
 
Example 13
Source File: JedisClientPool.java    From blog-sample with Apache License 2.0 5 votes vote down vote up
@Override
public Long srem(String key, String... members) {
    Jedis jedis = jedisPool.getResource();
    Long result = jedis.srem(key, members);
    jedis.close();
    return result;
}
 
Example 14
Source File: JedisUtils.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * 通过key删除set中对应的value值
 * </p>
 *
 * @param key
 * @param members
 *            可以是一个String 也可以是一个String数组
 * @return 删除的个数
 */
public Long srem(String key, String... members) {
    Jedis jedis = null;
    Long res = null;
    try {
        jedis = jedisPool.getResource();
        res = jedis.srem(key, members);
    } catch (Exception e) {

        log.error(e.getMessage());
    } finally {
        returnResource(jedisPool, jedis);
    }
    return res;
}
 
Example 15
Source File: RedisStorage.java    From quartz-redis-jobstore with Apache License 2.0 4 votes vote down vote up
/**
 * Remove the given job from Redis
 * @param jobKey the job to be removed
 * @param jedis a thread-safe Redis connection
 * @return true if the job was removed; false if it did not exist
 */
@Override
public boolean removeJob(JobKey jobKey, Jedis jedis) throws JobPersistenceException {
    final String jobHashKey = redisSchema.jobHashKey(jobKey);
    final String jobBlockedKey = redisSchema.jobBlockedKey(jobKey);
    final String jobDataMapHashKey = redisSchema.jobDataMapHashKey(jobKey);
    final String jobGroupSetKey = redisSchema.jobGroupSetKey(jobKey);
    final String jobTriggerSetKey = redisSchema.jobTriggersSetKey(jobKey);

    Pipeline pipe = jedis.pipelined();
    // remove the job and any associated data
    Response<Long> delJobHashKeyResponse = pipe.del(jobHashKey);
    // remove the blocked job key
    pipe.del(jobBlockedKey);
    // remove the job's data map
    pipe.del(jobDataMapHashKey);
    // remove the job from the set of all jobs
    pipe.srem(redisSchema.jobsSet(), jobHashKey);
    // remove the job from the set of blocked jobs
    pipe.srem(redisSchema.blockedJobsSet(), jobHashKey);
    // remove the job from its group
    pipe.srem(jobGroupSetKey, jobHashKey);
    // retrieve the keys for all triggers associated with this job, then delete that set
    Response<Set<String>> jobTriggerSetResponse = pipe.smembers(jobTriggerSetKey);
    pipe.del(jobTriggerSetKey);
    Response<Long> jobGroupSetSizeResponse = pipe.scard(jobGroupSetKey);
    pipe.sync();
    if(jobGroupSetSizeResponse.get() == 0){
        // The group now contains no jobs. Remove it from the set of all job groups.
        jedis.srem(redisSchema.jobGroupsSet(), jobGroupSetKey);
    }

    // remove all triggers associated with this job
    pipe = jedis.pipelined();
    for (String triggerHashKey : jobTriggerSetResponse.get()) {
        // get this trigger's TriggerKey
        final TriggerKey triggerKey = redisSchema.triggerKey(triggerHashKey);
        final String triggerGroupSetKey = redisSchema.triggerGroupSetKey(triggerKey);
        unsetTriggerState(triggerHashKey, jedis);
        // remove the trigger from the set of all triggers
        pipe.srem(redisSchema.triggersSet(), triggerHashKey);
        // remove the trigger's group from the set of all trigger groups
        pipe.srem(redisSchema.triggerGroupsSet(), triggerGroupSetKey);
        // remove this trigger from its group
        pipe.srem(triggerGroupSetKey, triggerHashKey);
        // delete the trigger
        pipe.del(triggerHashKey);
    }
    pipe.sync();

    return delJobHashKeyResponse.get() == 1;
}
 
Example 16
Source File: RedisStorage.java    From quartz-redis-jobstore with Apache License 2.0 4 votes vote down vote up
/**
 * Remove (delete) the <code>{@link org.quartz.Trigger}</code> with the given key.
 * @param triggerKey the key of the trigger to be removed
 * @param removeNonDurableJob if true, the job associated with the given trigger will be removed if it is non-durable
 *                            and has no other triggers
 * @param jedis a thread-safe Redis connection
 * @return true if the trigger was found and removed
 */
@Override
protected boolean removeTrigger(TriggerKey triggerKey, boolean removeNonDurableJob, Jedis jedis) throws JobPersistenceException, ClassNotFoundException {
    final String triggerHashKey = redisSchema.triggerHashKey(triggerKey);
    final String triggerGroupSetKey = redisSchema.triggerGroupSetKey(triggerKey);

    if(!jedis.exists(triggerHashKey)){
        return false;
    }

    OperableTrigger trigger = retrieveTrigger(triggerKey, jedis);

    final String jobHashKey = redisSchema.jobHashKey(trigger.getJobKey());
    final String jobTriggerSetKey = redisSchema.jobTriggersSetKey(trigger.getJobKey());

    Pipeline pipe = jedis.pipelined();
    // remove the trigger from the set of all triggers
    pipe.srem(redisSchema.triggersSet(), triggerHashKey);
    // remove the trigger from its trigger group set
    pipe.srem(triggerGroupSetKey, triggerHashKey);
    // remove the trigger from the associated job's trigger set
    pipe.srem(jobTriggerSetKey, triggerHashKey);
    pipe.sync();

    if(jedis.scard(triggerGroupSetKey) == 0){
        // The trigger group set is empty. Remove the trigger group from the set of trigger groups.
        jedis.srem(redisSchema.triggerGroupsSet(), triggerGroupSetKey);
    }

    if(removeNonDurableJob){
        pipe = jedis.pipelined();
        Response<Long> jobTriggerSetKeySizeResponse = pipe.scard(jobTriggerSetKey);
        Response<Boolean> jobExistsResponse = pipe.exists(jobHashKey);
        pipe.sync();
        if(jobTriggerSetKeySizeResponse.get() == 0 && jobExistsResponse.get()){
            JobDetail job = retrieveJob(trigger.getJobKey(), jedis);
            if(!job.isDurable()){
                // Job is not durable and has no remaining triggers. Delete it.
                removeJob(job.getKey(), jedis);
                signaler.notifySchedulerListenersJobDeleted(job.getKey());
            }
        }
    }

    if(isNullOrEmpty(trigger.getCalendarName())){
        jedis.srem(redisSchema.calendarTriggersSetKey(trigger.getCalendarName()), triggerHashKey);
    }
    unsetTriggerState(triggerHashKey, jedis);
    jedis.del(triggerHashKey);
    jedis.del(redisSchema.triggerDataMapHashKey(triggerKey));
    return true;
}
 
Example 17
Source File: MQConsumer.java    From jseckill with Apache License 2.0 4 votes vote down vote up
@Override
        public void handleDelivery(String consumerTag, Envelope envelope,
                                   AMQP.BasicProperties properties, byte[] body)
                throws IOException {

            long threadId1 = Thread.currentThread().getId();
            logger.info("---receive_threadId_1={}", threadId1);

            String msg = new String(body, "UTF-8");
            logger.info("[mqReceive]  '" + msg + "'");
            SeckillMsgBody msgBody = JSON.parseObject(msg, SeckillMsgBody.class);

            AckAction ackAction = AckAction.ACCEPT;
            try {
                // 这里演延时2秒,模式秒杀的耗时操作, 上线的时候需要注释掉
//                try {
//                    Thread.sleep(2000);
//                } catch (InterruptedException e) {
//                    logger.error(e.getMessage(), e);
//                }
                seckillService.handleInRedis(msgBody.getSeckillId(), msgBody.getUserPhone());
                ackAction = AckAction.ACCEPT;
            } catch (SeckillException seckillE) {
                if (seckillE.getSeckillStateEnum() == SeckillStateEnum.SOLD_OUT
                        || seckillE.getSeckillStateEnum() == SeckillStateEnum.REPEAT_KILL) {
                    // 已售罄,或者此人之前已经秒杀过的
                    ackAction = AckAction.THROW;
                } else {
                    logger.error(seckillE.getMessage(), seckillE);
                    logger.info("---->NACK--error_requeue!!!");
                    ackAction = AckAction.RETRY;
                }
            } finally {
                logger.info("------processIt----");
                switch (ackAction) {
                    case ACCEPT:
                        try {
                            logger.info("---->ACK");
                            channel.basicAck(envelope.getDeliveryTag(), false);
                        } catch (IOException ioE) {
                            logger.info("---------basicAck_throws_IOException----------");
                            logger.error(ioE.getMessage(), ioE);
                            throw ioE;
                        }

                        Jedis jedis = jedisPool.getResource();
                        jedis.srem(RedisKey.QUEUE_PRE_SECKILL, msgBody.getSeckillId() + "@" + msgBody.getUserPhone());
                        jedis.close();
                        break;

                    case THROW:
                        logger.info("--LET_MQ_ACK REASON:SeckillStateEnum.SOLD_OUT,SeckillStateEnum.REPEAT_KILL");
                        channel.basicAck(envelope.getDeliveryTag(), false);

                        Jedis jedis1 = jedisPool.getResource();
                        jedis1.srem(RedisKey.QUEUE_PRE_SECKILL, msgBody.getSeckillId() + "@" + msgBody.getUserPhone());
                        jedis1.close();

                        break;

                    case RETRY:
                        logger.info("---->NACK--error_requeue!!!");
                        channel.basicNack(envelope.getDeliveryTag(), false, true);
                        break;

                }
            }
        }
 
Example 18
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private Long srem0(Jedis j, String key, String... members) {
	return j.srem(key, members);
}
 
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 long srem(String key, String member) {
	Jedis jedis = getJedis();
	long s = jedis.srem(key, member);
	returnJedis(jedis);
	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 long srem(String key, String member) {
	Jedis jedis = getJedis();
	long s = jedis.srem(key, member);
	jedis.close();
	return s;
}