redis_bj_01


windows下安装redis


下载地址https://github.com/dmajkic/redis/downloads。下载到的Redis支持32bit和64bit。根据自己实际情况选择,我选择64bit。把64bit文件内容拷贝到需要安装的目录下E:\molly\redis\redis-windows
打开一个cmd窗口,使用cd命令切换到指定目录(E:\molly\redis\redis-windows)运行 redis-server.exe redis.conf 。运行以后出现如下界面
这里写图片描述


这就说明Redis服务端已经安装成功。
重新打开一个cmd窗口,使用cd命令切换到指定目录(E:\molly\redis\redis-windows)运行 redis-cli.exe -h 127.0.0.1 -p 6379,其中 127.0.0.1是本地ip,6379是redis服务端的默认端口。运行成功如下图所示。
这样,Redis windows环境下搭建已经完成,是不是很简单。
这里写图片描述


环境已经搭建好,总得测试下吧。比如:存储一个key为test,value为hello word的字符串,然后获取key值
这里写图片描述
正确输出 hell word,测试成功!


3.java应用redis


Jedis 是 Redis 官方首选的 Java 客户端开发包


开发前置准备工作:


使用Java操作Redis需要jedis-2.1.0.jar,如果需要使用Redis连接池的话,还需commons-pool-1.5.4.jar,下载地址:可以参考我的下载资源,也可以自己网页下载



"
fang" redis 127.0.0.1:6379> set bar 1 OK redis 127.0.0.1:6379> keys * 1) "hashs" 2) "bar" 3) "xixi" redis 127.0.0.1:6379> exists bar (integer) 1 redis 127.0.0.1:6379> exists xixi (integer) 1 redis 127.0.0.1:6379> exists xx (integer) 0 redis 127.0.0.1:6379> delbar (error) ERR unknown command 'delbar' redis 127.0.0.1:6379> del bar (integer) 1 redis 127.0.0.1:6379> type xixi string redis 127.0.0.1:6379> lpush bar 1 #类型:list (integer) 1 redis 127.0.0.1:6379> type bar list redis 127.0.0.1:6379> incr num (integer) 1 redis 127.0.0.1:6379> incr num (integer) 2 redis 127.0.0.1:6379> incr num (integer) 3 redis 127.0.0.1:6379> set notint sdfdfd OK redis 127.0.0.1:6379> incr notint (error) ERR value is not an integer or out of range redis 127.0.0.1:6379> incrby bar 7 (error) ERR Operation against a key holding the wrong kind of value redis 127.0.0.1:6379> incr bar (error) ERR Operation against a key holding the wrong kind of value redis 127.0.0.1:6379> incr num (integer) 4 redis 127.0.0.1:6379> incrby num 2 (integer) 6 redis 127.0.0.1:6379> decr num #减少 (integer) 5 redis 127.0.0.1:6379> incrby num -5 (integer) 0 redis 127.0.0.1:6379> incrbyfloat num 2.7 (error) ERR unknown command 'incrbyfloat' redis 127.0.0.1:6379> incrbyfloat num 2.7 (error) ERR unknown command 'incrbyfloat' redis 127.0.0.1:6379> INCRBYFLOAT num 2.1 (error) ERR unknown command 'INCRBYFLOAT' redis 127.0.0.1:6379> append key num (integer) 3 redis 127.0.0.1:6379> get "num" "0" redis 127.0.0.1:6379> get num "0" redis 127.0.0.1:6379> get xixi "alamps" redis 127.0.0.1:6379> type xixi string redis 127.0.0.1:6379> append xixi " haha" (integer) 11 redis 127.0.0.1:6379> get xixi "alamps haha" redis 127.0.0.1:6379> strlen xixi (integer) 11 redis 127.0.0.1:6379> set hehe 奋斗 OK redis 127.0.0.1:6379> strlen hehe (integer) 4 redis 127.0.0.1:6379> mset key1 v1 key2 v2 key3 v3 ---map :put OK redis 127.0.0.1:6379> get key2 "v2" redis 127.0.0.1:6379> mget key1 key2 --map :get 1) "v1" 2) "v2" redis 127.0.0.1:6379> set foo bar OK redis 127.0.0.1:6379> getbit foo 0 (integer) 0 redis 127.0.0.1:6379> getbit foo 6 (integer) 1 redis 127.0.0.1:6379> hset car privce 200000 (integer) 1 redis 127.0.0.1:6379> hset car name bmw (integer) 1 redis 127.0.0.1:6379> hget car name "bmw" redis 127.0.0.1:6379> hset swk age 50000 sex male (error) ERR wrong number of arguments for 'hset' command redis 127.0.0.1:6379> hset swk age 5 sex 1 (error) ERR wrong number of arguments for 'hset' command redis 127.0.0.1:6379> hmset swk age 50000 sex male OK redis 127.0.0.1:6379> hmget swk (error) ERR wrong number of arguments for 'hmget' command redis 127.0.0.1:6379> hmget swk age sex 1) "50000" 2) "male" redis 127.0.0.1:6379> hgetall swk 1) "age" 2) "50000" 3) "sex" 4) "male" redis 127.0.0.1:6379> hincrby person score 60 (integer) 60 redis 127.0.0.1:6379> getall person (error) ERR unknown command 'getall' redis 127.0.0.1:6379> mgetall person (error) ERR unknown command 'mgetall' redis 127.0.0.1:6379> hgetall person 1) "score" 2) "60" redis 127.0.0.1:6379> hdel person age (integer) 0 redis 127.0.0.1:6379> hdel person score (integer) 1 redis 127.0.0.1:6379> hgetall persion (empty list or set) redis 127.0.0.1:6379> hgetall person (empty list or set) redis 127.0.0.1:6379> hincrby student age 31 (integer) 31 redis 127.0.0.1:6379> hgetall student 1) "age" 2) "31" redis 127.0.0.1:6379> hkeys car --get map keys 1) "privce" 2) "name" redis 127.0.0.1:6379> hvalues car --get map values (error) ERR unknown command 'hvalues' redis 127.0.0.1:6379> hvals car 1) "200000" 2) "bmw" redis 127.0.0.1:6379> hlen car (integer) 2 redis 127.0.0.1:6379> lpush stulist 1 (integer) 1 redis 127.0.0.1:6379> lpush stulist 2 3 (error) ERR wrong number of arguments for 'lpush' command redis 127.0.0.1:6379> lpush stulist 2 3 (error) ERR wrong number of arguments for 'lpush' command redis 127.0.0.1:6379> rpush stulist 0 -1 (error) ERR wrong number of arguments for 'rpush' command redis 127.0.0.1:6379> lpop sutlist (nil) redis 127.0.0.1:6379> lpop stulist "1" redis 127.0.0.1:6379> llen stulist (integer) 0 redis 127.0.0.1:6379> lpush xxx 1 (integer) 1 redis 127.0.0.1:6379> lpush xxx 2 3 (error) ERR wrong number of arguments for 'lpush' command redis 127.0.0.1:6379> lpush xxx 2,3 (integer) 2 redis 127.0.0.1:6379> lrange xxx (error) ERR wrong number of arguments for 'lrange' command redis 127.0.0.1:6379> lrange 0 3 (error) ERR wrong number of arguments for 'lrange' command redis 127.0.0.1:6379> lrange 0,3 (error) ERR wrong number of arguments for 'lrange' command redis 127.0.0.1:6379> lrange 0 2 (error) ERR wrong number of arguments for 'lrange' command redis 127.0.0.1:6379> lrange xxx 0 2 1) "2,3" 2) "1" redis 127.0.0.1:6379> lpush x 1 (integer) 1 redis 127.0.0.1:6379> lpush x 2 (integer) 2 redis 127.0.0.1:6379> lpush x 3 (integer) 3 redis 127.0.0.1:6379> lrange x (error) ERR wrong number of arguments for 'lrange' command redis 127.0.0.1:6379> lrange x 0 2 1) "3" 2) "2" 3) "1" redis 127.0.0.1:6379> lrange x -1 -2 (empty list or set) redis 127.0.0.1:6379> lrange x -2 -1 1) "2" 2) "1" redis 127.0.0.1:6379> lrem x -1 3 (integer) 1 redis 127.0.0.1:6379> lrange x 0 1 1) "2" 2) "1" redis 127.0.0.1:6379> lindex x 0 "2" redis 127.0.0.1:6379> lindex x -1 "1" redis 127.0.0.1:6379> lset x 1 7 OK redis 127.0.0.1:6379> lindex x 0 "2" redis 127.0.0.1:6379> lindex x 1 "7" redis 127.0.0.1:6379> lpush x 3 (integer) 3 redis 127.0.0.1:6379> lindex 3 (error) ERR wrong number of arguments for 'lindex' command redis 127.0.0.1:6379> lindex x 3 (nil) redis 127.0.0.1:6379> lindex x 2 "7" redis 127.0.0.1:6379> ltrim x 0 1 OK redis 127.0.0.1:6379> lindex x 0 "3" redis 127.0.0.1:6379> lindex x 1 "2" redis 127.0.0.1:6379> lindex x 2 (nil) redis 127.0.0.1:6379> lindex x 0 1 (error) ERR wrong number of arguments for 'lindex' command redis 127.0.0.1:6379> lrange x 0 1 1) "3" 2) "2" redis 127.0.0.1:6379> lrange x 0 2 1) "3" 2) "2" redis 127.0.0.1:6379> lrange x 0 -1 1) "3" 2) "2" redis 127.0.0.1:6379> linsert x before 2 7 (integer) 3 redis 127.0.0.1:6379> lrange x 0 -1 1) "3" 2) "7" 3) "2" redis 127.0.0.1:6379> linsert x after 7 9 (integer) 4 redis 127.0.0.1:6379> lrange 0 -1 (error) ERR wrong number of arguments for 'lrange' command redis 127.0.0.1:6379> lrange x 0 -1 -- list 查询所有 1) "3" 2) "7" 3) "9" 4) "2" redis 127.0.0.1:6379> sadd set a ---set.add (integer) 1 redis 127.0.0.1:6379> sadd set a b c (integer) 2 redis 127.0.0.1:6379> srem set c ---set.remove (integer) 1 redis 127.0.0.1:6379> smembers set ---set.查询所有 1) "a" 2) "b" redis 127.0.0.1:6379> sismember a member (integer) 0 redis 127.0.0.1:6379> sismember a (error) ERR wrong number of arguments for 'sismember' command redis 127.0.0.1:6379> sismember d (error) ERR wrong number of arguments for 'sismember' command redis 127.0.0.1:6379> sismember set a (integer) 1 redis 127.0.0.1:6379> sismember set d (integer) 0 redis 127.0.0.1:6379> scard set (integer) 2 redis 127.0.0.1:6379> smembers set 1) "a" 2) "b" redis 127.0.0.1:6379> s pop a (error) ERR unknown command 's' redis 127.0.0.1:6379> s pop set (error) ERR unknown command 's' redis 127.0.0.1:6379> spop set "a" redis 127.0.0.1:6379> spop set "b" redis 127.0.0.1:6379> spop set (nil) redis 127.0.0.1:6379> zadd orderlist 1 a 2 b 3 c (integer) 3 redis 127.0.0.1:6379> zadd orderlist 7 b (integer) 0 redis 127.0.0.1:6379> zscore orderlist b "7" redis 127.0.0.1:6379> zscore orderlist 0 2 (error) ERR wrong number of arguments for 'zscore' command redis 127.0.0.1:6379> zrange orderlist 0 2 1) "a" 2) "c" 3) "b" redis 127.0.0.1:6379> zrange orderlist 0 -1 1) "a" 2) "c" 3) "b" redis 127.0.0.1:6379> zcard orderlist (integer) 3 redis 127.0.0.1:6379>

 

package redis;

public class Main {

    public static void main(String[] args) {

        new RedisClient().show(); 
    }
}


----

package redis;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.SortingParams;

public class RedisClient {

    private Jedis jedis;//非切片额客户端连接
    private JedisPool jedisPool;//非切片连接池
    private ShardedJedis shardedJedis;//切片额客户端连接
    private ShardedJedisPool shardedJedisPool;//切片连接池

    public RedisClient() 
    { 
        initialPool(); 
        initialShardedPool(); 
        shardedJedis = shardedJedisPool.getResource(); 
        jedis = jedisPool.getResource(); 
    } 

    /**
     * 初始化非切片池
     */
    private void initialPool() 
    { 
        // 池基本配置 
        JedisPoolConfig config = new JedisPoolConfig(); 
        config.setMaxActive(20); 
        config.setMaxIdle(5); 
        config.setMaxWait(1000l); 
        config.setTestOnBorrow(false); 

        jedisPool = new JedisPool(config,"127.0.0.1",6379);
    }

    /** 
     * 初始化切片池 
     */ 
    private void initialShardedPool() 
    { 
        // 池基本配置 
        JedisPoolConfig config = new JedisPoolConfig(); 
        config.setMaxActive(20); 
        config.setMaxIdle(5); 
        config.setMaxWait(1000l); 
        config.setTestOnBorrow(false); 
        // slave链接 
        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); 
        shards.add(new JedisShardInfo("127.0.0.1", 6379, "master")); 

        // 构造池 
        shardedJedisPool = new ShardedJedisPool(config, shards); 
    } 

    public void show() {     
        KeyOperate(); 
        StringOperate(); 
        ListOperate(); 
        SetOperate();
        SortedSetOperate();
        HashOperate(); 
        jedisPool.returnResource(jedis);
        shardedJedisPool.returnResource(shardedJedis);
    } 
    
    public void test() {     
        test01(); 
        jedisPool.returnResource(jedis);
        shardedJedisPool.returnResource(shardedJedis);
    } 

    private void test01() {
//        jedis.set("xixi", "alamps");
//        System.out.println(jedis.get("xixi"));
        
        
    }

    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)
         */
    }

    private void StringOperate() 
    {  
        System.out.println("======================String_1=========================="); 
        // 清空数据 
        System.out.println("清空库中所有数据:"+jedis.flushDB());

        System.out.println("=============增=============");
        jedis.set("key001","value001");
        jedis.set("key002","value002");
        jedis.set("key003","value003");
        System.out.println("已新增的3个键值对如下:");
        System.out.println(jedis.get("key001"));
        System.out.println(jedis.get("key002"));
        System.out.println(jedis.get("key003"));

        System.out.println("=============删=============");  
        System.out.println("删除key003键值对:"+jedis.del("key003"));  
        System.out.println("获取key003键对应的值:"+jedis.get("key003"));

        System.out.println("=============改=============");
        //1、直接覆盖原来的数据
        System.out.println("直接覆盖key001原来的数据:"+jedis.set("key001","value001-update"));
        System.out.println("获取key001对应的新值:"+jedis.get("key001"));
        //2、直接覆盖原来的数据  
        System.out.println("在key002原来值后面追加:"+jedis.append("key002","+appendString"));
        System.out.println("获取key002对应的新值"+jedis.get("key002")); 

        System.out.println("=============增,删,查(多个)=============");
        /** 
         * mset,mget同时新增,修改,查询多个键值对 
         * 等价于:
         * jedis.set("name","ssss"); 
         * jedis.set("jarorwar","xxxx"); 
         */  
        System.out.println("一次性新增key201,key202,key203,key204及其对应值:"+jedis.mset("key201","value201",
                        "key202","value202","key203","value203","key204","value204"));  
        System.out.println("一次性获取key201,key202,key203,key204各自对应的值:"+
                        jedis.mget("key201","key202","key203","key204"));
        System.out.println("一次性删除key201,key202:"+jedis.del(new String[]{"key201", "key202"}));
        System.out.println("一次性获取key201,key202,key203,key204各自对应的值:"+
                jedis.mget("key201","key202","key203","key204")); 
        System.out.println();


        //jedis具备的功能shardedJedis中也可直接使用,下面测试一些前面没用过的方法
        System.out.println("======================String_2=========================="); 
        // 清空数据 
        System.out.println("清空库中所有数据:"+jedis.flushDB());       

        System.out.println("=============新增键值对时防止覆盖原先值=============");
        System.out.println("原先key301不存在时,新增key301:"+shardedJedis.setnx("key301", "value301"));
        System.out.println("原先key302不存在时,新增key302:"+shardedJedis.setnx("key302", "value302"));
        System.out.println("当key302存在时,尝试新增key302:"+shardedJedis.setnx("key302", "value302_new"));
        System.out.println("获取key301对应的值:"+shardedJedis.get("key301"));
        System.out.println("获取key302对应的值:"+shardedJedis.get("key302"));

        System.out.println("=============超过有效期键值对被删除=============");
        // 设置key的有效期,并存储数据 
        System.out.println("新增key303,并指定过期时间为2秒"+shardedJedis.setex("key303", 2, "key303-2second")); 
        System.out.println("获取key303对应的值:"+shardedJedis.get("key303")); 
        try{ 
            Thread.sleep(3000); 
        } 
        catch (InterruptedException e){ 
        } 
        System.out.println("3秒之后,获取key303对应的值:"+shardedJedis.get("key303")); 

        System.out.println("=============获取原值,更新为新值一步完成=============");
        System.out.println("key302原值:"+shardedJedis.getSet("key302", "value302-after-getset"));
        System.out.println("key302新值:"+shardedJedis.get("key302"));

        System.out.println("=============获取子串=============");
        System.out.println("获取key302对应值中的子串:"+shardedJedis.getrange("key302", 5, 7));         
    }

    private void ListOperate() 
    { 
        System.out.println("======================list=========================="); 
        // 清空数据 
        System.out.println("清空库中所有数据:"+jedis.flushDB()); 

        System.out.println("=============增=============");
        shardedJedis.lpush("stringlists", "vector"); 
        shardedJedis.lpush("stringlists", "ArrayList"); 
        shardedJedis.lpush("stringlists", "vector");
        shardedJedis.lpush("stringlists", "vector");
        shardedJedis.lpush("stringlists", "LinkedList");
        shardedJedis.lpush("stringlists", "MapList");
        shardedJedis.lpush("stringlists", "SerialList");
        shardedJedis.lpush("stringlists", "HashList");
        shardedJedis.lpush("numberlists", "3");
        shardedJedis.lpush("numberlists", "1");
        shardedJedis.lpush("numberlists", "5");
        shardedJedis.lpush("numberlists", "2");
        System.out.println("所有元素-stringlists:"+shardedJedis.lrange("stringlists", 0, -1));
        System.out.println("所有元素-numberlists:"+shardedJedis.lrange("numberlists", 0, -1));

        System.out.println("=============删=============");
        // 删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈
        System.out.println("成功删除指定元素个数-stringlists:"+shardedJedis.lrem("stringlists", 2, "vector")); 
        System.out.println("删除指定元素之后-stringlists:"+shardedJedis.lrange("stringlists", 0, -1));
        // 删除区间以外的数据 
        System.out.println("删除下标0-3区间之外的元素:"+shardedJedis.ltrim("stringlists", 0, 3));
        System.out.println("删除指定区间之外元素后-stringlists:"+shardedJedis.lrange("stringlists", 0, -1));
        // 列表元素出栈 
        System.out.println("出栈元素:"+shardedJedis.lpop("stringlists")); 
        System.out.println("元素出栈后-stringlists:"+shardedJedis.lrange("stringlists", 0, -1));

        System.out.println("=============改=============");
        // 修改列表中指定下标的值 
        shardedJedis.lset("stringlists", 0, "hello list!"); 
        System.out.println("下标为0的值修改后-stringlists:"+shardedJedis.lrange("stringlists", 0, -1));
        System.out.println("=============查=============");
        // 数组长度 
        System.out.println("长度-stringlists:"+shardedJedis.llen("stringlists"));
        System.out.println("长度-numberlists:"+shardedJedis.llen("numberlists"));
        // 排序 
        /*
         * list中存字符串时必须指定参数为alpha,如果不使用SortingParams,而是直接使用sort("list"),
         * 会出现"ERR One or more scores can't be converted into double"
         */
        SortingParams sortingParameters = new SortingParams();
        sortingParameters.alpha();
        sortingParameters.limit(0, 3);
        System.out.println("返回排序后的结果-stringlists:"+shardedJedis.sort("stringlists",sortingParameters)); 
        System.out.println("返回排序后的结果-numberlists:"+shardedJedis.sort("numberlists"));
        // 子串:  start为元素下标,end也为元素下标;-1代表倒数一个元素,-2代表倒数第二个元素
        System.out.println("子串-第二个开始到结束:"+shardedJedis.lrange("stringlists", 1, -1));
        // 获取列表指定下标的值 
        System.out.println("获取下标为2的元素:"+shardedJedis.lindex("stringlists", 2)+"\n");
    }

    private void SetOperate() 
    { 

        System.out.println("======================set=========================="); 
        // 清空数据 
        System.out.println("清空库中所有数据:"+jedis.flushDB());

        System.out.println("=============增=============");
        System.out.println("向sets集合中加入元素element001:"+jedis.sadd("sets", "element001")); 
        System.out.println("向sets集合中加入元素element002:"+jedis.sadd("sets", "element002")); 
        System.out.println("向sets集合中加入元素element003:"+jedis.sadd("sets", "element003"));
        System.out.println("向sets集合中加入元素element004:"+jedis.sadd("sets", "element004"));
        System.out.println("查看sets集合中的所有元素:"+jedis.smembers("sets")); 
        System.out.println();

        System.out.println("=============删=============");
        System.out.println("集合sets中删除元素element003:"+jedis.srem("sets", "element003"));
        System.out.println("查看sets集合中的所有元素:"+jedis.smembers("sets"));
        /*System.out.println("sets集合中任意位置的元素出栈:"+jedis.spop("sets"));//注:出栈元素位置居然不定?--无实际意义
        System.out.println("查看sets集合中的所有元素:"+jedis.smembers("sets"));*/
        System.out.println();

        System.out.println("=============改=============");
        System.out.println();

        System.out.println("=============查=============");
        System.out.println("判断element001是否在集合sets中:"+jedis.sismember("sets", "element001"));
        System.out.println("循环查询获取sets中的每个元素:");
        Set<String> set = jedis.smembers("sets");   
        Iterator<String> it=set.iterator() ;   
        while(it.hasNext()){   
            Object obj=it.next();   
            System.out.println(obj);   
        }  
        System.out.println();

        System.out.println("=============集合运算=============");
        System.out.println("sets1中添加元素element001:"+jedis.sadd("sets1", "element001")); 
        System.out.println("sets1中添加元素element002:"+jedis.sadd("sets1", "element002")); 
        System.out.println("sets1中添加元素element003:"+jedis.sadd("sets1", "element003")); 
        System.out.println("sets1中添加元素element002:"+jedis.sadd("sets2", "element002")); 
        System.out.println("sets1中添加元素element003:"+jedis.sadd("sets2", "element003")); 
        System.out.println("sets1中添加元素element004:"+jedis.sadd("sets2", "element004"));
        System.out.println("查看sets1集合中的所有元素:"+jedis.smembers("sets1"));
        System.out.println("查看sets2集合中的所有元素:"+jedis.smembers("sets2"));
        System.out.println("sets1和sets2交集:"+jedis.sinter("sets1", "sets2"));
        System.out.println("sets1和sets2并集:"+jedis.sunion("sets1", "sets2"));
        System.out.println("sets1和sets2差集:"+jedis.sdiff("sets1", "sets2"));//差集:set1中有,set2中没有的元素

    }

    private void SortedSetOperate() 
    { 
        System.out.println("======================zset=========================="); 
        // 清空数据 
        System.out.println(jedis.flushDB()); 

        System.out.println("=============增=============");
        System.out.println("zset中添加元素element001:"+shardedJedis.zadd("zset", 7.0, "element001")); 
        System.out.println("zset中添加元素element002:"+shardedJedis.zadd("zset", 8.0, "element002")); 
        System.out.println("zset中添加元素element003:"+shardedJedis.zadd("zset", 2.0, "element003")); 
        System.out.println("zset中添加元素element004:"+shardedJedis.zadd("zset", 3.0, "element004"));
        System.out.println("zset集合中的所有元素:"+shardedJedis.zrange("zset", 0, -1));//按照权重值排序
        System.out.println();

        System.out.println("=============删=============");
        System.out.println("zset中删除元素element002:"+shardedJedis.zrem("zset", "element002"));
        System.out.println("zset集合中的所有元素:"+shardedJedis.zrange("zset", 0, -1));
        System.out.println();

        System.out.println("=============改=============");
        System.out.println();

        System.out.println("=============查=============");
        System.out.println("统计zset集合中的元素中个数:"+shardedJedis.zcard("zset"));
        System.out.println("统计zset集合中权重某个范围内(1.0——5.0),元素的个数:"+shardedJedis.zcount("zset", 1.0, 5.0));
        System.out.println("查看zset集合中element004的权重:"+shardedJedis.zscore("zset", "element004"));
        System.out.println("查看下标1到2范围内的元素值:"+shardedJedis.zrange("zset", 1, 2));

    }

    private void HashOperate() 
    { 
        System.out.println("======================hash==========================");
        //清空数据 
        System.out.println(jedis.flushDB()); 

        System.out.println("=============增=============");
        System.out.println("hashs中添加key001和value001键值对:"+shardedJedis.hset("hashs", "key001", "value001")); 
        System.out.println("hashs中添加key002和value002键值对:"+shardedJedis.hset("hashs", "key002", "value002")); 
        System.out.println("hashs中添加key003和value003键值对:"+shardedJedis.hset("hashs", "key003", "value003"));
        System.out.println("新增key004和4的整型键值对:"+shardedJedis.hincrBy("hashs", "key004", 4l));
        System.out.println("hashs中的所有值:"+shardedJedis.hvals("hashs"));
        System.out.println();

        System.out.println("=============删=============");
        System.out.println("hashs中删除key002键值对:"+shardedJedis.hdel("hashs", "key002"));
        System.out.println("hashs中的所有值:"+shardedJedis.hvals("hashs"));
        System.out.println();

        System.out.println("=============改=============");
        System.out.println("key004整型键值的值增加100:"+shardedJedis.hincrBy("hashs", "key004", 100l));
        System.out.println("hashs中的所有值:"+shardedJedis.hvals("hashs"));
        System.out.println();

        System.out.println("=============查=============");
        System.out.println("判断key003是否存在:"+shardedJedis.hexists("hashs", "key003"));
        System.out.println("获取key004对应的值:"+shardedJedis.hget("hashs", "key004"));
        System.out.println("批量获取key001和key003对应的值:"+shardedJedis.hmget("hashs", "key001", "key003")); 
        System.out.println("获取hashs中所有的key:"+shardedJedis.hkeys("hashs"));
        System.out.println("获取hashs中所有的value:"+shardedJedis.hvals("hashs"));
        System.out.println();

    }
}

 

posted @ 2017-03-18 16:30  Alamps  阅读(598)  评论(0编辑  收藏  举报