1 package com.jedis.test; 2 3 import redis.clients.jedis.Jedis; 4 5 /** 6 * Created by Administrator on 2017-07-13. 7 */ 8 public class JedisDemo1 { 9 /** 10 * 单实例测试 11 */ 12 public static void main(String[] args) { 13 //设置IP地址和端口 14 Jedis jedis=new Jedis("192.168.21.195",6379); 15 //保存数据 16 jedis.set("name","lu"); 17 //获取数据 18 String value=jedis.get("name"); 19 System.out.println("name对应的:"+value); 20 //释放资源 21 jedis.close(); 22 } 23 }
1 package com.jedis.test; 2 3 4 import redis.clients.jedis.Jedis; 5 import redis.clients.jedis.JedisPool; 6 import redis.clients.jedis.JedisPoolConfig; 7 8 import java.util.Date; 9 10 /** 11 *连接池的方式连接 12 */ 13 public class JedisDemo2 { 14 public static void main(String[] args) { 15 //获取连接池的配置 16 JedisPoolConfig config = new JedisPoolConfig(); 17 //设置最大连接数 18 config.setMaxTotal(30); 19 //设置最大空闲连接数 20 config.setMaxIdle(10); 21 //获得连接池 22 JedisPool jedisPool=new JedisPool(config,"192.168.21.195",6379); 23 //获得核心对象 24 Jedis jedis=null; 25 try { 26 //通过连接池获得连接 27 jedis=jedisPool.getResource(); 28 //设置数据 29 jedis.set("name","张三"); 30 //获取数据 31 String value = jedis.get("name"); 32 System.out.println("name对应的value:"+value); 33 }catch (Exception e){ 34 e.printStackTrace(); 35 }finally { 36 if (jedis!=null){ 37 jedis.close(); 38 } 39 if (jedisPool!=null){ 40 jedisPool.close(); 41 } 42 } 43 } 44 45 }
redis 127.0.0.1:6379> set company lu OK redis 127.0.0.1:6379> get company "lu" redis 127.0.0.1:6379> getset company baidu "lu" redis 127.0.0.1:6379> get company "baidu" redis 127.0.0.1:6379> del company (integer) 1 redis 127.0.0.1:6379> get company (nil) redis 127.0.0.1:6379> incr num (integer) 1 redis 127.0.0.1:6379> get num "1" redis 127.0.0.1:6379> incr num (integer) 2 redis 127.0.0.1:6379> get num "2" redis 127.0.0.1:6379> incr name (error) ERR value is not an integer or out of range redis 127.0.0.1:6379> decr num (integer) 1 redis 127.0.0.1:6379> decr num2 (integer) -1 redis 127.0.0.1:6379> incrby num 5 (integer) 6 redis 127.0.0.1:6379> incrby num3 5 (integer) 5 redis 127.0.0.1:6379> decrby num3 2 (integer) 3 redis 127.0.0.1:6379> append num 5 (integer) 2 redis 127.0.0.1:6379> get num "65" redis 127.0.0.1:6379> append num4 4 (integer) 1 redis 127.0.0.1:6379> get num4 "4"
13、存储Hash
String Key和String Value的map容器
每一个Hash可以存储4294967295个键值对
储存Hash常用命令:
赋值
取值删除增加数字
自学命令
1 redis 127.0.0.1:6379> hset myhash username jack 2 (integer) 1 3 redis 127.0.0.1:6379> hset myhash age 18 4 (integer) 1 5 redis 127.0.0.1:6379> hmset myhash2 username rose age 21 6 OK 7 redis 127.0.0.1:6379> hget myhash username 8 "jack" 9 redis 127.0.0.1:6379> hget myhash age 10 "18" 11 redis 127.0.0.1:6379> hmget myhash username age 12 1) "jack" 13 2) "18" 14 redis 127.0.0.1:6379> hgetall myhash 15 1) "username" 16 2) "jack" 17 3) "age" 18 4) "18" 19 redis 127.0.0.1:6379> hdel myhash2 username 20 (integer) 1 21 redis 127.0.0.1:6379> hgetall myhash2 22 1) "age" 23 2) "21" 24 redis 127.0.0.1:6379> del myhash2 25 (integer) 1 26 redis 127.0.0.1:6379> hgetall myhash2 27 (empty list or set) 28 redis 127.0.0.1:6379> hincrby myhash age 5 29 (integer) 23 30 redis 127.0.0.1:6379> hexists myhash username 31 (integer) 1 32 redis 127.0.0.1:6379> hexists myhash password 33 (integer) 0 34 redis 127.0.0.1:6379> hlen myhash 35 (integer) 2 36 redis 127.0.0.1:6379> hkeys myhash 37 1) "username" 38 2) "age" 39 redis 127.0.0.1:6379> hvalues myhash 40 (error) ERR unknown command 'hvalues' 41 redis 127.0.0.1:6379> hvals myhash 42 1) "jack" 43 2) "23"
14、存储list
ArrayList使用数组方式
LinkedList使用双向链接方式
双向链表中增加数据
双向链表中删除数据
存储list常用命令:
两端添加
两端弹出
扩展命令
查看列表
获取列表元素个数
1 redis 127.0.0.1:6379> lpush mylist a b c 2 (integer) 3 3 redis 127.0.0.1:6379> lpush mylist 1 2 3 4 (integer) 6 5 redis 127.0.0.1:6379> rpush mylist2 a b c 6 (integer) 3 7 redis 127.0.0.1:6379> rpush mylist2 1 2 3 8 (integer) 6 9 redis 127.0.0.1:6379> lrange mylist 0 5 10 1) "3" 11 2) "2" 12 3) "1" 13 4) "c" 14 5) "b" 15 6) "a" 16 redis 127.0.0.1:6379> lrange mylist2 0 -1 17 1) "a" 18 2) "b" 19 3) "c" 20 4) "1" 21 5) "2" 22 6) "3" 23 redis 127.0.0.1:6379> lrange mylist2 0 -2 24 1) "a" 25 2) "b" 26 3) "c" 27 4) "1" 28 5) "2" 29 redis 127.0.0.1:6379> lpop mylist 30 "3" 31 redis 127.0.0.1:6379> lrange mylist 0 -1 32 1) "2" 33 2) "1" 34 3) "c" 35 4) "b" 36 5) "a" 37 redis 127.0.0.1:6379> rpop mylist 38 "a" 39 redis 127.0.0.1:6379> lrange mylist2 0 -1 40 1) "a" 41 2) "b" 42 3) "c" 43 4) "1" 44 5) "2" 45 6) "3" 46 redis 127.0.0.1:6379> rpop mylist2 47 "3" 48 redis 127.0.0.1:6379> rpop mylist2 49 "2" 50 redis 127.0.0.1:6379> lrange mylist2 0 -1 51 1) "a" 52 2) "b" 53 3) "c" 54 4) "1" 55 redis 127.0.0.1:6379> llen mylist 56 (integer) 4 57 redis 127.0.0.1:6379> llen mylist2 58 (integer) 4 59 redis 127.0.0.1:6379> llen mylist3 60 (integer) 0 61 redis 127.0.0.1:6379> lpushx mylist x 62 (integer) 5 63 redis 127.0.0.1:6379> lrange mylist 0 -1 64 1) "x" 65 2) "2" 66 3) "1" 67 4) "c" 68 5) "b" 69 redis 127.0.0.1:6379> lpushx mylist3 x 70 (integer) 0 71 redis 127.0.0.1:6379> rpushx mylist2 x 72 (integer) 5 73 redis 127.0.0.1:6379> lrange mylist2 74 (error) ERR wrong number of arguments for 'lrange' command 75 redis 127.0.0.1:6379> lrange mylist2 0 -1 76 1) "a" 77 2) "b" 78 3) "c" 79 4) "1" 80 5) "x" 81 redis 127.0.0.1:6379> lpush mylist3 1 2 3 82 (integer) 3 83 redis 127.0.0.1:6379> lpush mylist3 1 2 3 84 (integer) 6 85 redis 127.0.0.1:6379> lpush mylist3 1 2 3 86 (integer) 9 87 redis 127.0.0.1:6379> lrange mylist3 0 -1 88 1) "3" 89 2) "2" 90 3) "1" 91 4) "3" 92 5) "2" 93 6) "1" 94 7) "3" 95 8) "2" 96 9) "1" 97 redis 127.0.0.1:6379> lrem mylist3 2 3 98 (integer) 2 99 redis 127.0.0.1:6379> lrange mylist3 0 -1 100 1) "2" 101 2) "1" 102 3) "2" 103 4) "1" 104 5) "3" 105 6) "2" 106 7) "1" 107 redis 127.0.0.1:6379> lrem mylist3 -2 1 108 (integer) 2 109 redis 127.0.0.1:6379> lrange mylist3 0 -1 110 1) "2" 111 2) "1" 112 3) "2" 113 4) "3" 114 5) "2" 115 redis 127.0.0.1:6379> lrem mylist3 0 2 116 (integer) 3 117 redis 127.0.0.1:6379> lrange mylist3 0 -1 118 1) "1" 119 2) "3" 120 redis 127.0.0.1:6379> lrange mylist 0 -1 121 1) "x" 122 2) "2" 123 3) "1" 124 4) "c" 125 5) "b" 126 redis 127.0.0.1:6379> lset mylist 3 mmm 127 OK 128 redis 127.0.0.1:6379> lrange mylist 0 -1 129 1) "x" 130 2) "2" 131 3) "1" 132 4) "mmm" 133 5) "b" 134 redis 127.0.0.1:6379> lpush mylist4 a b c 135 (integer) 3 136 redis 127.0.0.1:6379> lpush mylist4 a b c 137 (integer) 6 138 redis 127.0.0.1:6379> lrange mylist4 0 -1 139 1) "c" 140 2) "b" 141 3) "a" 142 4) "c" 143 5) "b" 144 6) "a" 145 redis 127.0.0.1:6379> linsert mylist4 before b 11 146 (integer) 7 147 redis 127.0.0.1:6379> lrange mylist4 0 -1 148 1) "c" 149 2) "11" 150 3) "b" 151 4) "a" 152 5) "c" 153 6) "b" 154 7) "a" 155 redis 127.0.0.1:6379> linsert mylist4 after b 22 156 (integer) 8 157 redis 127.0.0.1:6379> lrange mylist4 0 -1 158 1) "c" 159 2) "11" 160 3) "b" 161 4) "22" 162 5) "a" 163 6) "c" 164 7) "b" 165 8) "a" 166 redis 127.0.0.1:6379> lpush mylist5 1 2 3 167 (integer) 3 168 redis 127.0.0.1:6379> lpush mylist6 a b c 169 (integer) 3 170 redis 127.0.0.1:6379> rpoplpush mylist5 mylist6 171 "1" 172 redis 127.0.0.1:6379> lrange mylist5 0 -1 173 1) "3" 174 2) "2" 175 redis 127.0.0.1:6379> lrange mylist6 0 -1 176 1) "1" 177 2) "c" 178 3) "b" 179 4) "a"
15、存储Set
和List类型不同的是,Set集合中不允许出现重复的元素
存储set常用命令:
添加/删除元素
获得集合中的元素
集合中的差集运算
集合中的交集运算
集合中的并集运算
扩展命令
1 redis 127.0.0.1:6379> sadd myset a b c 2 (integer) 3 3 redis 127.0.0.1:6379> sadd myset a 4 (integer) 0 5 redis 127.0.0.1:6379> sadd myset 1 2 3 6 (integer) 3 7 redis 127.0.0.1:6379> srem myset 1 2 8 (integer) 2 9 redis 127.0.0.1:6379> smembers myset 10 1) "3" 11 2) "c" 12 3) "a" 13 4) "b" 14 redis 127.0.0.1:6379> sismember myset a 15 (integer) 1 16 redis 127.0.0.1:6379> sismember myset 6 17 (integer) 0 18 redis 127.0.0.1:6379> saaa mya1 a b c 19 (error) ERR unknown command 'saaa' 20 redis 127.0.0.1:6379> sadd mya1 a b c 21 (integer) 3 22 redis 127.0.0.1:6379> sadd mya2 a c 1 2 23 (integer) 4 24 redis 127.0.0.1:6379> sdiff mya1 mya2 25 1) "b" 26 redis 127.0.0.1:6379> sinter mya1 mya2 27 1) "c" 28 2) "a" 29 redis 127.0.0.1:6379> sunion mya1 mya2 30 1) "1" 31 2) "a" 32 3) "b" 33 4) "c" 34 5) "2" 35 redis 127.0.0.1:6379> smembers myset 36 1) "3" 37 2) "c" 38 3) "a" 39 4) "b" 40 redis 127.0.0.1:6379> scard myset 41 (integer) 4 42 redis 127.0.0.1:6379> srandmember myset 43 "a" 44 redis 127.0.0.1:6379> srandmember myset 45 "a" 46 redis 127.0.0.1:6379> srandmember myset 47 "3" 48 redis 127.0.0.1:6379> sdiffstore my1 mya1 mya2 49 (integer) 1 50 redis 127.0.0.1:6379> smembers my1 51 1) "b" 52 redis 127.0.0.1:6379> sinterstore my2 mya1 mya2 53 (integer) 2 54 redis 127.0.0.1:6379> smembers my2 55 1) "c" 56 2) "a" 57 redis 127.0.0.1:6379> sunionstore my3 mya1 mya2 58 (integer) 5 59 redis 127.0.0.1:6379> smembers my3 60 1) "1" 61 2) "a" 62 3) "b" 63 4) "c" 64 5) "2"
存储Set使用场景
跟踪一些唯一性数据
用于维护数据对象之间的关联关系
16、存储Sorted-Set
Sorted-Set中的成员在集合中的位置是有序的
存储Sorted-Set常用命令:
添加元素
删除元素
获得元素
范围查询
扩展命令
1 redis 127.0.0.1:6379> zadd mysort 70 zs 80 ls 90 ww 2 (integer) 3 3 redis 127.0.0.1:6379> zadd mysort zs 4 (error) ERR wrong number of arguments for 'zadd' command 5 redis 127.0.0.1:6379> zadd mysort 100 zs 6 (integer) 0 7 redis 127.0.0.1:6379> zadd mysort 60 tom 8 (integer) 1 9 redis 127.0.0.1:6379> zscore mysort zs 10 "100" 11 redis 127.0.0.1:6379> zcard mysort 12 (integer) 4 13 redis 127.0.0.1:6379> zrem mysort tom ww 14 (integer) 2 15 redis 127.0.0.1:6379> zcard mysort 16 (integer) 2 17 redis 127.0.0.1:6379> zadd mysort 85 jack 95 rose 18 (integer) 2 19 redis 127.0.0.1:6379> zrange mysort 0 -1 20 1) "ls" 21 2) "jack" 22 3) "rose" 23 4) "zs" 24 redis 127.0.0.1:6379> zrange mysort 0 -1 withscores 25 1) "ls" 26 2) "80" 27 3) "jack" 28 4) "85" 29 5) "rose" 30 6) "95" 31 7) "zs" 32 8) "100" 33 redis 127.0.0.1:6379> zrevrange mysort 0 -1 withscores 34 1) "zs" 35 2) "100" 36 3) "rose" 37 4) "95" 38 5) "jack" 39 6) "85" 40 7) "ls" 41 8) "80" 42 redis 127.0.0.1:6379> zremrangebyrank mysort 0 4 43 (integer) 4 44 redis 127.0.0.1:6379> zrevrange mysort 0 -1 withscores 45 (empty list or set) 46 redis 127.0.0.1:6379> zadd mysort 80 zs 90 ls 100 ww 47 (integer) 3 48 redis 127.0.0.1:6379> zremrangebyscore mysort 80 100 49 (integer) 3 50 redis 127.0.0.1:6379> zrevrange mysort 0 -1 withscores 51 (empty list or set) 52 redis 127.0.0.1:6379> zadd mysort 70 zs 80 ls 90 ww 53 (integer) 3 54 redis 127.0.0.1:6379> zrangebyscore mysort 0 100 55 1) "zs" 56 2) "ls" 57 3) "ww" 58 redis 127.0.0.1:6379> zrangebyscore mysort 0 100 withscore 59 (error) ERR syntax error 60 redis 127.0.0.1:6379> zrangebyscore mysort 0 100 withscores 61 1) "zs" 62 2) "70" 63 3) "ls" 64 4) "80" 65 5) "ww" 66 6) "90" 67 redis 127.0.0.1:6379> byscore mysort 0 100 withscores limit 0 2 68 1) "zs" 69 2) "70" 70 3) "ls" 71 4) "80" 72 redis 127.0.0.1:6379> zincrby mysort 3 ls 73 "83" 74 redis 127.0.0.1:6379> zscore mysort ls 75 "83" 76 redis 127.0.0.1:6379> zcount mysort 80 90 77 (integer) 2
Sorted-Set使用场景
大型在线游戏积分排行榜
构建索引数据
17、Redis特性
切换数据库
select 0
移动key到某一个数据库
move key 1
开启事务
multi
提交事务
exec
回滚事务
discard
18、Redis持久化
持久化方式:
RDB方式(配置以多少秒将数据储存到硬盘)
AOF方式(读取日志文件)
无持久化(缓存方式)
同时使用RDB和AOF