抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

Redis特点

1.速度快
2.广泛语言的支持
3.持久化
4.支持多种数据类型
5.主从复制
6.分布式与高可用

Redis通用命令

select命令:
示例:select 0 选择0号数据库
set命令:
示例:set name lily 设置key=name,value=lily
get命令:
示例:get hello 获取key=hello结果
keys命令:
示例: keys he* 根据Pattern表达式查询符合条件的key
dbsize命令:
示例:dbsize 返回key的总数
exists命令:
示例:exists a 检查key=a是否存在
del命令:
示例:del a 删除key=a的数据
expire命令:
示例:expire hello 20 设置key=hello 20秒后过期
ttl命令:
示例:ttl hello 查看key=hello的过期剩余时间

Redis数据类型

String -字符串类型

字符串命令

get 示例:get hello 说明:获取key=hello结果
set 示例:set hello world 说明:获取key=hello,value=world
mset 示例:mset hello world java best 说明:一次性设置或者获取多个值
mget 示例:mget hello world 说明:一次性设置或者获取多个值
del 示例:del hello 说明:删除key=hello
incr/decr 示例:incr count decr count 说明:key值自增/自减1
incrby/decrby 示例:incrby/decrby count 99 说明:自增自减指定的步长

Hash -Hash类型

Hash类型用于保存结构化数据

Hash 命令

hget
hset
hmset
hmget
hgetall
hdel
hexists
hlen

List -列表类型

List列表就是一系列字符串数组,按插入顺序排序
List列表的最大长度2的32次方-1

List命令

rpush listkey c b a - 右侧插入
lpush listkey f e d - 左侧插入
rpop listkey - 右侧弹出
lpop listkey - 左侧弹出
lrange listkey 0 -1 查看

Set -集合类型

Set集合是字符串的无序集合,集合成员是唯一的

Zset -有序集合类型

Zset集合是字符串的有序集合,集合成员是唯一的

Jedis操作数据

1.引入Jedis的依赖包

1
2
3
4
5
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>

2.操作数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.imooc.jedis;

import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JedisTestor {
public static void main(String[] args) {
Jedis jedis = new Jedis("101.42.107.155",6379);
try {
jedis.auth("123456");
jedis.select(2);
System.out.println("Redis连接成功");
//字符串
jedis.set("sn","7781-9928");
String sn = jedis.get("sn");
System.out.println(sn);
jedis.mset(new String[]{"title","婴幼儿奶粉","num","20"});
List<String> goods = jedis.mget(new String[]{"sn","title","num"});
System.out.println(goods);
Long num = jedis.incr("num");
System.out.println(num);
//Hash
jedis.hset("student:3312","name","张希");
String name = jedis.hget("student:3312","name");
System.out.println(name);
Map<String,String> studentMap = new HashMap();
studentMap.put("name","tom");
studentMap.put("age","18");
jedis.hmset("student:3313",studentMap);
Map<String,String> smap = jedis.hgetAll("student:3313");
System.out.println(smap);
//List
jedis.del("letter");
jedis.rpush("letter",new String[]{"d","e","f"});
jedis.lpush("letter",new String[]{"c","b","a"});
List<String> letter = jedis.lrange("letter",0,-1);
jedis.lpop("letter");
jedis.rpop("letter");
letter = jedis.lrange("letter",0,-1);
System.out.println(letter);
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis.close();
}
}
}

利用Jedis缓存数据

使用JSON序列化把javaBean转化为json字符串,把json的字符串保存在redis中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.imooc.jedis;

public class Goods {
private Integer goodsId;
private String goodsName;
private String description;
private Float price;

public Goods() {
}

public Goods(Integer goodsId, String goodsName, String description, Float price) {
this.goodsId = goodsId;
this.goodsName = goodsName;
this.description = description;
this.price = price;
}

public Integer getGoodsId() {
return goodsId;
}

public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}

public String getGoodsName() {
return goodsName;
}

public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Float getPrice() {
return price;
}

public void setPrice(Float price) {
this.price = price;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.imooc.jedis;

import com.alibaba.fastjson.JSON;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CacheSample {
public CacheSample(){
Jedis jedis = new Jedis("101.42.107.155",6379);
try{
List<Goods> goodsList=new ArrayList<Goods>();
goodsList.add(new Goods(8818,"红富士苹果","",12.5f));
goodsList.add(new Goods(8818,"进口香橙","",18.0f));
goodsList.add(new Goods(8818,"日本香蕉","",4.5f));
jedis.auth("123546");
jedis.select(3);
for (Goods goods:goodsList){
String json = JSON.toJSONString(goods);
System.out.println(json);
String key = "goods:"+goods.getGoodsId();
jedis.set(key,json);
}
}catch (Exception e){
e.printStackTrace();
}finally {
jedis.close();
}
}

public static void main(String[] args) {
new CacheSample();
System.out.println("请输入要查询的商品编号:");
String goodsId = new Scanner(System.in).next();
Jedis jedis = new Jedis("101.42.107.155",6379);
try{
jedis.auth("123546");
jedis.select(3);
String key = "goods:"+goodsId;
if (jedis.exists(key)){
String json = jedis.get(key);
System.out.println(json);
//把JSON转回成JAVA对象
Goods goods = JSON.parseObject(json,Goods.class);
System.out.println(goods.getGoodsName());
}else {
System.out.println("您输入的商品编号不存在,请重新输入!");
}
}catch (Exception e){
e.printStackTrace();
}finally {
jedis.close();
}
}
}