java redis client jedis 测试

package cn.byref.demo1;

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

import junit.framework.TestCase;
import redis.clients.jedis.Jedis;

public class RedisClientTest extends TestCase {

</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Jedis getJedis() {
    Jedis jedis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("10.68.113.103", 6379<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> jedis;
}

</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, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> test_string_set() {
    Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> getJedis();
    jedis.set(</span>"age", "1000"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.get(</span>"age"<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的过期时间: setex(key,expire,value)
 </span><span style="color: rgba(0, 128, 0, 1)">*/</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_strign_setex() {
    Jedis j </span>=<span style="color: rgba(0, 0, 0, 1)"> getJedis();
    j.setex(</span>"customer", 2, "张铭"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(</span>"customer:" + j.get("customer"<span style="color: rgba(0, 0, 0, 1)">));
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        Thread.sleep(</span>3000<span style="color: rgba(0, 0, 0, 1)">);
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (InterruptedException e) {
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated catch block</span>

e.printStackTrace();
}
System.out.println(
"customer sleep 3 second =" + j.get("customer"));
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 对已有key的值追加字符串
 </span><span style="color: rgba(0, 128, 0, 1)">*/</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_string_append() {
    Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> getJedis();
    jedis.append(</span>"age", "append"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.get(</span>"age"<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:exists(String key)
 </span><span style="color: rgba(0, 128, 0, 1)">*/</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_exists_key() {
    Jedis j </span>=<span style="color: rgba(0, 0, 0, 1)"> getJedis();
    </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> exists = j.exists("age"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(</span>"age exists:" +<span style="color: rgba(0, 0, 0, 1)"> exists);

    exists </span>= j.exists("name"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(</span>"name exists:" +<span style="color: rgba(0, 0, 0, 1)"> exists);
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 获取所有的key
 </span><span style="color: rgba(0, 128, 0, 1)">*/</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_get_all_keys() {
    Jedis j </span>=<span style="color: rgba(0, 0, 0, 1)"> getJedis();
    Set</span>&lt;String&gt; keys = j.keys("*"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String key : keys) {
        System.out.println(</span>"key=" +<span style="color: rgba(0, 0, 0, 1)"> key);
    }
}

</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, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> test_expire() {
    Jedis j </span>=<span style="color: rgba(0, 0, 0, 1)"> getJedis();
    String key </span>= "username"<span style="color: rgba(0, 0, 0, 1)">;
    j.set(key, </span>"张三"<span style="color: rgba(0, 0, 0, 1)">);

    System.out.println(key </span>+ " exists=" +<span style="color: rgba(0, 0, 0, 1)"> j.exists(key));

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 查看某个key的剩余生存时间,单位【秒】.永久生存或者不存在的都返回-1</span>
    System.out.println(key + " ttl=" +<span style="color: rgba(0, 0, 0, 1)"> j.ttl(key));

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置过期时间,单位秒</span>
    j.expire(key, 5<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)"> 休眠2秒</span>
    <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        Thread.sleep(</span>2000<span style="color: rgba(0, 0, 0, 1)">);
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (InterruptedException e) {
        e.printStackTrace();
    }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 查看key过期时间</span>
    System.out.println(key + " sleep 2 second ttl=" +<span style="color: rgba(0, 0, 0, 1)"> j.ttl(key));

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 移除key的生存时间</span>

j.persist(key);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 查看key过期时间</span>
    System.out.println(key + " persist ttl=" +<span style="color: rgba(0, 0, 0, 1)"> j.ttl(key));
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * map类型操作 hmset(key,map)
 </span><span style="color: rgba(0, 128, 0, 1)">*/</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_map_set() {
    Map</span>&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)">();
    String key </span>= "user score"<span style="color: rgba(0, 0, 0, 1)">;
    map.put(</span>"username", "zhansan"<span style="color: rgba(0, 0, 0, 1)">);
    map.put(</span>"age", "20"<span style="color: rgba(0, 0, 0, 1)">);
    map.put(</span>"address", "北京"<span style="color: rgba(0, 0, 0, 1)">);

    Jedis j </span>=<span style="color: rgba(0, 0, 0, 1)"> getJedis();
    j.hmset(</span>"user score"<span style="color: rgba(0, 0, 0, 1)">, map);

    List</span>&lt;String&gt; values = j.hmget(key, "age", "username", "address"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(</span>"users list = " +<span style="color: rgba(0, 0, 0, 1)"> values);

    j.hdel(key, </span>"age"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(</span>"hdel users list = " +<span style="color: rgba(0, 0, 0, 1)"> values);
    System.out.println(</span>"map len = " +<span style="color: rgba(0, 0, 0, 1)"> j.hlen(key));
    System.out.println(</span>"all keys = " +<span style="color: rgba(0, 0, 0, 1)"> j.hkeys(key));
    System.out.println(</span>"all values = " +<span style="color: rgba(0, 0, 0, 1)"> j.hvals(key));

    System.out.println(</span>"*** iterate all keys ***"<span style="color: rgba(0, 0, 0, 1)">);
    Set</span>&lt;String&gt; mapKeys =<span style="color: rgba(0, 0, 0, 1)"> j.hkeys(key);
    </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String mapKey : mapKeys) {
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> String val = j.hget(key, mapKey); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回String</span>
        List&lt;String&gt; val = j.hmget(key, mapKey); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回List&lt;String&gt;</span>
        System.out.println(mapKey + " = " +<span style="color: rgba(0, 0, 0, 1)"> val);
    }

}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * List类型操作:lpush,rpush,lset,rset,lrange,lrem
 </span><span style="color: rgba(0, 128, 0, 1)">*/</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_list_set() {
    Jedis j </span>=<span style="color: rgba(0, 0, 0, 1)"> getJedis();
    
    String key </span>= "java collection"<span style="color: rgba(0, 0, 0, 1)">;
    j.del(key);
    j.lpush(key, </span>"Collection"<span style="color: rgba(0, 0, 0, 1)">);
    j.lpush(key, </span>"List"<span style="color: rgba(0, 0, 0, 1)">);
    j.lpush(key, </span>"Vector"<span style="color: rgba(0, 0, 0, 1)">);
    j.lpush(key, </span>"Map"<span style="color: rgba(0, 0, 0, 1)">);

    System.out.println(</span>"list len = " +<span style="color: rgba(0, 0, 0, 1)"> j.llen(key));
    
    System.out.println(</span>"lpush lrange(0,-1) = " + j.lrange(key, 0, -1<span style="color: rgba(0, 0, 0, 1)">));
    System.out.println(</span>"lpush lrange(0,2) = " + j.lrange(key, 0, 1<span style="color: rgba(0, 0, 0, 1)">));
    
    j.del(key);
    j.lpush(key, </span>"Collection"<span style="color: rgba(0, 0, 0, 1)">);
    j.lpush(key, </span>"List"<span style="color: rgba(0, 0, 0, 1)">);
    j.rpush(key, </span>"Vector"<span style="color: rgba(0, 0, 0, 1)">);
    j.rpush(key, </span>"Map"<span style="color: rgba(0, 0, 0, 1)">);
    j.rpush(key, </span>"List"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(</span>"rpush lrange(0,-1) = " + j.lrange(key, 0, -1<span style="color: rgba(0, 0, 0, 1)">));
    System.out.println(key </span>+ " ttl=" +<span style="color: rgba(0, 0, 0, 1)"> j.ttl(key));
    
    j.lrem(key,</span>1,"List"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> lrem(key,count,value) count表示有重复的值时,删除几次</span>
    System.out.println("lrem lrange(0,-1) = " + j.lrange(key, 0, -1)); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">[Collection, Vector, Map, List]</span>
j.lset(key, 1, "NewList"); System.out.println("lrem lrange(0,-1) =" + j.lrange(key, 0, -1));
}

}

 

常用命令
    1)连接操作命令
    quit:关闭连接(connection)
    auth:简单密码认证
    help cmd: 查看 cmd 帮助,例如:help quit
    
    2)持久化
    save:将数据同步保存到磁盘
    bgsave:将数据异步保存到磁盘
    lastsave:返回上次成功将数据保存到磁盘的 Unix 时戳
    shundown:将数据同步保存到磁盘,然后关闭服务
    
    3)远程服务控制
    info:提供服务器的信息和统计
    monitor:实时转储收到的请求
    slaveof:改变复制策略设置
    config:在运行时配置 Redis 服务器
    
    4)对 value 操作的命令
    exists(key):确认一个 key 是否存在
    del(key):删除一个 key
    type(key):返回值的类型
    keys(pattern):返回满足给定 pattern 的所有 key
    randomkey:随机返回 key 空间的一个
    keyrename(oldname, newname):重命名 key
    dbsize:返回当前数据库中 key 的数目
    expire:设定一个 key 的活动时间(s)
    ttl:获得一个 key 的活动时间
    select(index):按索引查询
    move(key, dbindex):移动当前数据库中的 key 到 dbindex 数据库
    flushdb:删除当前选择数据库中的所有 key
    flushall:删除所有数据库中的所有 key
    
    5)String
    set(key, value):给数据库中名称为 key 的 string 赋予值 value
    get(key):返回数据库中名称为 key 的 string 的 value
    getset(key, value):给名称为 key 的 string 赋予上一次的 value
    mget(key1, key2,…, key N):返回库中多个 string 的 value
    setnx(key, value):添加 string,名称为 key,值为 value
    setex(key, time, value):向库中添加 string,设定过期时间 time
    mset(key N, value N):批量设置多个 string 的值
    msetnx(key N, value N):如果所有名称为 key i 的 string 都不存在
    incr(key):名称为 key 的 string 增 1 操作
    incrby(key, integer):名称为 key 的 string 增加 integer
    decr(key):名称为 key 的 string 减 1 操作
    decrby(key, integer):名称为 key 的 string 减少 integer
    append(key, value):名称为 key 的 string 的值附加 value
    substr(key, start, end):返回名称为 key 的 string 的 value 的子串
    
    6)List 
    rpush(key, value):在名称为 key 的 list 尾添加一个值为 value 的元素
    lpush(key, value):在名称为 key 的 list 头添加一个值为 value 的 元素
    llen(key):返回名称为 key 的 list 的长度
    lrange(key, start, end):返回名称为 key 的 list 中 start 至 end 之间的元素
    ltrim(key, start, end):截取名称为 key 的 list
    lindex(key, index):返回名称为 key 的 list 中 index 位置的元素
    lset(key, index, value):给名称为 key 的 list 中 index 位置的元素赋值
    lrem(key, count, value):删除 count 个 key 的 list 中值为 value 的元素
    lpop(key):返回并删除名称为 key 的 list 中的首元素
    rpop(key):返回并删除名称为 key 的 list 中的尾元素
    blpop(key1, key2,… key N, timeout):lpop 命令的 block 版本。
    brpop(key1, key2,… key N, timeout):rpop 的 block 版本。
    rpoplpush(srckey, dstkey):返回并删除名称为 srckey 的 list 的尾元素,

              并将该元素添加到名称为 dstkey 的 list 的头部
    
    7)Set
    sadd(key, member):向名称为 key 的 set 中添加元素 member
    srem(key, member) :删除名称为 key 的 set 中的元素 member
    spop(key) :随机返回并删除名称为 key 的 set 中一个元素
    smove(srckey, dstkey, member) :移到集合元素
    scard(key) :返回名称为 key 的 set 的基数
    sismember(key, member) :member 是否是名称为 key 的 set 的元素
    sinter(key1, key2,…key N) :求交集
    sinterstore(dstkey, (keys)) :求交集并将交集保存到 dstkey 的集合
    sunion(key1, (keys)) :求并集
    sunionstore(dstkey, (keys)) :求并集并将并集保存到 dstkey 的集合
    sdiff(key1, (keys)) :求差集
    sdiffstore(dstkey, (keys)) :求差集并将差集保存到 dstkey 的集合
    smembers(key) :返回名称为 key 的 set 的所有元素
    srandmember(key) :随机返回名称为 key 的 set 的一个元素
    
    8)Hash
    hset(key, field, value):向名称为 key 的 hash 中添加元素 field
    hget(key, field):返回名称为 key 的 hash 中 field 对应的 value
    hmget(key, (fields)):返回名称为 key 的 hash 中 field i 对应的 value
    hmset(key, (fields)):向名称为 key 的 hash 中添加元素 field 
    hincrby(key, field, integer):将名称为 key 的 hash 中 field 的 value 增加 integer
    hexists(key, field):名称为 key 的 hash 中是否存在键为 field 的域
    hdel(key, field):删除名称为 key 的 hash 中键为 field 的域
    hlen(key):返回名称为 key 的 hash 中元素个数
    hkeys(key):返回名称为 key 的 hash 中所有键
    hvals(key):返回名称为 key 的 hash 中所有键对应的 value
    hgetall(key):返回名称为 key 的 hash 中所有的键(field)及其对应的 value