redis java api

Jedis 就是 redis 支持 java 的第三方库,我们可以使用 Jedis 类库操作 redis 数据库。

package bhz.redis.test;

import java.util.Iterator;
import java.util.List;

import org.junit.Test;

import redis.clients.jedis.Jedis;

public class testRedis {

</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> Jedis jedis = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("139.196.123.218", 6379<span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> args
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> test() {

    Jedis jedis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("139.196.123.218", 6379<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis);
    </span><span style="color: rgba(0, 128, 0, 1)">//

List<String> list = jedis.mget("name", "age");
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
String string
= (String) iterator.next();
System.out.println(string);
}
//
// Map<String, String> user = new HashMap<String, String>();
// user.put("name","huangyuxuan");
// user.put("age", "0.5");
// user.put("sex","男");
// jedis.hmset("user",user);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> List&lt;String&gt; rsmap = jedis.hmget("user", "name", "age","sex");
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> System.out.println(rsmap);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> jedis.hdel("user","age");
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> System.out.println(jedis.hmget("user", "age")); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">因为删除了,所以返回的是null
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> System.out.println(jedis.hlen("user")); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回key为user的键中存放的值的个数2
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> System.out.println(jedis.exists("user"));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">是否存在key为user的记录 返回true
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> System.out.println(jedis.hkeys("user"));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回map对象中的所有key
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> System.out.println(jedis.hvals("user"));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回map对象中的所有value
    </span><span style="color: rgba(0, 128, 0, 1)">//</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> testStr();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> testList();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> testSet();

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Iterator&lt;String&gt; iter=jedis.hkeys("user").iterator();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> while (iter.hasNext()){
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> String key = iter.next();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> System.out.println(key+":"+jedis.hmget("user",key));
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> }</span>

}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testStr() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> -----添加数据----------</span>
    jedis.set("name", "bhz");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 向key--&gt;name中放入了value--&gt;xinxin</span>
    System.out.println(jedis.get("name"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 执行结果:xinxin</span>
jedis.append("name", "is my lover"); // 拼接 System.out.println(jedis.get("name"));
    jedis.del(</span>"name"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除某个键</span>
    System.out.println(jedis.get("name"<span style="color: rgba(0, 0, 0, 1)">));
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置多个键值对</span>
    jedis.mset("name", "bhz", "age", "27", "qq", "174754613"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.incr(</span>"age"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 进行加1操作</span>
    System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-"
            + jedis.get("qq"<span style="color: rgba(0, 0, 0, 1)">));

}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testList() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 开始前,先移除所有的内容</span>
    jedis.del("java framework"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.lrange(</span>"java framework", 0, -1<span style="color: rgba(0, 0, 0, 1)">));
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 先向key java framework中存放三条数据</span>
    jedis.lpush("java framework", "spring"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.lpush(</span>"java framework", "struts"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.lpush(</span>"java framework", "hibernate"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 再取出所有数据jedis.lrange是按范围取出,
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有</span>
    System.out.println(jedis.lrange("java framework", 0, -1<span style="color: rgba(0, 0, 0, 1)">));

    jedis.del(</span>"java framework"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.rpush(</span>"java framework", "spring"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.rpush(</span>"java framework", "struts"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.rpush(</span>"java framework", "hibernate"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.lrange(</span>"java framework", 0, -1<span style="color: rgba(0, 0, 0, 1)">));
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSet() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 添加</span>
    jedis.sadd("user1", "liuling"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.sadd(</span>"user1", "xinxin"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.sadd(</span>"user1", "ling"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.sadd(</span>"user1", "zhangxinxin"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.sadd(</span>"user1", "who"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 移除noname</span>
    jedis.srem("user1", "who"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.smembers(</span>"user1"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取所有加入的value</span>
    System.out.println(jedis.sismember("user1", "who"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断 who
                                                        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否是user集合的元素</span>
    System.out.println(jedis.srandmember("user1"<span style="color: rgba(0, 0, 0, 1)">));
    System.out.println(jedis.scard(</span>"user1"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回集合的元素个数</span>

}

}

 

package bhz.redis01;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.BeforeClass;
import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPipeline;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.Transaction;

public class TestSingleRedis {

</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Jedis jedis;
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> ShardedJedis shard;
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> ShardedJedisPool pool;

@BeforeClass
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setUpBeforeClass() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 单个节点</span>
    jedis = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("139.196.123.218", 6379<span style="color: rgba(0, 0, 0, 1)">);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 分片
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> List&lt;JedisShardInfo&gt; shards = Arrays.asList(new JedisShardInfo(
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "139.196.123.218", 6379));
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> shard = new ShardedJedis(shards);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> GenericObjectPoolConfig goConfig = new GenericObjectPoolConfig();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> goConfig.setMaxTotal(100);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> goConfig.setMaxIdle(20);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> goConfig.setMaxWaitMillis(-1);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> goConfig.setTestOnBorrow(true);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> pool = new ShardedJedisPool(goConfig, shards);</span>

}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> @AfterClass</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> tearDownAfterClass() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception {
    jedis.disconnect();
    shard.disconnect();
    pool.destroy();
}

@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testString() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> -----添加数据----------</span>
    jedis.set("name", "bhz");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 向key--&gt;name中放入了value--&gt;xinxin</span>
    System.out.println(jedis.get("name"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 执行结果:xinxin</span>
jedis.append("name", "is my lover"); // 拼接 System.out.println(jedis.get("name"));
    jedis.del(</span>"name"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除某个键</span>
    System.out.println(jedis.get("name"<span style="color: rgba(0, 0, 0, 1)">));
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置多个键值对</span>
    jedis.mset("name", "bhz", "age", "27", "qq", "174754613"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.incr(</span>"age"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 进行加1操作</span>
    System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-"
            + jedis.get("qq"<span style="color: rgba(0, 0, 0, 1)">));
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * redis操作Map
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testMap() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> -----添加数据----------</span>
    Map&lt;String, String&gt; map = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap&lt;String, String&gt;<span style="color: rgba(0, 0, 0, 1)">();
    map.put(</span>"name", "xinxin"<span style="color: rgba(0, 0, 0, 1)">);
    map.put(</span>"age", "22"<span style="color: rgba(0, 0, 0, 1)">);
    map.put(</span>"qq", "123456"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.hmset(</span>"user"<span style="color: rgba(0, 0, 0, 1)">, map);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 取出user中的name,执行结果:[minxr]--&gt;注意结果是一个泛型的List
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第一个参数是存入redis中map对象的key,后面跟的是放入map中的对象的key,后面的key可以跟多个,是可变参数</span>
    List&lt;String&gt; rsmap = jedis.hmget("user", "name", "age", "qq"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(rsmap);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除map中的某个键值</span>
    jedis.hdel("user", "age"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.hmget(</span>"user", "age")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 因为删除了,所以返回的是null</span>
    System.out.println(jedis.hlen("user")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回key为user的键中存放的值的个数2</span>
    System.out.println(jedis.exists("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否存在key为user的记录 返回true</span>
    System.out.println(jedis.hkeys("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回map对象中的所有key</span>
    System.out.println(jedis.hvals("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回map对象中的所有value</span>
Iterator<String> iter = jedis.hkeys("user").iterator(); while (iter.hasNext()) { String key = iter.next(); System.out.println(key + ":" + jedis.hmget("user", key));} }
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * jedis操作List
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testList() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 开始前,先移除所有的内容</span>
    jedis.del("java framework"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.lrange(</span>"java framework", 0, -1<span style="color: rgba(0, 0, 0, 1)">));
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 先向key java framework中存放三条数据</span>
    jedis.lpush("java framework", "spring"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.lpush(</span>"java framework", "struts"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.lpush(</span>"java framework", "hibernate"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 再取出所有数据jedis.lrange是按范围取出,
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有</span>
    System.out.println(jedis.lrange("java framework", 0, -1<span style="color: rgba(0, 0, 0, 1)">));

    jedis.del(</span>"java framework"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.rpush(</span>"java framework", "spring"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.rpush(</span>"java framework", "struts"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.rpush(</span>"java framework", "hibernate"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.lrange(</span>"java framework", 0, -1<span style="color: rgba(0, 0, 0, 1)">));
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * jedis操作Set
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSet() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 添加</span>
    jedis.sadd("user", "liuling"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.sadd(</span>"user", "xinxin"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.sadd(</span>"user", "ling"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.sadd(</span>"user", "zhangxinxin"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.sadd(</span>"user", "who"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 移除noname</span>
    jedis.srem("user", "who"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.smembers(</span>"user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取所有加入的value</span>
    System.out.println(jedis.sismember("user", "who"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断 who
                                                        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否是user集合的元素</span>
    System.out.println(jedis.srandmember("user"<span style="color: rgba(0, 0, 0, 1)">));
    System.out.println(jedis.scard(</span>"user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回集合的元素个数</span>

}

@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> testRLpush() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> InterruptedException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> jedis 排序
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 注意,此处的rpush和lpush是List的操作。是一个双向链表(但从表现来看的)</span>
    jedis.del("a");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 先清除数据,再加入数据进行测试</span>
    jedis.rpush("a", "1"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.lpush(</span>"a", "6"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.lpush(</span>"a", "3"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.lpush(</span>"a", "9"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.lrange(</span>"a", 0, -1));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> [9, 3, 6, 1]</span>
    System.out.println(jedis.sort("a")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> [1, 3, 6, 9] </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">输入排序后结果</span>
    System.out.println(jedis.lrange("a", 0, -1<span style="color: rgba(0, 0, 0, 1)">));
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> @Test</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testTrans() {
    </span><span style="color: rgba(0, 0, 255, 1)">long</span> start =<span style="color: rgba(0, 0, 0, 1)"> System.currentTimeMillis();
    Transaction tx </span>=<span style="color: rgba(0, 0, 0, 1)"> jedis.multi();
    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; 1000; i++<span style="color: rgba(0, 0, 0, 1)">) {
        tx.set(</span>"t" + i, "t" +<span style="color: rgba(0, 0, 0, 1)"> i);
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> System.out.println(tx.get("t1000").get());</span>
List<Object> results = tx.exec(); long end = System.currentTimeMillis(); System.out.println("Transaction SET:" + ((end - start) / 1000.0) + "seconds");}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> @Test</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testPipelined() {
    Pipeline pipeline </span>=<span style="color: rgba(0, 0, 0, 1)"> jedis.pipelined();
    </span><span style="color: rgba(0, 0, 255, 1)">long</span> start =<span style="color: rgba(0, 0, 0, 1)"> System.currentTimeMillis();
    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; 1000; i++<span style="color: rgba(0, 0, 0, 1)">) {
        pipeline.set(</span>"p" + i, "p" +<span style="color: rgba(0, 0, 0, 1)"> i);
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> System.out.println(pipeline.get("p1000").get());</span>
    List&lt;Object&gt; results =<span style="color: rgba(0, 0, 0, 1)"> pipeline.syncAndReturnAll();
    </span><span style="color: rgba(0, 0, 255, 1)">long</span> end =<span style="color: rgba(0, 0, 0, 1)"> System.currentTimeMillis();
    System.out.println(</span>"Pipelined SET: " + ((end - start) / 1000.0<span style="color: rgba(0, 0, 0, 1)">)
            </span>+ " seconds"<span style="color: rgba(0, 0, 0, 1)">);
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> @Test</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testPipelineTrans() {
    </span><span style="color: rgba(0, 0, 255, 1)">long</span> start =<span style="color: rgba(0, 0, 0, 1)"> System.currentTimeMillis();
    Pipeline pipeline </span>=<span style="color: rgba(0, 0, 0, 1)"> jedis.pipelined();
    pipeline.multi();
    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; 100000; i++<span style="color: rgba(0, 0, 0, 1)">) {
        pipeline.set(</span>"" + i, "" +<span style="color: rgba(0, 0, 0, 1)"> i);
    }
    pipeline.exec();
    List</span>&lt;Object&gt; results =<span style="color: rgba(0, 0, 0, 1)"> pipeline.syncAndReturnAll();
    </span><span style="color: rgba(0, 0, 255, 1)">long</span> end =<span style="color: rgba(0, 0, 0, 1)"> System.currentTimeMillis();
    System.out.println(</span>"Pipelined transaction SET: "
            + ((end - start) / 1000.0) + " seconds"<span style="color: rgba(0, 0, 0, 1)">);
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> @Test</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testShard() {
    </span><span style="color: rgba(0, 0, 255, 1)">long</span> start =<span style="color: rgba(0, 0, 0, 1)"> System.currentTimeMillis();
    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; 100000; i++<span style="color: rgba(0, 0, 0, 1)">) {
        String result </span>= shard.set("shard" + i, "n" +<span style="color: rgba(0, 0, 0, 1)"> i);
    }
    </span><span style="color: rgba(0, 0, 255, 1)">long</span> end =<span style="color: rgba(0, 0, 0, 1)"> System.currentTimeMillis();
    System.out.println(</span>"shard SET: " + ((end - start) / 1000.0<span style="color: rgba(0, 0, 0, 1)">)
            </span>+ " seconds"<span style="color: rgba(0, 0, 0, 1)">);
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> @Test</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testShardpipelined() {
    ShardedJedisPipeline pipeline </span>=<span style="color: rgba(0, 0, 0, 1)"> shard.pipelined();
    </span><span style="color: rgba(0, 0, 255, 1)">long</span> start =<span style="color: rgba(0, 0, 0, 1)"> System.currentTimeMillis();
    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; 100000; i++<span style="color: rgba(0, 0, 0, 1)">) {
        pipeline.set(</span>"sp" + i, "p" +<span style="color: rgba(0, 0, 0, 1)"> i);
    }
    List</span>&lt;Object&gt; results =<span style="color: rgba(0, 0, 0, 1)"> pipeline.syncAndReturnAll();
    </span><span style="color: rgba(0, 0, 255, 1)">long</span> end =<span style="color: rgba(0, 0, 0, 1)"> System.currentTimeMillis();
    System.out.println(</span>"shardPipelined SET: " + ((end - start) / 1000.0<span style="color: rgba(0, 0, 0, 1)">)
            </span>+ " seconds"<span style="color: rgba(0, 0, 0, 1)">);
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> @Test</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testShardPool() {
    ShardedJedis sj </span>=<span style="color: rgba(0, 0, 0, 1)"> pool.getResource();

    </span><span style="color: rgba(0, 0, 255, 1)">long</span> start =<span style="color: rgba(0, 0, 0, 1)"> System.currentTimeMillis();
    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; 100000; i++<span style="color: rgba(0, 0, 0, 1)">) {
        String result </span>= sj.set("spn" + i, "n" +<span style="color: rgba(0, 0, 0, 1)"> i);
    }
    </span><span style="color: rgba(0, 0, 255, 1)">long</span> end =<span style="color: rgba(0, 0, 0, 1)"> System.currentTimeMillis();
    pool.returnResource(sj);
    System.out.println(</span>"shardPool SET: " + ((end - start) / 1000.0<span style="color: rgba(0, 0, 0, 1)">)
            </span>+ " seconds"<span style="color: rgba(0, 0, 0, 1)">);
}

}