Redis 是速度非常快的非关系型(NoSQL)内存键值数据库,可以存储键和五种不同类型的值之间的映射。
键的类型只能为字符串,值支持的五种类型数据类型为:字符串、列表、集合、有序集合、散列表。
Redis 支持很多特性,例如将内存中的数据持久化到硬盘中,使用复制来扩展读性能,使用分片来扩展写性能。
数据类型 | 可以存储的值 | 操作 |
---|---|---|
STRING | 字符串、整数或者浮点数 | 对整个字符串或者字符串的其中一部分执行操作 对整数和浮点数执行自增或者自减操作 |
LIST | 列表 | 从两端压入或者弹出元素 读取单个或者多个元素 进行修剪,只保留一个范围内的元素 |
SET | 无序集合 | 添加、获取、移除单个元素 检查一个元素是否存在于集合中 计算交集、并集、差集 从集合里面随机获取元素 |
HASH | 包含键值对的无序散列表 | 添加、获取、移除单个键值对 获取所有键值对 检查某个键是否存在 |
ZSET | 有序集合 | 添加、获取、删除元素 根据分值范围或者成员来获取元素 计算一个键的排名 |
首先,我们是在java使用Jedis对redis数据库进行操作的,Maven下可以直接添加如下依赖就可以使用了。
redis.clients
jedis
2.9.0
jar
compile
接下来是连接redis数据库,如下:
Jedis jedis = new Jedis("redis://localhost:6379/9"); //默认地连接我的6379端口,如果什么都不填的话
jedis.flushDB(); //删掉之前数据
或者
pool = new JedisPool("redis://localhost:6379/10");
Jedis jedis = pool.getResource(); // 需在try-catch中使用
此外,添加一个print函数打印结果:
public static void print(int index, Object obj){
System.out.println(String.format("%d, %s",index,obj.toString()));
}
jedis.set("username","wade");
print(1,jedis.get("username"));
jedis.setex("hello",15,"world"); //只存在15s,不需要自己删除,用在验证码上(带有过期时间)
// 短信的验证时间
// 数字的自增,自减
jedis.set("pv","100");
jedis.incr("pv");
jedis.incr("pv");
print(2,jedis.get("pv"));
jedis.incrBy("pv",5);
print(3,jedis.get("pv"));
jedis.decr("pv");
jedis.decrBy("pv",2);
print(4,jedis.get("pv"));
print(5,jedis.keys("*"));
lpush : 从队列左边入队一个或多个元素。
lrange:从列表中获取指定的元素。 llen:获取列表长度
lpop:从左边出队一个元素 lindex:通过索引获取元素
linsert:在列表中一个元素的前后插入元素。
String listName = "list";
jedis.del(listName);
for (int i = 0; i < 10; i++) {
jedis.lpush(listName, "a"+String.valueOf(i));
}
print(5,jedis.lrange(listName,0,12));
print(5,jedis.lrange(listName,0,3));
print(6,jedis.llen(listName));
print(7,jedis.lpop(listName));
print(8,jedis.llen(listName));
print(9,jedis.lindex(listName,3));
print(10,jedis.linsert(listName, BinaryClient.LIST_POSITION.AFTER,"a4","xx"));
print(10,jedis.linsert(listName, BinaryClient.LIST_POSITION.BEFORE,"a4","yy"));
print(11,jedis.lrange(listName,0,12));
//hash 开发的过程中,不知道对象有什么属性,就用这种
String userKey = "userxxx";
jedis.hset(userKey,"name","jim"); // hset
jedis.hset(userKey,"age","12");
jedis.hset(userKey,"phone","15623165465");
print(12,jedis.hget(userKey,"name")); //hget
print(13,jedis.hgetAll(userKey)); //hgetAll
//print(14,jedis.hdel(userKey,"phone"));
print(15,jedis.hexists(userKey,"email"));
print(16,jedis.hexists(userKey,"age"));
print(17,jedis.hkeys(userKey)); // hkeys
print(18,jedis.hvals(userKey));
jedis.hsetnx(userKey,"school","nuaa"); // 不存在的话才更新
jedis.hsetnx(userKey,"name","lzy");
print(19,jedis.hgetAll(userKey));
结果:
12, jim
13, {name=jim, phone=15623165465, age=12}
15, false
16, true
17, [name, phone, age]
18, [jim, 12, 15623165465]
19, {name=jim, phone=15623165465, school=nuaa, age=12}
添加:sadd:
遍历:smembers:
集合操作: sunion、sdiff、sinter
删除:srem
计数:scard
//set
String likekey1 = "commentLike1";
String likekey2 = "commentLike2";
for (int i = 0; i < 10; i++) {
jedis.sadd(likekey1,String.valueOf(i));
jedis.sadd(likekey2,String.valueOf(i*i));
}
print(20,jedis.smembers(likekey1));
print(21,jedis.smembers(likekey2));
// 集合求交
print(22,jedis.sunion(likekey1,likekey2));
print(23,jedis.sdiff(likekey1,likekey2));
print(24,jedis.sinter(likekey1,likekey2));
print(25,jedis.sismember(likekey1,"4"));
print(26,jedis.sismember(likekey1,"16"));
jedis.srem(likekey1,"5"); //删除
print(27,jedis.smembers(likekey1));
jedis.smove(likekey2,likekey1,"25"); //从前面向后面移个25
print(28,jedis.smembers(likekey1));
print(29,jedis.scard(likekey1)); // 计数,里面有几个值
结果:
20, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
21, [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
22, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 25, 36, 49, 64, 81]
23, [8, 2, 3, 5, 6, 7]
24, [0, 1, 4, 9]
25, true
26, false
27, [0, 1, 2, 3, 4, 6, 7, 8, 9]
28, [0, 1, 2, 3, 4, 6, 7, 8, 9, 25]
29, 10
添加,zadd 计数,zcard 增加score值,zincrby(rankKey,2,"lucy")
取得score值: zscore
// 优先队列 可以用在排行榜
String rankKey = "rankKey";
jedis.zadd(rankKey,15,"jim");
jedis.zadd(rankKey,60,"ben");
jedis.zadd(rankKey,90,"lee");
jedis.zadd(rankKey,75,"lucy");
jedis.zadd(rankKey,80,"mei");
print(30,jedis.zcard(rankKey)); //计数
print(31,jedis.zcount(rankKey,61,100));
print(32,jedis.zscore(rankKey,"lucy")); // 得到score值
jedis.zincrby(rankKey,2,"lucy"); // add score值
print(33,jedis.zscore(rankKey,"lucy"));
print(34,jedis.zrange(rankKey, 0,10));
print(35,jedis.zrange(rankKey, 0,2)); //默认从小到大
print(36,jedis.zrevrange(rankKey, 0,2));
for (Tuple tuple:jedis.zrangeByScoreWithScores(rankKey,"60","100")) { // 把分值范围内的值取出来
print(37,tuple.getElement()+":"+String.valueOf(tuple.getScore()));
}
print(37,jedis.zrank(rankKey,"lee")); // 得出具体排名
print(38,jedis.zrevrank(rankKey,"lee"));
String setKey = "zset";
jedis.zadd(setKey,1,"a");
jedis.zadd(setKey,1,"b");
jedis.zadd(setKey,1,"c");
jedis.zadd(setKey,1,"d");
jedis.zadd(setKey,1,"e");
print(39,jedis.zlexcount(setKey,"-","+"));
print(40,jedis.zlexcount(setKey,"(b","(d"));
print(41,jedis.zlexcount(setKey,"[b","[d"));
jedis.zrem(setKey,"b");
print(42,jedis.zrange(setKey,0,10));
jedis.zremrangeByLex(setKey,"(c","+"); //根据字典序删除
print(43,jedis.zrange(setKey,0,10));
结果:
30, 5
31, 3
32, 75.0
33, 77.0
34, [jim, ben, lucy, mei, lee]
35, [jim, ben, lucy]
36, [lee, mei, lucy]
37, ben:60.0
37, lucy:77.0
37, mei:80.0
37, lee:90.0
37, 4
38, 0
39, 5
40, 1
41, 3
42, [a, c, d, e]
43, [a, c]