예제 #1
0
  @Test(expected = JedisDataException.class)
  public void pipelineResponseWithinPipeline() {
    jedis.set("string", "foo");

    ShardedJedisPipeline p = jedis.pipelined();
    Response<String> string = p.get("string");
    string.get();
    p.sync();
  }
예제 #2
0
 public void addAuthCode(String key, String authCode) {
   ShardedJedis shardedJedis = shardedJedisPool.getResource();
   try {
     shardedJedis.set(key, authCode);
     shardedJedis.expire(key, Constants.AUTH_CODE_EXPIRE);
   } catch (Throwable e) {
     throw new RuntimeException(e);
   } finally {
     shardedJedis.close();
   }
 }
예제 #3
0
 public void addAccessToken(String key, String accessToken) {
   ShardedJedis shardedJedis = shardedJedisPool.getResource();
   try {
     shardedJedis.set(key, accessToken);
     shardedJedis.expire(key, Constants.ACCESS_TOKEN_EXPIRE);
   } catch (Throwable e) {
     throw new RuntimeException(e);
   } finally {
     shardedJedis.close();
   }
 }
예제 #4
0
  @Override
  public String set(String key, String value) {

    ShardedJedis jedis = shardedJedisPool.getResource();
    try {
      return jedis.set(key, value);
    } catch (Exception e) {
      logger.error("Execute redis command failure", e);
    } finally {
      jedis.close();
    }
    return null;
  }
예제 #5
0
 /**
  * 向redis存入key和value,并释放连接资源
  *
  * <p>如果key已经存在 则覆盖
  *
  * @param key
  * @param value
  * @return 成功 返回OK 失败返回 0
  */
 public String set(String key, String value) {
   ShardedJedis jedis = null;
   try {
     jedis = pool.getResource();
     return jedis.set(key, value);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
     return "0";
   } finally {
     returnResource(pool, jedis);
   }
 }
예제 #6
0
 public boolean set(String key, String value) {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = masterShardedJedisPool.getResource();
     shardedJedis.set(key, value);
     return true;
   } catch (Exception ex) {
     logger.error("set error.", ex);
   } finally {
     returnResource(shardedJedis);
   }
   return false;
 }
예제 #7
0
 private void KeyOperate() {
   System.out.println("======================key==========================");
   // 清空数据
   System.out.println("清空库中所有数据:" + jedis.flushDB());
   // 判断key否存在
   System.out.println("判断key999键是否存在:" + shardedJedis.exists("key999"));
   System.out.println("新增key001,value001键值对:" + shardedJedis.set("key001", "value001"));
   System.out.println("判断key001是否存在:" + shardedJedis.exists("key001"));
   // 输出系统中所有的key
   System.out.println("新增key002,value002键值对:" + shardedJedis.set("key002", "value002"));
   System.out.println("系统中所有键如下:");
   Set<String> keys = jedis.keys("*");
   Iterator<String> it = keys.iterator();
   while (it.hasNext()) {
     String key = it.next();
     System.out.println(key);
   }
   // 删除某个key,若key不存在,则忽略该命令。
   System.out.println("系统中删除key002: " + jedis.del("key002"));
   System.out.println("判断key002是否存在:" + shardedJedis.exists("key002"));
   // 设置 key001的过期时间
   System.out.println("设置 key001的过期时间为5秒:" + jedis.expire("key001", 5));
   try {
     Thread.sleep(2000);
   } catch (InterruptedException e) {
   }
   // 查看某个key的剩余生存时间,单位【秒】.永久生存或者不存在的都返回-1
   System.out.println("查看key001的剩余生存时间:" + jedis.ttl("key001"));
   // 移除某个key的生存时间
   System.out.println("移除key001的生存时间:" + jedis.persist("key001"));
   System.out.println("查看key001的剩余生存时间:" + jedis.ttl("key001"));
   // 查看key所储存的值的类型
   System.out.println("查看key所储存的值的类型:" + jedis.type("key001"));
   /*
    * 一些其他方法:1、修改键名:jedis.rename("key6", "key0");
    * 2、将当前db的key移动到给定的db当中:jedis.move("foo", 1)
    */
 }
예제 #8
0
 public String set(String key, String value) {
   ShardedJedis jedis = null;
   boolean success = true;
   try {
     pool = getPool();
     jedis = pool.getResource();
     return jedis.set(key, value);
   } catch (JedisException e) {
     success = false;
     if (jedis != null) {
       pool.close();
     }
     throw e;
   } finally {
     if (success && jedis != null) {
       pool.close();
     }
   }
 }
예제 #9
0
  @Test
  public void pipelineResponse() {
    jedis.set("string", "foo");
    jedis.lpush("list", "foo");
    jedis.hset("hash", "foo", "bar");
    jedis.zadd("zset", 1, "foo");
    jedis.sadd("set", "foo");

    ShardedJedisPipeline p = jedis.pipelined();
    Response<String> string = p.get("string");
    Response<String> list = p.lpop("list");
    Response<String> hash = p.hget("hash", "foo");
    Response<Set<String>> zset = p.zrange("zset", 0, -1);
    Response<String> set = p.spop("set");
    Response<Boolean> blist = p.exists("list");
    Response<Double> zincrby = p.zincrby("zset", 1, "foo");
    Response<Long> zcard = p.zcard("zset");
    p.lpush("list", "bar");
    Response<List<String>> lrange = p.lrange("list", 0, -1);
    Response<Map<String, String>> hgetAll = p.hgetAll("hash");
    p.sadd("set", "foo");
    Response<Set<String>> smembers = p.smembers("set");
    Response<Set<Tuple>> zrangeWithScores = p.zrangeWithScores("zset", 0, -1);
    p.sync();

    assertEquals("foo", string.get());
    assertEquals("foo", list.get());
    assertEquals("bar", hash.get());
    assertEquals("foo", zset.get().iterator().next());
    assertEquals("foo", set.get());
    assertFalse(blist.get());
    assertEquals(new Double(2), zincrby.get());
    assertEquals(new Long(1), zcard.get());
    assertEquals(1, lrange.get().size());
    assertNotNull(hgetAll.get().get("foo"));
    assertEquals(1, smembers.get().size());
    assertEquals(1, zrangeWithScores.get().size());
  }
예제 #10
0
파일: Demo3.java 프로젝트: wangqingqi/test
  /**
   * 添加数据的方法
   *
   * @param keys
   * @param value
   */
  public void addValue(String keys, String value) {

    jedis.set(keys, value);
  }