本文整理汇总了Java中redis.clients.jedis.Jedis类的典型用法代码示例。如果您正苦于以下问题:Java Jedis类的具体用法?Java Jedis怎么用?Java Jedis使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Jedis类属于redis.clients.jedis包,在下文中一共展示了Jedis类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: listObjectAdd
import redis.clients.jedis.Jedis; //导入依赖的package包/类
/**
* 向List缓存中添加值
* @param key 键
* @param value 值
* @return
*/
public static long listObjectAdd(String key, Object... value) {
long result = 0;
Jedis jedis = null;
try {
jedis = getResource();
List<byte[]> list = Lists.newArrayList();
for (Object o : value){
list.add(toBytes(o));
}
result = jedis.rpush(getBytesKey(key), (byte[][])list.toArray());
logger.debug("listObjectAdd {} = {}", key, value);
} catch (Exception e) {
logger.warn("listObjectAdd {} = {}", key, value, e);
} finally {
returnResource(jedis);
}
return result;
}
示例2: zScore
import redis.clients.jedis.Jedis; //导入依赖的package包/类
/** {@inheritDoc} */
@Override
public Double zScore(String key, Object data) {
int tries = 0;
boolean sucess = false;
Double retVal = null;
do {
tries++;
try {
Jedis jedis = pool.getResource();
retVal = jedis.zscore(key, SerializationUtil.serialize(data));
jedis.close();
sucess = true;
} catch (JedisConnectionException ex) {
log.error(RedisConstants.CONN_FAILED_RETRY_MSG + tries);
if (tries == numRetries) {
throw ex;
}
}
} while (!sucess && tries <= numRetries);
return retVal;
}
示例3: hgetAllObjects
import redis.clients.jedis.Jedis; //导入依赖的package包/类
@Override
public <T> Map<String, T> hgetAllObjects(final String bizkey, final String nameSpace, final TypeReference<T> type,
final GetDataCallBack<T> gbs) {
final String key = CacheUtils.getKeyByNamespace(bizkey, nameSpace);
return this.performFunction(key, new CallBack<Map<String, T>>() {
public Map<String, T> invoke(Jedis jedis) {
try {
Map<String, String> all = jedis.hgetAll(key);
Map<String, T> allObjs = new HashMap<String, T>();
for (Entry<String, String> item : all.entrySet()) {
String _key = item.getKey();
T _value = CacheUtils.parseObject(key, item.getValue(), type);
allObjs.put(_key, _value);
}
return allObjs;
} catch (Exception e) {
logger.error("key:" + key + "hgetAllObjects Exception:" + e.getMessage());
}
return null;
}
});
}
示例4: getLenBySet
import redis.clients.jedis.Jedis; //导入依赖的package包/类
/**
* 获取Set长度
* @param setKey
* @return
*/
public Long getLenBySet(String setKey) throws Exception {
Jedis jds = null;
try {
jds = getJedis();
jds.select(0);
Long result = jds.scard(setKey);
return result;
} catch (Exception e) {
throw e;
} finally {
if(jds != null)
{
jds.close();
}
}
}
示例5: expireat
import redis.clients.jedis.Jedis; //导入依赖的package包/类
@Override
public boolean expireat(String key, String val, int cacheTime) {
Jedis jedis = getJedisPool().getResource();
try {
jedis.set(key, val);
if (cacheTime > 0) {
jedis.expire(key, cacheTime);
}
return true;
} catch (Exception e) {
logger.error("expireat ", e);
return false;
} finally {
getJedisPool().returnResource(jedis);
}
}
示例6: hget
import redis.clients.jedis.Jedis; //导入依赖的package包/类
public String hget(String key, String field) {
Jedis jedis = null;
boolean success = true;
String ret = null;
try {
jedis = jedisPool.getResource();
if (jedis == null) {
success = false;
return ret;
}
ret = jedis.hget(key, field);
} catch (Exception e) {
success = false;
returnBrokenResource(jedis, "hmgetString" + key, e);
} finally {
releaseReidsSource(success, jedis);
}
return ret;
}
示例7: getActiveSessions
import redis.clients.jedis.Jedis; //导入依赖的package包/类
/**
* 获取会话列表
* @param offset
* @param limit
* @return
*/
public Map getActiveSessions(int offset, int limit) {
Map sessions = new HashMap();
Jedis jedis = RedisUtil.getJedis();
// 获取在线会话总数
long total = jedis.llen(ZHENG_UPMS_SERVER_SESSION_IDS);
// 获取当前页会话详情
List<String> ids = jedis.lrange(ZHENG_UPMS_SERVER_SESSION_IDS, offset, (offset + limit - 1));
List<Session> rows = new ArrayList<>();
for (String id : ids) {
String session = RedisUtil.get(ZHENG_UPMS_SHIRO_SESSION_ID + "_" + id);
// 过滤redis过期session
if (null == session) {
RedisUtil.lrem(ZHENG_UPMS_SERVER_SESSION_IDS, 1, id);
total = total - 1;
continue;
}
rows.add(SerializableUtil.deserialize(session));
}
jedis.close();
sessions.put("total", total);
sessions.put("rows", rows);
return sessions;
}
示例8: pushWhenNoRepeat
import redis.clients.jedis.Jedis; //导入依赖的package包/类
@Override
protected void pushWhenNoRepeat(Task task, Request request) {
Jedis jedis = jedisPool.getResource();
try {
String content = serializer.serialize(request);
if(request.getPriority() == 0) {
jedis.rpush(RedisKeys.getQueueNoPriorityKey(task), content);
} else if (request.getPriority() > 0) {
jedis.zadd(RedisKeys.getZsetPlusPriorityKey(task), request.getPriority(), content);
} else {
jedis.zadd(RedisKeys.getZsetMinusPriorityKey(task), request.getPriority(), content);
}
jedis.sadd(RedisKeys.getSetKey(task), request.key());
} finally {
jedis.close();
}
}
示例9: publish
import redis.clients.jedis.Jedis; //导入依赖的package包/类
private static void publish(int id, String string)
{
Jedis jedis = null;
try
{
jedis = SamaGamesAPI.get().getBungeeResource();
if (jedis != null)
jedis.publish("tsbot", SamaGamesAPI.get().getServerName() + "/" + id + ":" + string);
}
catch (Exception exception)
{
SamaGamesAPI.get().getPlugin().getLogger().log(Level.SEVERE, "Jedis error", exception);
}
finally
{
if (jedis != null)
jedis.close();
}
}
示例10: zrem
import redis.clients.jedis.Jedis; //导入依赖的package包/类
public void zrem(String key, String member) {
Jedis jedis = null;
try {
jedis = getJedis();
if (jedis == null) {
logger.error("get jedis fail");
}
jedis.zrem(key, member);
} catch (JedisConnectionException e) {
if (jedis != null) {
jedis.close();
}
} finally {
returnJedisResource(jedis);
}
}
示例11: lpop
import redis.clients.jedis.Jedis; //导入依赖的package包/类
public String lpop(String key) {
Jedis jedis = null;
boolean success = true;
String ret = null;
try {
jedis = jedisPool.getResource();
if (jedis == null) {
success = false;
return ret;
}
ret = jedis.lpop(key);
} catch (Exception e) {
success = false;
returnBrokenResource(jedis, "lpop key:" + key, e);
} finally {
releaseReidsSource(success, jedis);
}
return ret;
}
示例12: ttl
import redis.clients.jedis.Jedis; //导入依赖的package包/类
public Long ttl(String key) {
log.trace("get set expire " + key);
Jedis jedis = null;
try {
jedis = redisConnection.getJedis();
jedis.select(dbIndex);
return jedis.ttl(key);
} catch (Exception e) {
log.warn(e.getMessage(), e);
} finally {
if (jedis != null) {
jedis.close();
}
}
return -2L;
}
示例13: hget
import redis.clients.jedis.Jedis; //导入依赖的package包/类
/**
* If key holds a hash, retrieve the value associated to the specified field.
* <p>
* If the field is not found or the key does not exist, a special 'nil' value is returned.
* <p>
* <b>Time complexity:</b> O(1)
*
* @param key specified key
* @param field hash field
* @return Bulk reply
*/
@Override
public String hget(final String bizkey,final String nameSpace, final String field,final GetDataCallBack<String> gbs) {
final String key = CacheUtils.getKeyByNamespace(bizkey,nameSpace);
return this.performFunction(key, new CallBack<String>() {
public String invoke(Jedis jedis) {
String res = jedis.hget(key, field);
if(StringUtils.isEmpty(res)){
if(null!=gbs){
res = gbs.invoke();
if(StringUtils.isNotEmpty(res)){
hset(bizkey,nameSpace,field,res);
}
}
}
return res;
}
});
}
示例14: createIndex
import redis.clients.jedis.Jedis; //导入依赖的package包/类
/**
* Create the index definition in redis
* @param schema a schema definition, see {@link Schema}
* @param options index option flags, see {@link IndexOptions}
* @return true if successful
*/
public boolean createIndex(Schema schema, IndexOptions options) {
Jedis conn = _conn();
ArrayList<String> args = new ArrayList<>();
args.add(indexName);
options.serializeRedisArgs(args);
args.add("SCHEMA");
for (Schema.Field f : schema.fields) {
f.serializeRedisArgs(args);
}
String rep = conn.getClient()
.sendCommand(commands.getCreateCommand(),
args.toArray(new String[args.size()]))
.getStatusCodeReply();
conn.close();
return rep.equals("OK");
}
示例15: hlen
import redis.clients.jedis.Jedis; //导入依赖的package包/类
@Override
public Long hlen(String key) {
Jedis jedis = null;
Long res = null;
try {
jedis = pool.getResource();
res = jedis.hlen(key);
} catch (Exception e) {
LOGGER.error(e.getMessage());
} finally {
returnResource(pool, jedis);
}
return res;
}