一.部署redis
1.新建文件夹存放:
mkdir /home/henlafeng/redis cd redis/
2.下载:
http://redis.io/ 官方英文网址 http://www.redis.cn/ 中文网址 wget http://download.redis.io/releases/redis-3.2.9.tar.gz
3.解压:
tar -zxvf redis-3.2.9.tar.gz cd redis-3.2.9
4.编译:
make make install (可以不执行) Redis Test (可以不执行) 运行make命令出现的错误解析: “gcc命令未找到”解决:安装gcc:yum install gcc-c++ (能上网),再次make “Jemalloc/jemalloc.h:没有那个文件或目录”解决:make distclean ,再次make
5.启动redis服务:启动后java即可连接使用,也可使用redis客户端
cd /redis-3.2.9/src ./redis-server 如果修改配置文件了,指定配置文件启动使用: ./redis-server ../redis1.conf (后面加新配置文件的路径)
6.启动客户端:
cd /redis-3.2.9/src ./redis-cli
7.关闭客户端:
单实例关闭:redis-cli shutdown 多实例关闭:指定端口关闭:redis-cli -p6379 shutdown
8.关闭redis服务:
ctrl + c 终止正在执行的命令 或 新开个终端杀掉redis的进程: ps -ef|grep redis 查看进程 kill 进程号 杀掉进程
二.java连接使用redis
1)准备工作:启动redis服务和导入jar包:
commons-pool2-2.4.2.jar jedis-2.9.0.jar
2)测试连接:
Jedis jedis = new Jedis("192.168.31.XX",6379);//参数:ip和redis端口 jedis.auth("XXXXX"); //输入密码 System.out.println(jedis.ping());// 打印pong连接成功
3)使用演示:
键(key)、字符串(String)、列表(List) 集合(Set)、哈希(Hash)、有序集合(Zset/sorted set) 以上是redis的数据类型,根据数据类型顺序一一演示java中常用操作
// key
jedis.flushDB();//清除当前库所有数据 jedis.set("k1","v1");//赋值 jedis.set("k2","v2"); jedis.set("k3","v3"); System.out.println("0exists(k1):" + jedis.exists("k1"));//是否存在:true jedis.move("k3",2);//k3移动到2号库,默认是0号库 System.out.println("0库的key:" + jedis.keys("*")); jedis.select(2);//切换到2号库 System.out.println("2库的key:" + jedis.keys("*"));//查看所有key System.out.println("2exists(k3):" + jedis.exists("k3"));// true jedis.select(0);//切换到0号库 System.out.println("0库的k3的值:" + jedis.get("k3"));// 取值:null System.out.println("0库k1的存活时间:" + jedis.ttl("k1"));///存活时间 System.out.println("0库k2的存活时间:" + jedis.ttl("k2")); jedis.expire("k2",1);//设置k2的有效时间为1秒 Thread.sleep(1000); System.out.println("0库k1的存活时间:" + jedis.ttl("k1"));// -1永久有效 System.out.println("0库k2的存活时间:" + jedis.ttl("k2"));// -2已过期 System.out.println("0库k2的值:" + jedis.get("k2"));// 过期的值get获取不到 jedis.del("k1"); System.out.println("0库k1的值:" + jedis.get("k1"));// del的值get获取不到 // 0库无数据,2库有k3 jedis.select(2); System.out.println("2库k3的值:" + jedis.get("k3"));// v3 jedis.set("k3","vv"); System.out.println("2库k3的值:" + jedis.get("k3"));// vv,新值覆盖旧值 System.out.println("2库k3的类型" + jedis.type("k3"));// 查看类型:string jedis.lpush("mylist","1"); jedis.lpush("mylist","2"); jedis.lpush("mylist","3"); jedis.lpush("mylist","4"); jedis.lpush("mylist","5"); System.out.println("mylist的值:" + jedis.lrange("mylist",-1));// 5,4,3,2,1 System.out.println("mylist的类型:" + jedis.type("mylist"));// list
// String
jedis.flushDB(); jedis.set("k1","v1"); jedis.append("k1","2345");// k1值后面追加2345 System.out.println(jedis.get("k1"));// v12345 System.out.println(jedis.strlen("k1"));// 长度:6 jedis.set("k2","4"); jedis.incr("k2");// 每次加1 jedis.incr("k2"); jedis.incr("k2"); jedis.decr("k2");// 每次减1 jedis.decr("k2"); System.out.println(jedis.get("k2"));// 5 jedis.incrBy("k2",5);// 加5 jedis.decrBy("k2",4);// 减4 System.out.println(jedis.get("k2"));// 6 System.out.println(jedis.getrange("k1",2));// 下标0到下标2:v12 System.out.println(jedis.getrange("k1",-1));// 查看k1值:v12345 jedis.setrange("k1","DDD");// 下标0开始的值改为DDD,覆盖原位置相应的值 System.out.println(jedis.get("k1"));// DDD345 System.out.println(jedis.setnx("k1","v1"));// k1不存在就set进去:0 k1存在set失败 System.out.println(jedis.setnx("k3","v3"));// k3不存在就set进去:1 k3不存在set成功 jedis.mset("k1","v1","k2","v2","k3","v3");// 同时赋值多个,逗号间隔 System.out.println(jedis.mget("k1","k3"));// [v1,v2,v3] System.out.println(jedis.msetnx("k3","v3","k4","v4"));// 不存在就赋值多个:0 // k3存在所以赋值失败 System.out.println(jedis.msetnx("k4","v4","k5","v5"));// 1 System.out.println(jedis.mget("k4","k5"));// [v4,v5]
// list
jedis.select(0); jedis.flushDB(); jedis.lpush("list01","1");// 左边存入 lpush的list01:[5,1] jedis.lpush("list01","2"); jedis.lpush("list01","3"); jedis.lpush("list01","4"); jedis.lpush("list01","5"); jedis.rpush("list02","1");// 右边存入 rpush的list02:[1,5] jedis.rpush("list02","2"); jedis.rpush("list02","3"); jedis.rpush("list02","4"); jedis.rpush("list02","5"); List<String> lrange01 = jedis.lrange("list01",-1);//查看所有 List<String> lrange02 = jedis.lrange("list02",-1); System.out.println("lpush的list01:" + lrange01.toString());// 正进反出 System.out.println("rpush的list02:" + lrange02.toString());// 正进正出 String lpop01 = jedis.lpop("list01");// 左边出栈 String lpop02 = jedis.lpop("list02");// 左边出栈 System.out.println("list01的lpop:" + lpop01);// list01的lpop:5 System.out.println("list02的lpop:" + lpop02);// list02的lpop:1 String rpop01 = jedis.rpop("list01"); String rpop02 = jedis.rpop("list02"); System.out.println("list01的rpop:" + rpop01);// list01的rpop:1 System.out.println("list02的rpop:" + rpop02);// list02的rpop:5 System.out.println(jedis.lindex("list01",2));// 下标3的值:2 System.out.println(jedis.lindex("list02",2));// 下标3的值:4 System.out.println(jedis.llen("list01"));// 集合长度:3 System.out.println(jedis.llen("list02"));// 集合长度:3 System.out.println(jedis.lindex("list01",3));// null System.out.println(jedis.lindex("list02",3));// null jedis.rpush("list03","1"); jedis.rpush("list03","2"); jedis.rpush("list03","3"); jedis.rpush("list03","4"); jedis.rpush("list03","5"); jedis.rpush("list03","6"); jedis.lrem("list03","1");// 移除2个1 jedis.lrem("list03","2");// 移除2个2 jedis.lrem("list03","3");// 移除2个3 System.out.println(jedis.lrange("list03",-1));// [1,5,6] jedis.del("list01");//删除元素 jedis.lpush("list01","1"); jedis.lpush("list01","5"); jedis.lpush("list01","6"); jedis.lpush("list01","7"); jedis.lpush("list01","8"); jedis.ltrim("list01",4);// 截取下标0到4 System.out.println(jedis.lrange("list01",-1));// [8,7,6,4] jedis.ltrim("list03",4);// 截取下标0到4 System.out.println(jedis.lrange("list03",5] jedis.rpoplpush("list01","list03");// list01的尾数放到list03的头值 System.out.println(jedis.lrange("list01",5] System.out.println(jedis.lrange("list03",-1));// [4,1,5] jedis.lset("list03","6");// list03下标0的值为6 System.out.println(jedis.lrange("list03",-1));// [6,5] jedis.linsert("list01",LIST_POSITION.BEFORE,"7","java");// list01的值7前插入值java jedis.linsert("list01",LIST_POSITION.AFTER,"6","redis");// list01的值6后插入值redis System.out.println(jedis.lrange("list01",java,// redis,5] jedis.sort("list01");// 排序
// set
jedis.flushDB(); jedis.sadd("set01","1"); jedis.sadd("set01","2"); jedis.sadd("set01","3"); jedis.sadd("set01","3"); System.out.println(jedis.smembers("set01"));// 查询所有:[1,3] System.out.println(jedis.sismember("set01","1"));// 是否存在值1:true System.out.println(jedis.scard("set01"));// 获取元素个数:3 jedis.srem("set01","1");// 移除值1 System.out.println(jedis.smembers("set01"));// [2,3] jedis.sadd("set02","1"); jedis.sadd("set02","2"); jedis.sadd("set02","3"); jedis.sadd("set02","4"); jedis.sadd("set02","5"); jedis.sadd("set02","6"); jedis.sadd("set02","7"); jedis.srandmember("set02",3);// 随机取3个数字 System.out.println(jedis.srandmember("set02",3));// [5,6] [1,6]... jedis.spop("set01");// 随机出栈 System.out.println(jedis.spop("set01"));// 3 2 System.out.println(jedis.smembers("set01"));// [] 出栈就没了 jedis.sadd("set03","x"); jedis.sadd("set03","y"); jedis.sadd("set03","z"); jedis.smove("set02","set03","4");// set02的4移动到set03 System.out.println(jedis.smembers("set02"));// [1,7] System.out.println(jedis.smembers("set03"));// [4,y,z,x] // 数学集合类 jedis.del("set01"); jedis.sadd("set01","a"); jedis.sadd("set01","b"); jedis.sdiff("set01","set02");// 差集:在第一个set里面而不在后面任何一个set里面的项 System.out.println(jedis.sdiff("set01","set02"));// 以set01为标准的差集: [b,// a] System.out.println(jedis.sdiff("set02","set01"));// 以set02为标准的差集:[7,// 5] jedis.sinter("set01","set02");// 交集:每个set都有的项 System.out.println(jedis.sinter("set01","set02"));// [2,3] jedis.sunion("set01","set02");// 并集:所有set的总和 System.out.println(jedis.sunion("set01","set02"));//[2,b,a,3]
// hash-map
jedis.flushDB(); jedis.hset("user","id","11"); System.out.println(jedis.hget("user","id"));// 11 jedis.hset("user","name","z3"); System.out.println(jedis.hget("user","name"));// z3 Map<String,String> map = new HashMap<String,String>(); map.put("name","l4"); map.put("age","26"); jedis.hmset("customer",map); List<String> list01 = jedis.hmget("customer","name"); System.out.println(list01);// [l4] jedis.hgetAll("customer");// 获取customer所有信息 System.out.println(jedis.hgetAll("customer"));// {name=l4,age=26} jedis.hdel("user","name");// 删除user的name System.out.println(jedis.hgetAll("user"));// 查看所有:{id=11} System.out.println(jedis.hlen("customer"));// 长度:2 System.out.println(jedis.hlen("user"));// 长度:1 jedis.hexists("user","name");// 是否存在name System.out.println(jedis.hexists("user","name"));// false System.out.println(jedis.hexists("customer","name"));// true System.out.println(jedis.hkeys("customer"));// 获取所有key:[age,name] System.out.println(jedis.hvals("customer"));// 获取所有value:[26,l4] System.out.println(jedis.hincrBy("customer","age",2));// age加2:28 jedis.hset("customer","score","91.5"); System.out.println(jedis.hincrByFloat("customer",0.5));// score加0.5:92.0 jedis.hsetnx("user","Z3");// 不存在就set System.out.println(jedis.hsetnx("customer","18"));// 0 jedis.hsetnx("customer","email","123@163.com"); Iterator<String> iterator = jedis.hkeys("customer").iterator(); while (iterator.hasNext()) { String key = iterator.next(); System.out.println("key:" + key);// key:score key:email key:age // key:name }
// zset
jedis.flushDB(); jedis.zadd("zset01",60,"v1"); jedis.zadd("zset01",70,"v2"); jedis.zadd("zset01",80,"v3"); jedis.zadd("zset01",90,"v4"); jedis.zadd("zset01",100,"v5"); System.out.println(jedis.zrange("zset01",-1));// [v1,v3,v4,// v5] System.out.println(jedis.zrangeByscore("zset01",90));// [v1,// v3,v4] System.out.println(jedis.zrangeByscore("zset01",2));// 60-90之间取下标2开始的2个值:[v3,// v4] jedis.zrem("zset01","v5");//移除v5元素 System.out.println(jedis.zrange("zset01",v4] System.out.println(jedis.zcard("zset01"));//长度:4 System.out.println(jedis.zcount("zset01",80));//60-80的个数:3 System.out.println(jedis.zrank("zset01","v4"));//下标值:3 System.out.println(jedis.zscore("zset01","v4"));//v3的值:90.0 System.out.println(jedis.zrevrank("zset01","v4"));//逆序获得下标值:0 System.out.println(jedis.zrevrangeByscore("zset01",60));//逆序获取90-60的值:[v4,v1]