本文整理汇总了Java中redis.clients.jedis.Jedis.setex方法的典型用法代码示例。如果您正苦于以下问题:Java Jedis.setex方法的具体用法?Java Jedis.setex怎么用?Java Jedis.setex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.clients.jedis.Jedis
的用法示例。
在下文中一共展示了Jedis.setex方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setRedisData
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
/**
* 向redis中存入数据
*
* @param key 键值
* @param object 数据
* @return boolean
*/
public <T> boolean setRedisData(String key, T object, int seconds) {
Jedis jedis = null;
try {
jedis = getConnect();
if (seconds > 0) {
jedis.setex(key.getBytes(), seconds, ConvertUtil.serialize(object));
} else {
jedis.set(key.getBytes(), ConvertUtil.serialize(object));
}
} catch (Exception e) {
logger.error("redis setRedisData data failed!", e);
return false;
} finally {
close(jedis);
}
return true;
}
示例2: putObject
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
public void putObject(String key, Object value, int expiration) {
Jedis jedis = null;
try {
jedis = redisConnection.getJedis();
jedis.select(dbIndex);
if (expiration > 0) {
jedis.setex(key.getBytes(), expiration, SerializeUtil.serialize(value));
} else {
jedis.set(key.getBytes(), SerializeUtil.serialize(value));
}
} catch (Exception e) {
log.warn(e.getMessage(), e);
} finally {
if (jedis != null) {
jedis.close();
}
}
}
示例3: generateAccessToken
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
/**
* 生成accessToken,返回key,并将accessToken保存到redis
*/
private String generateAccessToken(){
Jedis jedis = null;
String accessToken = null;
try{
accessToken = UUID.randomUUID().toString().replace("-", "");
jedis = JedisTemplate.getJedis();
jedis.setex(accessToken, 60, accessToken); // 60秒的超时时间
}catch(Exception e){
e.printStackTrace();
}finally{
jedis.close();
}
return accessToken;
}
示例4: acquire
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
/**
* Acquire lock.
*
* @param jedis
* @return true if lock is acquired, false acquire timeouted
* @throws InterruptedException
* in case of thread interruption
*/
public synchronized boolean acquire(Jedis jedis) throws InterruptedException {
int timeout = timeoutMsecs * 1000;
while (timeout >= 0) {
if (jedis.setnx(lockKey, lockKey) == 1) {
// lock acquired
locked = Boolean.TRUE;
jedis.setex(lockKey, expireMsecs, lockKey);
return locked;
}
timeout -= 100;
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
return false;
}
示例5: doCreate
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
/**
* SessionManager创建完session后会调用该方法
* @param session
* @return
*/
@Override
protected Serializable doCreate(Session session) {
Serializable sessionId = this.generateSessionId(session);
assignSessionId(session,sessionId);
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
//session由Redis缓存失效决定,这里作简单标识
session.setTimeout(expireTime);
jedis.setex(prefix + sessionId, expireTime, SerializeUtils.serializaToString((ShiroSession) session));
logger.info("sessionId {} name {} 被创建", sessionId, session.getClass().getName());
}catch (Exception e){
logger.warn("创建session失败",e);
}finally {
jedis.close();
}
return sessionId;
}
示例6: setex
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
/**
* 过期时间的
* @param key
* @param value
* @param timer (秒)
*/
public void setex(String key, Object value, int timer) throws Exception {
Jedis jds = null;
try {
jds = getJedis();
jds.select(0);
byte[] svalue = SerializeUtil.serialize(value);
jds.setex(key.getBytes(), timer, svalue);
} catch (Exception e) {
throw e;
} finally {
if(jds != null)
{
jds.close();
}
}
}
示例7: setex
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
/**
* 有过期时间
* @param key
* @param value
* @param timer (秒)
*/
public void setex(String key, V value, int timer) throws Exception {
Jedis jds = null;
try {
jds = redisManager.getJedis();
jds.select(0);
byte[] svalue = SerializeUtil.serialize(value);
jds.setex(key.getBytes(), timer, svalue);
} catch (Exception e) {
throw e;
} finally {
if(jds != null)
{
jds.close();
}
}
}
示例8: set
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
/**
* 设置 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) {
_log.error("Set keyex error : " + e);
}
}
示例9: put
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
@Override
public void put(String cacheKey, Object obj, int expiry) {
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.setex(cacheKey.getBytes(), expiry, getBytes(obj));
pool.returnResource(jedis);
} catch (Exception e) {
LOGGER.warn("Failed to put key in cache {0}", cacheKey, e);
pool.returnBrokenResource(jedis);
}
}
示例10: setex
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
@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;
}
示例11: addCacheWithExpire
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
public void addCacheWithExpire(String key, String value, int seconds) {
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.setex(key, seconds, value);
} catch (Exception var9) {
throw new RuntimeException(var9);
} finally {
pool.returnResourceObject(jedis);
}
}
示例12: addCacheForObject
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
public void addCacheForObject(String key, Object value, int seconds) {
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.setex(key.getBytes(),seconds, SerializeUtil.serialize(value));
} catch (Exception var9) {
throw new RuntimeException(var9);
} finally {
pool.returnResourceObject(jedis);
}
}
示例13: set
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
/**
* 设置 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);
}
}