🐴 一、Redis配置
🌈 1.1 搭redis环境
#解压tar包 /opt
tar -zxvf redis-5.0.7.tar.gz
#基本环境搭建,gcc是Gnu的C语言编译bai器
yum install gcc-c++
make
make install
#默认安装路径 /usr/local/bin
#创建配置文件地址 /usr/local/bin/qconfig
#拷贝配置文件
cp /opt/redis-5.0.7/redis.conf qconfig
#修改为后台启用,修改配置文件
daemonize yes
🚀🚀🚀 启动redis
#进入启动目录 cd /usr/local/bin #启动server redis-server qconfig/redis.conf #启动cli redis-cli -p 6379 #查看和设置redis密码 127.0.0.1:6379> config get requirepass 1) "requirepass" 2) "" 127.0.0.1:6379> config set requirepass "1234" OK #登录redis 127.0.0.1:6379> auth 1234 OK #查看redis是否启动,另起一个窗口 ps -ef | grep redis #关闭redis服务 127.0.0.1:6379> shutdown not connected> exit #再次查看redis是否启动 ps -ef | grep redis #切换数据库,默认为0 127.0.0.1:6379> select 3 OK #查看数据库大小 127.0.0.1:6379[3]> dbsize (integer) 0 #清除当前数据库 127.0.0.1:6379[3]> flushdb OK #清除所有数据库 127.0.0.1:6379[3]> flushall OK
🌈 1.2 常用命令
🐴 二、redis的数据类型
🌈 2.1 String
#追加字符串,没有的话就新建
append name “wangqin”
#字符串长度
strlen name
#i++
set count 0
incr count
#i–
decr count
#指定增量 count = count + 10
incrby count 10
#指定减量 count = count - 10
decrby count 10
#截取字符串 wangqin
getrange key 0 3
“wang”
#替换
setrange key 1 qq
“wqqgqin”
#设置过期时间
setex key 30 “hello”
#不存在再设置,往往在分布式中使用,如果key1不存在就创建,存在就创建失败
setnx key1 “redis”
#批量设置
mset k1 v1 k2 v2 k3 v3
#批量获取
mget k1 k2
#要么一起成功,要么一起失败,保持原子性
msetnx k1 v1 k2 v2
#先获取在写入,不存在值为nil,存在的话获取原来的值在设置新的值
getset
🌈 2.2 List
#将一个值或者多个值插入list的头部 127.0.0.1:6379> lpush list one (integer) 1 127.0.0.1:6379> lpush list two (integer) 2 127.0.0.1:6379> lpush list three (integer) 3 #查看list,头->尾 127.0.0.1:6379> lrange list 0 -1 1) "three" 2) "two" 3) "one" 127.0.0.1:6379> lrange list 0 1 1) "three" 2) "two" 127.0.0.1:6379> rpush list right (integer) 4 #将一个值或者多个值插入list的尾部 127.0.0.1:6379> lrange list 0 -1 1) "three" 2) "two" 3) "one" 4) "right" #移除list第一个元素 127.0.0.1:6379> lpop list "three" 127.0.0.1:6379> lrange list 0 -1 1) "two" 2) "one" 3) "right" #移除list最后一个元素 127.0.0.1:6379> rpop list "right" 127.0.0.1:6379> lrange list 0 -1 1) "two" 2) "one" #根据下标获取值 127.0.0.1:6379> lindex list 0 #获取list长度 127.0.0.1:6379> llen list (integer) 2 127.0.0.1:6379> lrange list 0 -1 1) "three" 2) "two" 3) "two" 4) "one" #从上至下删除一个 two 127.0.0.1:6379> lrem list 1 two (integer) 1 127.0.0.1:6379> lrange list 0 -1 1) "three" 2) "two" 3) "one" 127.0.0.1:6379>
🌈 2.3 Set
#添加Set元素 127.0.0.1:6379> sadd myset v1 v2 v3 v4 (integer) 4 #显示所有myset集合中的元素 127.0.0.1:6379> smembers myset 1) "v1" 2) "v3" 3) "v4" 4) "v2" #查询v1是否存在myset集合中,存在就返回1,不存在就返回0 127.0.0.1:6379> sismember myset v1 (integer) 1 127.0.0.1:6379> sismember myset v6 (integer) 0 127.0.0.1:6379> sadd uset k1 k2 k3 v1 (integer) 4 #合并集合myset和uset到集合set中,set重复的取一个就行 127.0.0.1:6379> sunionstore set myset uset (integer) 7 #合并显示集合myset和uset 127.0.0.1:6379> sunion myset uset 1) "v1" 2) "v3" 3) "k1" 4) "k3" 5) "v4" 6) "k2" 7) "v2" #set集合的长度 127.0.0.1:6379> scard set (integer) 6 #随机删除set中的元素 127.0.0.1:6379> spop set "v4" #移动指定的一个值到其他的集合中set->myset 127.0.0.1:6379> smove set myset k1 #两个集合的差集 127.0.0.1:6379> smembers set 1) "v3" 2) "k3" 3) "k2" 4) "v2" 127.0.0.1:6379> smembers myset 1) "v1" 2) "v3" 3) "v4" 4) "v2" 5) "k1" 127.0.0.1:6379> sdiff myset set 1) "v1" 2) "k1" 3) "v4" #两个集合的交集 127.0.0.1:6379> sinter myset set 1) "v3" 2) "v2" #两个集合的并集 127.0.0.1:6379> sunion myset set 1) "v1" 2) "v3" 3) "k1" 4) "k3" 5) "v4" 6) "k2" 7) "v2"
🌈 2.4 Hash(哈希)
#set一个具体的 key-value 127.0.0.1:6379> hset myhash name wangqin (integer) 1 #获取一个字段值 127.0.0.1:6379> hget myhash name "wangqin" #set一组 key-value 127.0.0.1:6379> hmset myhash name wangqin1 age 18 OK #获取一组 key-value 127.0.0.1:6379> hmget myhash name age 1) "wangqin1" 2) "18" #获取全部数据 127.0.0.1:6379> hgetall myhash 1) "name" 2) "wangqin1" 3) "age" 4) "18" #删除指定的key 127.0.0.1:6379> hdel myhash name (integer) 1 127.0.0.1:6379> hgetall myhash 1) "age" 2) "18" 127.0.0.1:6379> hmset myhash name wangqin set male OK #获取hash表的字段数量 127.0.0.1:6379> hlen myhash (integer) 3 #hash表中是否存在key 127.0.0.1:6379> hexists myhash name (integer) 1 127.0.0.1:6379> hexists myhash name1 (integer) 0 #获取hash表的key 127.0.0.1:6379> hkeys myhash 1) "age" 2) "name" 3) "set" #获取hash表的值 127.0.0.1:6379> hvals myhash 1) "18" 2) "wangqin" 3) "male"
🌈 2.5 Zset(有序集合)
#添加zset元素 127.0.0.1:6379> zadd zset 1 one (integer) 1 #批量添加zset元素 127.0.0.1:6379> zadd zset 2 two 3 three (integer) 2 #获取zset中所有元素 127.0.0.1:6379> zrange zset 0 -1 1) "one" 2) "two" 3) "three" #倒序查询 127.0.0.1:6379> zrevrange zset 0 -1 1) "three" 2) "two" 3) "one" 127.0.0.1:6379> zadd zset -1 -one (integer) 1 127.0.0.1:6379> zadd zset -2 -two (integer) 1 #正序排序,从-∞到+∞ 127.0.0.1:6379> zrangebyscore zset -inf +inf 1) "-two" 2) "-one" 3) "one" 4) "two" 5) "three" 127.0.0.1:6379> zrangebyscore zset -1 +inf 1) "-one" 2) "one" 3) "two" 4) "three" #倒序排序,从+∞到-∞ 127.0.0.1:6379> zrevrangebyscore zset +inf -1 1) "three" 2) "two" 3) "one" 4) "-one" 127.0.0.1:6379> zrem zset one (integer) 1 127.0.0.1:6379> zrange zset 0 -1 1) "-two" 2) "-one" 3) "two" 4) "three" #集合的长度 127.0.0.1:6379> zcard zset (integer) 4
🌈 2.6 geospatital(地理位置)
#添加地理位置 127.0.0.1:6379> geoadd China:city 116.42 29.90 home (integer) 1 127.0.0.1:6379> geoadd China:city 117.30 31.79 hefei (integer) 1 127.0.0.1:6379> geoadd China:city 120.21 30.20 hangzhou (integer) 1 127.0.0.1:6379> geoadd China:city 121.48 31.40 shenzhen (integer) 1 127.0.0.1:6379> geoadd China:city 106.54 29.40 chongqing (integer) 1 #获取纬度和经度 127.0.0.1:6379> geopos China:city home 1) 1) "116.41999751329421997" 2) "29.90000043079877656" #获取两地距离 127.0.0.1:6379> geodist China:city home hefei "226388.9875" 127.0.0.1:6379> geodist China:city home hefei km "226.3890" # 距离115 30范围300km的地方 127.0.0.1:6379> georadius China:city 115 30 300 km 1) "home" 2) "hefei"
🌈 2.7 Hyperloglog
如果可以允许容错的话可以使用Hyperloglog,0.81%的容错率
#创建元素
127.0.0.1:6379> pfadd mykey a b c d e f g
(integer) 1
#统计mykey元素的基数数量
127.0.0.1:6379> pfcount mykey
(integer) 7
127.0.0.1:6379> pfadd ukey q w e r t y u i o p
(integer) 1
#合并两组 mkey ukey->key,会去重
127.0.0.1:6379> pfmerge key mykey ukey
OK
127.0.0.1:6379> pfcount key
(integer) 15
🌈 2.8 Bitmap(位储存)
位图,只有两种状态的,操作二进制来进行记录,只有0 1两个状态
#添加元素
127.0.0.1:6379> setbit sign 0 1
(integer) 0
127.0.0.1:6379> setbit sign 1 1
(integer) 0
127.0.0.1:6379> setbit sign 2 0
(integer) 0
127.0.0.1:6379> setbit sign 3 0
(integer) 0
#查询元素
127.0.0.1:6379> getbit sign 3
(integer) 0
127.0.0.1:6379> getbit sign 1
(integer) 1
#统计次数
127.0.0.1:6379> bitcount sign
(integer) 3
🐴 三、事务
#开启一组事务 127.0.0.1:6379> multi OK 127.0.0.1:6379> set k1 v1 QUEUED 127.0.0.1:6379> set k2 v2 QUEUED 127.0.0.1:6379> get k2 QUEUED 127.0.0.1:6379> set k3 v3 QUEUED #执行事务 127.0.0.1:6379> exec 1) OK 2) OK 3) "v2" 4) OK #放弃事务 127.0.0.1:6379> multi OK 127.0.0.1:6379> set k4 v4 QUEUED 127.0.0.1:6379> set k5 v5 QUEUED 127.0.0.1:6379> discard OK 127.0.0.1:6379> get v4 (nil)
🐴 四、Jedis
🚀🚀🚀 导入依赖
<dependencies> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58</version> </dependency> </dependencies>
🚀🚀🚀测试连接
public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1", 6379); System.out.println("连接成功"); //查看服务是否运行 System.out.println("服务正在运行: "+ jedis.ping()); }
🚀🚀🚀Jedis的API
Jedis jedis = new Jedis("127.0.0.1", 6379); System.out.println("清空数据:"+jedis.flushDB()); System.out.println("判断某个键是否存在:"+jedis.exists("username")); System.out.println("新增<'username','wangqin'>的键值对:"+jedis.set("username", "wangqin")); System.out.println("新增<'password','password'>的键值对:"+jedis.set("password", "password")); System.out.print("系统中所有的键如下:"); Set<String> keys = jedis.keys("*"); System.out.println(keys); System.out.println("删除键password:"+jedis.del("password")); System.out.println("判断键password是否存在:"+jedis.exists("password")); System.out.println("查看键username所存储的值的类型:"+jedis.type("username")); System.out.println("随机返回key空间的一个:"+jedis.randomKey()); System.out.println("重命名key:"+jedis.rename("username","name")); System.out.println("取出改后的name:"+jedis.get("name")); System.out.println("按索引查询:"+jedis.select(0)); System.out.println("删除当前选择数据库中的所有key:"+jedis.flushDB()); System.out.println("返回当前数据库中key的数目:"+jedis.dbSize()); System.out.println("删除所有数据库中的所有key:"+jedis.flushAll());
🌈 4.1 String
Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); System.out.println("===========增加数据==========="); System.out.println(jedis.set("key1","value1")); System.out.println(jedis.set("key2","value2")); System.out.println(jedis.set("key3", "value3")); System.out.println("删除键key2:"+jedis.del("key2")); System.out.println("获取键key2:"+jedis.get("key2")); System.out.println("修改key1:"+jedis.set("key1", "value1Changed")); System.out.println("获取key1的值:"+jedis.get("key1")); System.out.println("在key3后面加入值:"+jedis.append("key3", "End")); System.out.println("key3的值:"+jedis.get("key3")); System.out.println("增加多个键值 对:"+jedis.mset("key01","value01","key02","value02","key03","value03")); System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03")); System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03","key04")); System.out.println("删除多个键值对:"+jedis.del("key01","key02")); System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03")); jedis.flushDB(); System.out.println("===========新增键值对防止覆盖原先值=============="); System.out.println(jedis.setnx("key1", "value1")); System.out.println(jedis.setnx("key2", "value2")); System.out.println(jedis.setnx("key2", "value2-new")); System.out.println(jedis.get("key1")); System.out.println(jedis.get("key2")); System.out.println("===========新增键值对并设置有效时间============="); System.out.println(jedis.setex("key3", 2, "value3")); System.out.println(jedis.get("key3")); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(jedis.get("key3")); System.out.println("===========获取原值,更新为新值=========="); System.out.println(jedis.getSet("key2", "key2GetSet")); System.out.println(jedis.get("key2")); System.out.println("获得key2的值的字串:"+jedis.getrange("key2", 2, 4));
🌈 4.2 List
Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); System.out.println("===========添加一个list==========="); jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap"); jedis.lpush("collections", "HashSet"); jedis.lpush("collections", "TreeSet"); jedis.lpush("collections", "TreeMap"); //-1代表倒数第一个元素,-2代表倒数第二个元素,end为-1表示查询全部 System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); System.out.println("collections区间0-3的元素:"+jedis.lrange("collections",0,3)); System.out.println("==============================="); // 删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈 System.out.println("删除指定元素个数:"+jedis.lrem("collections", 2, "HashMap")); System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); System.out.println("删除下表0-3区间之外的元素:"+jedis.ltrim("collections", 0, 3)); System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); System.out.println("collections列表出栈(左端):"+jedis.lpop("collections")); System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); System.out.println("collections添加元素,从列表右端,与lpush相对 应:"+jedis.rpush("collections", "EnumMap")); System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); System.out.println("collections列表出栈(右端):"+jedis.rpop("collections")); System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); System.out.println("修改collections指定下标1的内容:"+jedis.lset("collections", 1, "LinkedArrayList")); System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); System.out.println("==============================="); System.out.println("collections的长度:"+jedis.llen("collections")); System.out.println("获取collections下标为2的元素:"+jedis.lindex("collections", 2)); System.out.println("==============================="); jedis.lpush("sortedList", "3","6","2","0","7","4"); System.out.println("sortedList排序前:"+jedis.lrange("sortedList", 0, -1)); System.out.println(jedis.sort("sortedList")); System.out.println("sortedList排序后:"+jedis.lrange("sortedList", 0, -1));
🌈 4.3 Set
Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); System.out.println("============向集合中添加元素(不重复)============"); System.out.println(jedis.sadd("eleSet", "e1","e2","e4","e3","e0","e8","e7","e5")); System.out.println(jedis.sadd("eleSet", "e6")); System.out.println(jedis.sadd("eleSet", "e6")); System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet")); System.out.println("删除一个元素e0:"+jedis.srem("eleSet", "e0")); System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet")); System.out.println("删除两个元素e7和e6:"+jedis.srem("eleSet", "e7","e6")); System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet")); System.out.println("随机的移除集合中的一个元素:"+jedis.spop("eleSet")); System.out.println("随机的移除集合中的一个元素:"+jedis.spop("eleSet")); System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet")); System.out.println("eleSet中包含元素的个数:"+jedis.scard("eleSet")); System.out.println("e3是否在eleSet中:"+jedis.sismember("eleSet", "e3")); System.out.println("e1是否在eleSet中:"+jedis.sismember("eleSet", "e1")); System.out.println("e1是否在eleSet中:"+jedis.sismember("eleSet", "e5")); System.out.println("================================="); System.out.println(jedis.sadd("eleSet1", "e1","e2","e4","e3","e0","e8","e7","e5")); System.out.println(jedis.sadd("eleSet2", "e1","e2","e4","e3","e0","e8")); //移到集合元素 System.out.println("将eleSet1中删除e1并存入eleSet3中:"+jedis.smove("eleSet1", "eleSet3", "e1")); System.out.println("将eleSet1中删除e2并存入eleSet3中:"+jedis.smove("eleSet1", "eleSet3", "e2")); System.out.println("eleSet1中的元素:"+jedis.smembers("eleSet1")); System.out.println("eleSet3中的元素:"+jedis.smembers("eleSet3")); System.out.println("============集合运算================="); System.out.println("eleSet1中的元素:"+jedis.smembers("eleSet1")); System.out.println("eleSet2中的元素:"+jedis.smembers("eleSet2")); System.out.println("eleSet1和eleSet2的交集:"+jedis.sinter("eleSet1","eleSet2")); System.out.println("eleSet1和eleSet2的并集:"+jedis.sunion("eleSet1","eleSet2")); //eleSet1中有,eleSet2中没有 System.out.println("eleSet1和eleSet2的差集:"+jedis.sdiff("eleSet1","eleSet2")); //求交集并将交集保存到dstkey的集合 jedis.sinterstore("eleSet4","eleSet1","eleSet2"); System.out.println("eleSet4中的元素:"+jedis.smembers("eleSet4"));
🌈 4.4 Hash
Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); Map<String,String> map = new HashMap<String,String>(); map.put("key1","value1"); map.put("key2","value2"); map.put("key3","value3"); map.put("key4","value4"); //添加名称为hash(key)的hash元素 jedis.hmset("hash",map); //向名称为hash的hash中添加key为key5,value为value5元素 jedis.hset("hash", "key5", "value5"); //return Map<String,String> System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash")); //return Set<String> System.out.println("散列hash的所有键为:"+jedis.hkeys("hash")); //return List<String> System.out.println("散列hash的所有值为:"+jedis.hvals("hash")); System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加 key6:"+jedis.hincrBy("hash", "key6", 6)); System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash")); System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加 key6:"+jedis.hincrBy("hash", "key6", 3)); System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash")); System.out.println("删除一个或者多个键值对:"+jedis.hdel("hash", "key2")); System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash")); System.out.println("散列hash中键值对的个数:"+jedis.hlen("hash")); System.out.println("判断hash中是否存在key2:"+jedis.hexists("hash","key2")); System.out.println("判断hash中是否存在key3:"+jedis.hexists("hash","key3")); System.out.println("获取hash中的值:"+jedis.hmget("hash","key3")); System.out.println("获取hash中的值:"+jedis.hmget("hash","key3","key4"));