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

The following examples show how to use redis.clients.jedis.Jedis#incr() . 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: JedisUtil.java    From scaffold-cloud with MIT License 8 votes vote down vote up
/**
 * 对某个键的值自增
 *
 * @param key          键
 * @param cacheSeconds 超时时间,0为不超时
 * @return
 * @author liboyi
 */
public static long setIncr(String key, int cacheSeconds) {
    long result = 0;
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        result = jedis.incr(key);
        if (cacheSeconds != 0) {
            jedis.expire(key, cacheSeconds);
        }
        logger.debug("set " + key + " = " + result);
    } catch (Exception e) {
        logger.warn("set " + key + " = " + result);
    } finally {
        jedisPool.returnResource(jedis);
    }
    return result;
}
 
Example 2
Source File: JedisUtil.java    From scaffold-cloud with MIT License 6 votes vote down vote up
/**
 * 递增数字
 *
 * @param key 键
 * @param by  步长
 * @return
 */
public static long incr(String key, long by) {
    long result = 0;
    Jedis jedis = null;
    try {
        jedis = getResource();
        if (by > 0) {
            result = jedis.incrBy(key, by);
        } else {
            result = jedis.incr(key);
        }

    } catch (Exception e) {
        logger.warn("incr key={}, by={}", key, by, e);
    } finally {
        close(jedis);
    }
    return result;
}
 
Example 3
Source File: UUIDServiceRedisImpl.java    From redis_util with Apache License 2.0 6 votes vote down vote up
public Long fetchUUID(String key, Integer length, Boolean haveDay) {
    Jedis jedis = null;
    try {
        jedis = redisConnection.getJedis();
        jedis.select(dbIndex);
        Calendar now = new GregorianCalendar();
        Long num = jedis.incr(key);

        if (haveDay) {
            String day = df.format(now.getTime());
            return createUUID(num, day, length);
        } else {
            return num;
        }
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
}
 
Example 4
Source File: UUIDServiceRedisImpl.java    From redis_util with Apache License 2.0 6 votes vote down vote up
public Long fetchDailyUUID(String key, Integer length, Boolean haveDay) {
    Jedis jedis = null;
    try {
        jedis = redisConnection.getJedis();
        jedis.select(dbIndex);
        Calendar now = new GregorianCalendar();
        String day = df.format(now.getTime());
        key = key + "_" + day;
        Long num = jedis.incr(key);
        //设置 key 过期时间
        if (num == 1) {
            jedis.expire(key, (24 - now.get(Calendar.HOUR_OF_DAY)) * 3600 + 1800);
        }
        if (haveDay) {
            return createUUID(num, day, length);
        } else {
            return num;
        }
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
}
 
Example 5
Source File: RedisServiceImpl.java    From ace-cache with Apache License 2.0 5 votes vote down vote up
@Override
public Long incr(String key) {
    Jedis jedis = null;
    Long res = null;
    try {
        jedis = pool.getResource();
        res = jedis.incr(key);
    } catch (Exception e) {

        LOGGER.error(e.getMessage());
    } finally {
        returnResource(pool, jedis);
    }
    return res;
}
 
Example 6
Source File: TestJedis.java    From jframe with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadConf() throws Exception {
    Jedis j = redis.getJedis("t1");
    long l = j.incr("test.pay");
    System.out.println(l);
    j.del("test.pay");

    j = redis.getJedis();
    l = j.incr("test.pay.d");
    System.out.println(l);
    j.del("test.pay.d");

    j.close();
}
 
Example 7
Source File: ComputeController.java    From micro-service with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/add" ,method = RequestMethod.GET)
    public String add(@RequestParam Integer a, @RequestParam Integer b) {
    	
//        num.incrementAndGet();
//        
//        if (num.get() <= flag) {
//	        ServiceInstance instance = client.getLocalServiceInstance();
//	        Integer r = a + b;
//	        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
//	        return "From Service-B, Result is " + r+"\nPort:"+instance.getPort();
//        }
//        return "调用次数超限,一分钟内最多只能调用10次!";
    	InterfaceLimit limit = service.getEntityByPri(1);
    	Jedis jedis = RedisUtils.getJedis();
    	
    	//redis存的超时时间
    	String timeRound_1 = jedis.get("timeRound_1");
    	//如果不存在或者是不等于数据库设置值
    	if (timeRound_1 == null || !limit.getUnitTime().toString().equals(timeRound_1)) {
    		//重新设置超时时间
			jedis.set("timeRound_1", limit.getUnitTime().toString());
			jedis.expire("num_1", limit.getUnitTime());
		}
    	String num_1 = jedis.get("num_1");
    	if (num_1 == null) {
    		jedis.set("num_1", String.valueOf(0));
			jedis.expire("num_1", limit.getUnitTime());
		}
    	
    	jedis.incr("num_1");
        
        if (Integer.parseInt(jedis.get("num_1")) <= limit.getUnitNum()) {
	        ServiceInstance instance = client.getLocalServiceInstance();
	        Integer r = a + b;
	        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
	        return "From Service-B, Result is " + r+"\nPort:"+instance.getPort();
        }
        return "调用次数超限!";
    }
 
Example 8
Source File: BaseDockerComposeTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void simpleTest() {
    Jedis jedis = new Jedis(getEnvironment().getServiceHost("redis_1", REDIS_PORT), getEnvironment().getServicePort("redis_1", REDIS_PORT));

    jedis.incr("test");
    jedis.incr("test");
    jedis.incr("test");

    assertEquals("A redis instance defined in compose can be used in isolation", "3", jedis.get("test"));
}
 
Example 9
Source File: JedisClientPool.java    From paas with Apache License 2.0 5 votes vote down vote up
@Override
public Long incr(String key) {
    Jedis jedis = jedisPool.getResource();
    Long result = jedis.incr(key);
    jedis.close();
    return result;
}
 
Example 10
Source File: RedisUtil.java    From zhcc-server with Apache License 2.0 5 votes vote down vote up
/**
 * incr
 * @param key
 * @return value
 */
public synchronized static Long incr(String key) {
	Jedis jedis = getJedis();
	if (null == jedis) {
		return null;
	}
	long value = jedis.incr(key);
	jedis.close();
	return value;
}
 
Example 11
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void checkPoolRepairedWhenJedisIsBroken() {
  JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort());
  Jedis jedis = pool.getResource();
  jedis.auth("foobared");
  jedis.quit();
  jedis.close();

  jedis = pool.getResource();
  jedis.auth("foobared");
  jedis.incr("foo");
  jedis.close();
  pool.destroy();
  assertTrue(pool.isClosed());
}
 
Example 12
Source File: JedisClientPool.java    From BigDataPlatform with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Long incr(String key) {
	Jedis jedis = jedisPool.getResource();
	Long result = jedis.incr(key);
	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 Long incr(String key) {
    Jedis jedis = jedisPool.getResource();
    Long result = jedis.incr(key);
    jedis.close();
    return result;
}
 
Example 14
Source File: AbstractRedisTest.java    From x-pipe with Apache License 2.0 5 votes vote down vote up
protected void sendRandomMessage(RedisMeta redisMeta, int count, int messageLength) {

        Jedis jedis = createJedis(redisMeta);
        logger.info("[sendRandomMessage][begin]{}", redisMeta.desc());
        for (int i = 0; i < count; i++) {

            long currentIndex = totalSendMessageCount.incrementAndGet();
            jedis.set(String.valueOf(currentIndex), randomString(messageLength));
            jedis.incr("incr");
        }
        logger.info("[sendRandomMessage][ end ]{}", redisMeta.desc());
    }
 
Example 15
Source File: RedisClient.java    From springboot-learn with MIT License 5 votes vote down vote up
public Long incr(final String key) {
    Jedis jedis = null;
    try {
        jedis = getJedis();
        return jedis.incr(key);
    } catch (Exception e) {
        logger.error("[RedisClient] incr e,", e);
        throw new IllegalArgumentException("加1异常");
    } finally {
        close(jedis);
    }
}
 
Example 16
Source File: TestJedis.java    From jframe with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpire() throws Exception {
    String key = "test.pay.expire";
    Jedis j = redis.getJedis();
    long l = j.incr(key);
    System.out.println(l);
    System.out.println(j.get(key));
    j.expire(key, 2);
    Thread.sleep(4 * 1000);
    System.out.println(j.get(key));

    j.close();
}
 
Example 17
Source File: RedisPoolUtil.java    From seconds-kill with MIT License 5 votes vote down vote up
/**
 * key - value 自增
 */
public static Long incr (String key) {
    Jedis jedis = null;
    Long result = null;
    try {
        jedis = RedisPool.getJedis();
        result = jedis.incr(key);
    } catch (Exception e) {
        log.error("listGet key:{} error", key, e);
    } finally {
        RedisPool.jedisPoolClose(jedis);
    }
    return result;
}
 
Example 18
Source File: JedisUtils.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * 通过key 对value进行加值+1操作,当value不是int类型时会返回错误,当key不存在是则value为1
 * </p>
 *
 * @param key
 * @return 加值后的结果
 */
public Long incr(String key) {
    Jedis jedis = null;
    Long res = null;
    try {
        jedis = jedisPool.getResource();
        res = jedis.incr(key);
    } catch (Exception e) {

        log.error(e.getMessage());
    } finally {
        returnResource(jedisPool, jedis);
    }
    return res;
}
 
Example 19
Source File: ObjectCacheOperate.java    From xian with Apache License 2.0 4 votes vote down vote up
public static long incr(Jedis jedis, String key) {
    return jedis.incr(key);
}
 
Example 20
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private Long incr0(Jedis j, String key) {
	return j.incr(key);
}