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

The following examples show how to use redis.clients.jedis.Pipeline#sync() . 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
/**
 * Get the current state of the identified <code>{@link org.quartz.Trigger}</code>.
 * @param triggerKey the key of the desired trigger
 * @param jedis a thread-safe Redis connection
 * @return the state of the trigger
 */
@Override
public Trigger.TriggerState getTriggerState(TriggerKey triggerKey, Jedis jedis){
    final String triggerHashKey = redisSchema.triggerHashKey(triggerKey);
    Pipeline pipe = jedis.pipelined();
    Map<RedisTriggerState, Response<Double>> scores = new HashMap<>(RedisTriggerState.values().length);
    for (RedisTriggerState redisTriggerState : RedisTriggerState.values()) {
        scores.put(redisTriggerState, pipe.zscore(redisSchema.triggerStateKey(redisTriggerState), triggerHashKey));
    }
    pipe.sync();
    for (Map.Entry<RedisTriggerState, Response<Double>> entry : scores.entrySet()) {
        if(entry.getValue().get() != null){
            return entry.getKey().getTriggerState();
        }
    }
    return Trigger.TriggerState.NONE;
}
 
Example 2
Source File: RedisEventLog.java    From concursus with MIT License 6 votes vote down vote up
private Collection<Event> writeMultipleEvents(Collection<Event> events) {
    Map<AggregateId, List<Event>> eventsById = events.stream()
            .map(event -> event.processed(TimeUUID.timeBased()))
            .collect(groupingBy(Event::getAggregateId));

    if (eventsById.size() == 1) {
        eventsById.forEach(this::writeEventsForId);
    } else {
        final Pipeline pipeline = jedis.pipelined();

        eventsById.forEach((id, eventsForId) ->
                writeEventsForId(pipeline, id, eventsForId));

        pipeline.sync();
    }

    return eventsById.values().stream().flatMap(List::stream).collect(toList());
}
 
Example 3
Source File: JedisPoolStream.java    From RedisDirectory with Apache License 2.0 6 votes vote down vote up
@Override
public void saveFile(String fileLengthKey, String fileDataKey, String fileName, List<byte[]> values, long fileLength) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        Pipeline pipelined = jedis.pipelined();
        pipelined.hset(fileLengthKey.getBytes(), fileName.getBytes(), Longs.toByteArray(fileLength));
        Long blockSize = getBlockSize(fileLength);
        for (int i = 0; i < blockSize; i++) {
            pipelined.hset(fileDataKey.getBytes(), getBlockName(fileName, i), compressFilter(values.get(i)));
            if (i % Constants.SYNC_COUNT == 0) {
                pipelined.sync();
                pipelined = jedis.pipelined();
            }
        }
        values.clear();
        pipelined.sync();
    } finally {
        jedis.close();
    }
}
 
Example 4
Source File: JedisPoolStream.java    From RedisDirectory with Apache License 2.0 6 votes vote down vote up
@Override
public void rename(String fileLengthKey, String fileDataKey, String oldField, String newField, List<byte[]> values, long
        fileLength) {
    long blockSize = 0;
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        Pipeline pipelined = jedis.pipelined();
        //add new file length
        pipelined.hset(fileLengthKey.getBytes(), newField.getBytes(), Longs.toByteArray(fileLength));
        //add new file content
        blockSize = getBlockSize(fileLength);
        for (int i = 0; i < blockSize; i++) {
            pipelined.hset(fileDataKey.getBytes(), getBlockName(newField, i), compressFilter(values.get(i)));
        }
        values.clear();
        pipelined.sync();
    } finally {
        jedis.close();
        deleteFile(fileLengthKey, fileDataKey, oldField, blockSize);
    }
}
 
Example 5
Source File: JedisStream.java    From RedisDirectory with Apache License 2.0 6 votes vote down vote up
@Override
public void saveFile(String fileLengthKey, String fileDataKey, String fileName, List<byte[]> values, long fileLength) {
    Jedis jedis = openJedis();
    Pipeline pipelined = jedis.pipelined();
    pipelined.hset(fileLengthKey.getBytes(), fileName.getBytes(), Longs.toByteArray(fileLength));
    Long blockSize = getBlockSize(fileLength);
    for (int i = 0; i < blockSize; i++) {
        pipelined.hset(fileDataKey.getBytes(), getBlockName(fileName, i), compressFilter(values.get(i)));
        if (i % Constants.SYNC_COUNT == 0) {
            pipelined.sync();
            pipelined = jedis.pipelined();
        }
    }
    pipelined.sync();
    jedis.close();
    values.clear();
}
 
Example 6
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void testEvalshaKeyAndArg() {
  String key = "test";
  String arg = "3";
  String script = "redis.call('INCRBY', KEYS[1], ARGV[1]) redis.call('INCRBY', KEYS[1], ARGV[1])";
  String sha1 = jedis.scriptLoad(script);

  assertTrue(jedis.scriptExists(sha1));

  Pipeline p = jedis.pipelined();
  p.set(key, "0");
  Response<Object> result0 = p.evalsha(sha1, Arrays.asList(key), Arrays.asList(arg));
  p.incr(key);
  Response<Object> result1 = p.evalsha(sha1, Arrays.asList(key), Arrays.asList(arg));
  Response<String> result2 = p.get(key);
  p.sync();

  assertNull(result0.get());
  assertNull(result1.get());
  assertEquals("13", result2.get());
}
 
Example 7
Source File: JedisStream.java    From RedisDirectory with Apache License 2.0 6 votes vote down vote up
@Override
public void saveFile(String fileLengthKey, String fileDataKey, String fileName, List<byte[]> values, long fileLength) {
    Jedis jedis = openJedis();
    Pipeline pipelined = jedis.pipelined();
    pipelined.hset(fileLengthKey.getBytes(), fileName.getBytes(), Longs.toByteArray(fileLength));
    Long blockSize = getBlockSize(fileLength);
    for (int i = 0; i < blockSize; i++) {
        pipelined.hset(fileDataKey.getBytes(), getBlockName(fileName, i), compressFilter(values.get(i)));
        if (i % Constants.SYNC_COUNT == 0) {
            pipelined.sync();
            pipelined = jedis.pipelined();
        }
    }
    pipelined.sync();
    jedis.close();
    values.clear();
}
 
Example 8
Source File: JedisServiceDemo.java    From spring-redis-plugin with Apache License 2.0 5 votes vote down vote up
@Redis
public void incr(String key, int times) {
    Pipeline pipeline = jedis.pipelined();
    pipeline.set(key, "1");
    for (int i = 0; i < times; i++) {
        pipeline.incr(key + i);
    }
    Response<String> response = pipeline.get(key + 1);
    pipeline.sync();
    LOGGER.info(response.get());
    jedis.del(key);
}
 
Example 9
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvalWithBinary() {
  String script = "return 'success!'";

  Pipeline p = jedis.pipelined();
  Response<Object> result = p.eval(SafeEncoder.encode(script));
  p.sync();

  assertArrayEquals(SafeEncoder.encode("success!"), (byte[]) result.get());
}
 
Example 10
Source File: RedisPipleTest.java    From javabase with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
        jedis.auth("gaoguangjin");
        jedis.select(2);
//        Pipeline p = jedis.pipelined();
//        for(int i=0;i<3000;i++){
//            p.set("a"+i,i+"");
//        }
//        p.sync();

        long beginTime=System.currentTimeMillis();
//        Pipeline p = jedis.pipelined();
//        Response<Map<String, String>> dd = p.hgetAll("tieba_content_image_黄河科技学院");
//        p.sync();
//        Map<String, String> map=dd.get();
        Set<String> key = jedis.hkeys("tieba_content_image_黄河科技学院");
        System.out.println("耗时:"+(System.currentTimeMillis()-beginTime)+"s");
        Pipeline p = jedis.pipelined();
        List<Response<String>> lis=new ArrayList<>();
        for (String s : key) {
            beginTime=System.currentTimeMillis();
            lis.add( p.hget("tieba_content_image_黄河科技学院", s));
            System.out.println("耗时:"+(System.currentTimeMillis()-beginTime)+"s");
        }
        p.sync();
//        List<Response<String>> lis=new ArrayList<>();
//        Pipeline p = jedis.pipelined();
//        for(int i=0;i<3000;i++){
//            lis.add( p.get("a" + i));
//        }
//        p.sync();
        System.out.println(lis.size()+"耗时:"+(System.currentTimeMillis()-beginTime)+"s");
    }
 
Example 11
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void canRetrieveUnsetKey() {
  Pipeline p = jedis.pipelined();
  Response<String> shouldNotExist = p.get(UUID.randomUUID().toString());
  p.sync();
  assertNull(shouldNotExist.get());
}
 
Example 12
Source File: JedisTest.java    From gameserver with Apache License 2.0 5 votes vote down vote up
public void testLongSetPerformance1_2() {
	JedisAdapter jedis = new JedisAdapter(host, port, config);

	//Store a long list
	int count = 500000;
	Set<byte[]> userList = jedis.keys("key_*".getBytes());
	for ( byte[] key : userList ) {
		jedis.del(key);
	}
	
	byte[][] longList = createLongList(count);
	Pipeline pipeline = jedis.pipelined();
	for ( int i=0; i<longList.length; i++ ) {
		pipeline.set(("key_"+r.nextInt()).getBytes(), longList[i]);
	}
	pipeline.sync();
	userList = jedis.keys("key_*".getBytes());
	assertTrue(userList.size()>0);
	
	//Getting the list.
	long startM = 0l, endM = 0l;
	startM = System.currentTimeMillis();
	int max=10;
	for ( int i=0; i<max; i++ ) {
		userList = jedis.keys("key_*".getBytes());
	}
	endM = System.currentTimeMillis();
	System.out.println("Redis keypattern:"+ userList.size() + " loop " + max + " perform: " + (endM-startM)/max);
}
 
Example 13
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 14
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiscardInPipeline() {
  Pipeline pipeline = jedis.pipelined();
  pipeline.multi();
  pipeline.set("foo", "bar");
  Response<String> discard = pipeline.discard();
  Response<String> get = pipeline.get("foo");
  pipeline.sync();
  discard.get();
  get.get();
}
 
Example 15
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testReuseJedisWhenPipelineIsEmpty() {
  Pipeline pipeline = jedis.pipelined();
  pipeline.set("foo", "3");
  pipeline.sync();
  String result = jedis.get("foo");
  assertEquals(result, "3");
}
 
Example 16
Source File: JedisTest.java    From gameserver with Apache License 2.0 5 votes vote down vote up
public void testLongSetPerformance1_1() {
	JedisAdapter jedis = new JedisAdapter(host, port, config);

	//Store a long list
	int count = 500000;
	byte[] key = "longlist".getBytes();
	jedis.del(key);
	
	byte[][] longList = createLongList(count);
	Pipeline pipeline = jedis.pipelined();
	for ( int i=0; i<longList.length; i++ ) {
		pipeline.sadd(key, longList[i]);
	}
	pipeline.sync();
	Set<byte[]> userList = jedis.smembers(key);
	assertTrue(userList.size()>0);
	
	//Getting the list.
	long startM = 0l, endM = 0l;
	startM = System.currentTimeMillis();
	int max = 10;
	for ( int i=0; i<max; i++ ) {
		userList = jedis.smembers(key);
	}
	endM = System.currentTimeMillis();
	System.out.println("Redis longset:"+userList.size()+" loop " + max + " perform: " + (endM-startM)/max);
}
 
Example 17
Source File: DefaultCacheManager.java    From FATE-Serving with Apache License 2.0 5 votes vote down vote up
private void putIntoRedisCache(String cacheKey, CacheValueConfig cacheValueConfig, Object returnResult) {
    try (Jedis jedis = jedisPool.getResource()) {
        Pipeline redisPipeline = jedis.pipelined();
        redisPipeline.select(cacheValueConfig.getDbIndex());
        redisPipeline.set(cacheKey, JSON.toJSONString(returnResult));
        redisPipeline.expire(cacheKey, cacheValueConfig.getTtl());
        redisPipeline.sync();


    }
}
 
Example 18
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 19
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 20
Source File: TaskManager.java    From gameserver with Apache License 2.0 4 votes vote down vote up
/**
	 * An user finish a task. The task id will be removed from todo set
	 * and added to finished set.
	 * 
	 * @param user
	 * @param taskId
	 * @return
	 */
	public boolean finishTask(User user, String taskId) {
		//Get the persistent Jedis instance
		boolean result = true;
		Jedis jedisDB = JedisFactory.getJedisDB();
		String todoSet = getTodoSetName(user);
		String finishedSet = getFinishedSetName(user);
		
		//Delete it from todo list and add it to finish list
		Pipeline pipeline = jedisDB.pipelined();
		pipeline.srem(todoSet, taskId);
		pipeline.sadd(finishedSet, taskId);
		pipeline.sync();
		
		//Delete all data related to the task
		deleteTaskData(user, taskId);
				
		//Remove from user's tasklist if exist
		TaskPojo task = this.getTaskById(taskId);
		user.removeTask(task);
		user.addTaskFinished(task);
		
		if ( task.getType() == TaskType.TASK_ACHIVEMENT ) {
			this.takeTaskReward(user, taskId, 0);
			if ( task.isBroadcast() ) {
				String roleName = UserManager.getDisplayRoleName(user.getRoleName());
				String content = Text.text("notice.achievement", roleName, task.getName());
				ChatManager.getInstance().processChatToWorldAsyn(null, content);
			}
		} else {
			boolean success = false;
			if ( task.getType() == TaskType.TASK_ACTIVITY && 
					StringUtil.checkNotEmpty(task.getGiftDesc()) ) {
				success = this.takeTaskReward(user, taskId, 0);
			}
//			if ( success && task.isBroadcast() ) {
//				String roleName = UserManager.getDisplayRoleName(user.getRoleName());
//				String content = Text.text("notice.task", roleName, task.getName());
//				ChatManager.getInstance().processChatToWorldAsyn(null, content);
//			}
		}
//		boolean alreadyFinished = jedisDB.sismember(finishedSet, taskId);
//		if ( alreadyFinished ) {	
//			logger.debug("#finishTask: The task(id:{}) is already finished.", taskId);
//			return false;
//		} else {
//
//		}
		
		//Send the BseModiTask to client
		//Move this block of code the script.task.Step#step() method
		//wangqi 2012-02-09
		/*
		BseModiTask.Builder modiBuilder = BseModiTask.newBuilder();
		modiBuilder.setTaskID(StringUtil.toInt(taskId, 0));
		modiBuilder.setStep(task.getStep());
		XinqiMessage xinqiMsg = new XinqiMessage();
		xinqiMsg.payload = modiBuilder.build();
		
		GameContext.getInstance().writeResponse(user.getSession(), xinqiMsg);
		*/
		
		logger.debug("User {} finish the task(id:{}).", user.getRoleName(), taskId);
		
		return result;
	}