Java Code Examples for redis.clients.jedis.Pipeline#del()

The following examples show how to use redis.clients.jedis.Pipeline#del() . 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: RedisStorage.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
/**
 * Store a job in Redis
 * @param jobDetail the {@link org.quartz.JobDetail} object to be stored
 * @param replaceExisting if true, any existing job with the same group and name as the given job will be overwritten
 * @param jedis a thread-safe Redis connection
 * @throws org.quartz.ObjectAlreadyExistsException
 */
@Override
@SuppressWarnings("unchecked")
public void storeJob(JobDetail jobDetail, boolean replaceExisting, Jedis jedis) throws ObjectAlreadyExistsException {
    final String jobHashKey = redisSchema.jobHashKey(jobDetail.getKey());
    final String jobDataMapHashKey = redisSchema.jobDataMapHashKey(jobDetail.getKey());
    final String jobGroupSetKey = redisSchema.jobGroupSetKey(jobDetail.getKey());

    if(!replaceExisting && jedis.exists(jobHashKey)){
        throw new ObjectAlreadyExistsException(jobDetail);
    }

    Pipeline pipe = jedis.pipelined();
    pipe.hmset(jobHashKey, (Map<String, String>) mapper.convertValue(jobDetail, new TypeReference<HashMap<String, String>>() {}));
    pipe.del(jobDataMapHashKey);
    if(jobDetail.getJobDataMap() != null && !jobDetail.getJobDataMap().isEmpty()){
        pipe.hmset(jobDataMapHashKey, getStringDataMap(jobDetail.getJobDataMap()));
    }

    pipe.sadd(redisSchema.jobsSet(), jobHashKey);
    pipe.sadd(redisSchema.jobGroupsSet(), jobGroupSetKey);
    pipe.sadd(jobGroupSetKey, jobHashKey);
    pipe.sync();
}
 
Example 2
Source File: RedisStorage.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
/**
 * Remove (delete) the <code>{@link org.quartz.Calendar}</code> with the given name.
 * @param calendarName the name of the calendar to be removed
 * @param jedis a thread-safe Redis connection
 * @return true if a calendar with the given name was found and removed
 */
@Override
public boolean removeCalendar(String calendarName, Jedis jedis) throws JobPersistenceException {
    final String calendarTriggersSetKey = redisSchema.calendarTriggersSetKey(calendarName);

    if(jedis.scard(calendarTriggersSetKey) > 0){
        throw new JobPersistenceException(String.format("There are triggers pointing to calendar %s, so it cannot be removed.", calendarName));
    }
    final String calendarHashKey = redisSchema.calendarHashKey(calendarName);
    Pipeline pipe = jedis.pipelined();
    Response<Long> deleteResponse = pipe.del(calendarHashKey);
    pipe.srem(redisSchema.calendarsSet(), calendarHashKey);
    pipe.sync();

    return deleteResponse.get() == 1;
}
 
Example 3
Source File: BceChatHandlerTest.java    From gameserver with Apache License 2.0 6 votes vote down vote up
private void registerFakeSession(int count, User user) {
	//Clean all sessions
	Jedis jedis = JedisFactory.getJedis();
	Pipeline pipeline = jedis.pipelined();
	Set<byte[]> strs = jedis.keys("*".getBytes());
	for ( byte[] key : strs ) {
		pipeline.del(key);
	}
	pipeline.sync();
	
	UserId userId = user.get_id();
	SessionKey sessionKey = SessionKey.createSessionKeyFromRandomString();
	user.setSessionKey(sessionKey);
	
	//Store it with machineid to redis
	jedis = JedisFactory.getJedis();
	pipeline = jedis.pipelined();
	for ( int i=0; i<count; i++ ) {
		pipeline.hset(sessionKey.toString(), SessionManager.H_MACHINE_KEY, "localhost:10000");
		pipeline.hset(userId.toString(), SessionManager.H_MACHINE_KEY, "localhost:10000");
		pipeline.hset(userId.toString(), SessionManager.H_SESSION_KEY, sessionKey.toString());
	}
	pipeline.sync();
}
 
Example 4
Source File: RedisTarget.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private void doDeleteRecord(Record record, List<ErrorRecord> tempRecords, Pipeline pipeline, String key)
        throws StageException {
  if (!StringUtils.isEmpty(key)) {
    pipeline.del(key);
    tempRecords.add(new ErrorRecord(record, "Delete", key, ""));
  } else {
    LOG.error(Errors.REDIS_09.getMessage(), key);
    errorRecordHandler.onError(
            new OnRecordErrorException(
                    record,
                    Errors.REDIS_09
            )
    );
  }
}
 
Example 5
Source File: JedisUtil.java    From gameserver with Apache License 2.0 5 votes vote down vote up
/**
 * Use it with caution. 
 * It delete all the keys from Redis
 */
public static final void deleteAllKeys(Jedis jedis) {
	Pipeline pipeline = jedis.pipelined();
	Set<byte[]> bytes = jedis.keys("*".getBytes());
	for ( byte[] key : bytes ) {
		pipeline.del(key);
	}
	pipeline.sync();
}
 
Example 6
Source File: JedisTest.java    From gameserver with Apache License 2.0 5 votes vote down vote up
public void testClean() {
	com.xinqihd.sns.gameserver.jedis.Jedis jedis = JedisFactory.getJedis();
	Set<String> strs = jedis.keys("*");
	Pipeline pipeline = jedis.pipelined();
	for ( String key : strs ) {
		pipeline.del(key);
	}
	pipeline.sync();
}
 
Example 7
Source File: RedisCacheService.java    From commafeed with Apache License 2.0 5 votes vote down vote up
@Override
public void setLastEntries(Feed feed, List<String> entries) {
	try (Jedis jedis = pool.getResource()) {
		String key = buildRedisEntryKey(feed);

		Pipeline pipe = jedis.pipelined();
		pipe.del(key);
		for (String entry : entries) {
			pipe.sadd(key, entry);
		}
		pipe.expire(key, (int) TimeUnit.DAYS.toSeconds(7));
		pipe.sync();
	}
}
 
Example 8
Source File: RedisCacheService.java    From commafeed with Apache License 2.0 5 votes vote down vote up
@Override
public void setUserRootCategory(User user, Category category) {
	try (Jedis jedis = pool.getResource()) {
		String key = buildRedisUserRootCategoryKey(user);

		Pipeline pipe = jedis.pipelined();
		pipe.del(key);
		pipe.set(key, MAPPER.writeValueAsString(category));
		pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30));
		pipe.sync();
	} catch (JsonProcessingException e) {
		log.error(e.getMessage(), e);
	}
}
 
Example 9
Source File: RedisCacheService.java    From commafeed with Apache License 2.0 5 votes vote down vote up
@Override
public void setUnreadCount(FeedSubscription sub, UnreadCount count) {
	try (Jedis jedis = pool.getResource()) {
		String key = buildRedisUnreadCountKey(sub);

		Pipeline pipe = jedis.pipelined();
		pipe.del(key);
		pipe.set(key, MAPPER.writeValueAsString(count));
		pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30));
		pipe.sync();
	} catch (Exception e) {
		log.error(e.getMessage(), e);
	}
}
 
Example 10
Source File: RedisCacheService.java    From commafeed with Apache License 2.0 5 votes vote down vote up
@Override
public void invalidateUserRootCategory(User... users) {
	try (Jedis jedis = pool.getResource()) {
		Pipeline pipe = jedis.pipelined();
		if (users != null) {
			for (User user : users) {
				String key = buildRedisUserRootCategoryKey(user);
				pipe.del(key);
			}
		}
		pipe.sync();
	}
}
 
Example 11
Source File: RedisCacheService.java    From commafeed with Apache License 2.0 5 votes vote down vote up
@Override
public void invalidateUnreadCount(FeedSubscription... subs) {
	try (Jedis jedis = pool.getResource()) {
		Pipeline pipe = jedis.pipelined();
		if (subs != null) {
			for (FeedSubscription sub : subs) {
				String key = buildRedisUnreadCountKey(sub);
				pipe.del(key);
			}
		}
		pipe.sync();
	}
}
 
Example 12
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 13
Source File: RedisRoomManager.java    From gameserver with Apache License 2.0 4 votes vote down vote up
/**
 * Delete a room from system. If there are users (except AI users) in room,
 * it cannot be deleted and return false. 
 * @param roomSessionKey
 * @return
 */
@Override
public boolean deleteRoomIfEmpty(Room room) {
	try {
		room.lock();

		List<UserInfo> userList = room.getUserInfoList();
		boolean emptyRoom = true;
		for ( int i=0; i<userList.size(); i++ ) {
			UserInfo userInfo = userList.get(i);
			if ( userInfo == null || userInfo == Room.BLOCKED_USER_INFO ) {
				continue;
			} else {
				UserId userId = GameContext.getInstance().findUserIdBySessionKey(userInfo.getUserSessionKey());
				if ( userId != null ) {
					emptyRoom = false;
				}
				break;
			}
		}
		if ( emptyRoom ) {
			logger.debug("Delete room {} because it is empty.", room.getRoomSessionKey());
			
			Pipeline pipeline = JedisFactory.getJedis().pipelined();
			//pipeline.multi();
			pipeline.del(room.getRoomSessionKey().toString());
			pipeline.zrem(room.getCurrentSetName(), room.getRoomSessionKey().toString());
			pipeline.srem(localRoomSetName, room.getRoomSessionKey().toString());
			//pipeline.exec();
			pipeline.sync();
			
			roomMaps.remove(room.getRoomSessionKey());
			
			room.setRoomStatus(RoomStatus.DELETED);
			return true;
		} else {
			return false;
		}
	} finally {
		room.unlock();
	}
}