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

The following examples show how to use redis.clients.jedis.Jedis#setex() . 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: RedisService.java    From seckill with Apache License 2.0 8 votes vote down vote up
/**
 * 设置对象
 *
 * @param prefix
 * @param key
 * @param value
 * @param <T>
 * @return
 */
public <T> boolean set(KeyPrefix prefix, String key, T value) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        String str = beanToString(value);
        if (str == null || str.length() < 0) {
            return false;
        }
        //生成真正的key
        String realKey = prefix.getPrefix() + key;
        int seconds = prefix.expireSeconds();
        if (seconds < 1) {
            jedis.set(realKey, str);
        } else {
            jedis.setex(realKey, seconds, str);
        }

        return true;
    } finally {
        returnToPool(jedis);
    }
}
 
Example 2
Source File: RedisDao.java    From imooc-seckill with MIT License 6 votes vote down vote up
public String putSeckill(Seckill seckill) {
    // set Object(Seckill) -> 序列化 ->byte[]
    try {
        Jedis jedis = jedisPool.getResource();
        try {
            String key = "seckill:" + seckill.getSeckillId();
            byte[] bytes = ProtobufIOUtil.toByteArray(seckill, schema,
                    LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
            //超时缓存
            int timeout = 60 * 60;
            String result = jedis.setex(key.getBytes(), timeout, bytes);
            return result;
        } finally {
            jedis.close();
        }

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    return null;
}
 
Example 3
Source File: RedisContainer.java    From bahir-flink with Apache License 2.0 6 votes vote down vote up
@Override
public void setex(final String key, final String value, final Integer ttl) {
    Jedis jedis = null;
    try {
        jedis = getInstance();
        jedis.setex(key, ttl, value);
    } catch (Exception e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Cannot send Redis message with command SETEX to key {} error message {}",
                    key, e.getMessage());
        }
        throw e;
    } finally {
        releaseInstance(jedis);
    }
}
 
Example 4
Source File: RightCheck.java    From x-pipe with Apache License 2.0 6 votes vote down vote up
private void test(String key, String address) throws InterruptedException {
	
	logger.info("=========[test]{},{}=========", key, address);
	String []sp = address.split("\\s*(;|,)\\s*");
	
	String testKey = "xpipe-test-key";
	String testValue = UUID.randomUUID().toString();
	
	Jedis master = getJedis(sp[0]);
	logger.info("[test][write to master]{}, {}, {}", sp[0], key, address);
	master.setex(testKey, expireTime, testValue);
	
	TimeUnit.MILLISECONDS.sleep(100);
	
	for(int i=1; i < sp.length; i++){
		
		logger.info("[test][test slave]{}", sp[i]);
		
		Jedis slave = getJedis(sp[i]);
		String realValue = slave.get(testKey);
		if(!testValue.equals(realValue)){
			logger.error("[not equal]{}, {}, {} but: {}", sp[i], testKey, testValue, realValue);
		}
	}
	
}
 
Example 5
Source File: JedisCacheDriver.java    From rebuild with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void putx(String key, V value, int seconds) {
	Jedis jedis = null;
	try {
		jedis = jedisPool.getResource();

		byte[] bkey = key.getBytes();
		if (seconds > 0) {
   			jedis.setex(bkey, seconds, SerializationUtils.serialize(value));
           } else {
   			jedis.set(bkey, SerializationUtils.serialize(value));
           }
	} finally {
		IOUtils.closeQuietly(jedis);
	}
}
 
Example 6
Source File: RedisPoolUtilBAK.java    From mmall20180107 with Apache License 2.0 6 votes vote down vote up
public static String setEx(String key,String value,int extTime){

        Jedis jedis = null;
        String result = null;

        try {
            jedis = RedisPool.getJedis();
            result = jedis.setex(key,extTime,value);
        } catch (Exception e) {
            log.error("setex key:{} value:{} error",key,value,e);
            RedisPool.returnBrokenResource(jedis);
            return result;

        }

        RedisPool.returnResource(jedis);
        return result;

    }
 
Example 7
Source File: JedisUtils.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * 新增key,并将 key 的生存时间 (以秒为单位)
 * </p>
 *
 * @param key
 * @param seconds
 *            生存时间 单位:秒
 * @param value
 * @return 设置成功时返回 OK 。当 seconds 参数不合法时,返回一个错误。
 */
public String setex(String key, int seconds, String value) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        return jedis.setex(key, seconds, value);
    } catch (Exception e) {

        log.error(e.getMessage());
    } finally {
        returnResource(jedisPool, jedis);
    }
    return null;
}
 
Example 8
Source File: JedisCache.java    From charging_pile_cloud with MIT License 5 votes vote down vote up
public static String setStrex(int dbIndex, String key, String value, int seconds) {
    String result = null;
    Jedis jedis = null;
    try {
        jedis = jedisManager.getJedis();
        jedis.select(dbIndex);
        result = jedis.setex(key, seconds, value);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        jedis.close();
    }

    return result;
}
 
Example 9
Source File: RedisUtil.java    From zheng with MIT License 5 votes vote down vote up
/**
 * 设置 String 过期时间
 * @param key
 * @param value
 * @param seconds 以秒为单位
 */
public synchronized static void set(String key, String value, int seconds) {
	try {
		value = StringUtils.isBlank(value) ? "" : value;
		Jedis jedis = getJedis();
		jedis.setex(key, seconds, value);
		jedis.close();
	} catch (Exception e) {
		LOGGER.error("Set keyex error : " + e);
	}
}
 
Example 10
Source File: RedisClient.java    From heisenberg with Apache License 2.0 5 votes vote down vote up
/**
 * 保存一个小时
 * 
 * @param key
 * @param status
 */
public static void put(String key, String status) {
    String rKey = keyPrefix + key;
    Jedis jedis = getResource(rKey);
    try {
        jedis.setex(key, 600, status + "," + System.currentTimeMillis());
    } catch (Exception e) {
        logger.error("保存xa id" + rKey + ",s:" + status + "异常", e);
        throw new RuntimeException(e);
    } finally {
        recycleResource(jedis, rKey);
    }
}
 
Example 11
Source File: RedisServiceImpl.java    From ace-cache with Apache License 2.0 5 votes vote down vote up
@Override
public String setex(String key, String value, int seconds) {
    Jedis jedis = null;
    String res = null;
    try {
        jedis = pool.getResource();
        res = jedis.setex(key, seconds, value);
    } catch (Exception e) {

        LOGGER.error(e.getMessage());
    } finally {
        returnResource(pool, jedis);
    }
    return res;
}
 
Example 12
Source File: RedisUtil.java    From shop with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 存放一个定时过期的对象
 *
 * @param key
 * @param value
 * @param timeout
 * @return
 */
public String putObjectEx(String key, T value, int timeout) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        return jedis.setex(key, timeout, JSON.toJSONString(value));
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
}
 
Example 13
Source File: TokenIssue.java    From netty.book.kor with MIT License 5 votes vote down vote up
public void service() throws ServiceException {
    Jedis jedis = null;
    try {
        Map<String, Object> result = sqlSession.selectOne("users.userInfoByPassword", this.reqData);

        if (result != null) {
            final long threeHour = 60 * 60 * 3;
            long issueDate = System.currentTimeMillis() / 1000;
            String email = String.valueOf(result.get("USERID"));

            // token 만들기.
            JsonObject token = new JsonObject();
            token.addProperty("issueDate", issueDate);
            token.addProperty("expireDate", issueDate + threeHour);
            token.addProperty("email", email);
            token.addProperty("userNo", reqData.get("userNo"));

            // token 저장.
            KeyMaker tokenKey = new TokenKey(email, issueDate);
            jedis = helper.getConnection();
            jedis.setex(tokenKey.getKey(), 60 * 60 * 3, token.toString());

            // helper.
            this.apiResult.addProperty("resultCode", "200");
            this.apiResult.addProperty("message", "Success");
            this.apiResult.addProperty("token", tokenKey.getKey());
        }
        else {
            // 데이터 없음.
            this.apiResult.addProperty("resultCode", "404");
        }
    }
    catch (Exception e) {
        helper.returnResource(jedis);
    }
}
 
Example 14
Source File: RedisUtil.java    From shop with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 存放一条定时过期的数据
 *
 * @param key
 * @param value
 * @param timeout
 * @return
 */
public String putEx(String key, String value, int timeout) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        return jedis.setex(key, timeout, value);
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
}
 
Example 15
Source File: ShardedJedisCacheManager.java    From AutoLoadCache with Apache License 2.0 4 votes vote down vote up
@Override
public void setex(byte[] key, int seconds, byte[] value) {
    Jedis jedis = shardedJedis.getShard(key);
    jedis.setex(key, seconds, value);
}
 
Example 16
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private String setex0(Jedis j, String key, int seconds, String value) {
	return j.setex(key, seconds, value);
}
 
Example 17
Source File: JedisUtil.java    From BigData with GNU General Public License v3.0 3 votes vote down vote up
/**
 * 添加有过期时间的记录
 * 
 * @param String
 *            key
 * @param int seconds 过期时间,以秒为单位
 * @param String
 *            value
 * @return String 操作状态
 * */
public String setEx(byte[] key, int seconds, byte[] value) {
	Jedis jedis = getJedis();
	String str = jedis.setex(key, seconds, value);
	returnJedis(jedis);
	return str;
}
 
Example 18
Source File: JedisUtil.java    From BigData with GNU General Public License v3.0 3 votes vote down vote up
/**
 * 添加有过期时间的记录
 * 
 * @param String
 *            key
 * @param int seconds 过期时间,以秒为单位
 * @param String
 *            value
 * @return String 操作状态
 * */
public String setEx(String key, int seconds, String value) {
	Jedis jedis = getJedis();
	String str = jedis.setex(key, seconds, value);
	returnJedis(jedis);
	return str;
}
 
Example 19
Source File: JedisUtil.java    From Project with Apache License 2.0 3 votes vote down vote up
/**
 * 添加有过期时间的记录
 * 
 * @param  key
 * @param  seconds 过期时间,以秒为单位
 * @param value
 * @return String 操作状态
 */
public String setEx(byte[] key, int seconds, byte[] value) {
	Jedis jedis = getJedis();
	String str = jedis.setex(key, seconds, value);
	jedis.close();
	return str;
}
 
Example 20
Source File: JedisUtil.java    From Project with Apache License 2.0 3 votes vote down vote up
/**
 * 添加有过期时间的记录
 * 
 * @param  key
 * @param  seconds 过期时间,以秒为单位
 * @param  value
 * @return String 操作状态
 */
public String setEx(String key, int seconds, String value) {
	Jedis jedis = getJedis();
	String str = jedis.setex(key, seconds, value);
	jedis.close();
	return str;
}