3-4-1 NOSQL数据库-redis入门

Redis介绍

Redis是key-value型NoSQL数据库

Redis将数据存储在内存中,同事也能持久化到磁盘

Redis常用于缓存,利用内存的高效提高程序的处理速度

Redis特点

速度快

广泛的语言支持

持久化

多种数据结构

主从复制

分布式与高可用

Redis安装与启动

在linux系统中安装Redis

redis官网:https://redis.io/

安装:

$ wget http://download.redis.io/releases/redis-6.0.8.tar.gz
$ tar xzf redis-6.0.8.tar.gz
$ cd redis-6.0.8
$ make

启动:
$ src/redis-server redis.conf

在Windows系统中安装Redis

地址:https://github.com/MicrosoftArchive/redis

下载解压后进入目录

cmd启动命令:redis-server redis.windows.conf

Redis的常用基本配置

配置项 示例 说明
daemonize daemonize yes 是否启动后台运行,默认no
port port 6379 设置端口号,默认6379
logfile logfile 日志文件 设置日志文件
databases databases 255 设置redis数据库总量
dir dir 数据文件目录 设置数据文件存储目录
requirepass requirepass 123456 设置使用密码

linux在redis.conf中修改,windows在redis.windows.conf中修改

Redis通用命令

命令 示例 说明
select select 0 选择0号数据库
set set name lily 设置key=name,value=lily
get get hello 获得key=hello的结果
keys keys he* 根据正则表达式查询符合条件的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=a的剩余时间

Redis数据类型

String-字符串类型

命令 示例 说明
get get hello 获得key=hello的结果
set set name lily 设置key=name,value=lily

mset

mget

mset hello world java best

mget hello java

一次设置或者获取多个值
del del hello 删除key=hello
incr/decr

incr count

decr count

key值自增或自减1
incrby/decrby

incrby count 99

decrby count 99

自增自减指定步长

 

Hash-Hash类型

命令 示例 说明
hget hget emp:1 age 获取hash中key=age的值
hset hset emp:1 age 23 设置hash中age=23

hmset

hmget

hgetall

hmset emp:1 age 30 name kaka

hmget emp:1 age name

hgetall emp:1

设置hash多个值

获取hash多个值

获取hash所有值

hdel hdel emp:1 age 删除emp:1的age
hexists hexists emp:1 name 检查是否存在
hlen hlen emp:1 获取指定长度

 

List-列表类型

rpush listkey c b a -右侧插入  示例:c-b-a

lpush listkey f e d -左侧插入   示例:d-e-f-c-b-a

rpop listkey -右侧弹出 示例:d-e-f-c-b

lpop listkey -左侧弹出 示例:e-f-c-b

len listkey -获取长度

lrange listkey 0 -1 -查看全部listkey

Set-集合类型

无序集合,集合成员唯一

sadd set1 a b c - 创建集合

sadd set2 c b e 

smembers set1 -查看集合

sinter set1 set2 -交集

sunion set1 set2 -并集

sdiff set1 set2 -差集,指在set1中有,但是set2没有的元素

Zset-有序集合类型

有序集合,集合成员唯一

zadd zset1 100 a -创建集合,其中100表示排序的权重,默认升序排列

zadd zset 101 b

zrange zset1 0 -1 -查看集合

zrange zset 0 -1 withscores -查看集合和分数

Java客户端-Jedis

引入依赖,2.9.0是常用版本

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
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("127.0.0.1" , 6379);
        try {
//            jedis.auth("12345");
            jedis.select(2);
            System.out.println("Redis连接成功");
            //字符串
            jedis.set("sn" , "7781-9938");
            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", "李兰");
            studentMap.put("age", "18");
            studentMap.put("id", "3313");
            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();
        }

    }
}

缓存数据

 1 public class Goods {
 2     private Integer goodsId;
 3     private String goodsName;
 4     private String description;
 5     private Float price;
 6 
 7     public Goods(){
 8 
 9     }
10 
11     public Goods(Integer goodsId, String goodsName, String description, Float price) {
12         this.goodsId = goodsId;
13         this.goodsName = goodsName;
14         this.description = description;
15         this.price = price;
16     }
17 
18     public Integer getGoodsId() {
19         return goodsId;
20     }
21 
22     public void setGoodsId(Integer goodsId) {
23         this.goodsId = goodsId;
24     }
25 
26     public String getGoodsName() {
27         return goodsName;
28     }
29 
30     public void setGoodsName(String goodsName) {
31         this.goodsName = goodsName;
32     }
33 
34     public String getDescription() {
35         return description;
36     }
37 
38     public void setDescription(String description) {
39         this.description = description;
40     }
41 
42     public Float getPrice() {
43         return price;
44     }
45 
46     public void setPrice(Float price) {
47         this.price = price;
48     }
49 }
 1 import com.alibaba.fastjson.JSON;
 2 import redis.clients.jedis.Jedis;
 3 
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 import java.util.Scanner;
 7 
 8 public class CacheSample {
 9     public CacheSample(){
10         Jedis jedis = new Jedis("127.0.0.1");
11         try {
12             List<Goods> goodsList = new ArrayList<Goods>();
13             goodsList.add(new Goods(8818, "红富士苹果", "", 3.5f));
14             goodsList.add(new Goods(8819, "进口脐橙", "", 5f));
15             goodsList.add(new Goods(8820, "进口香蕉", "", 25f));
16 //            jedis.auth("12345");
17             jedis.select(3);
18             for (Goods goods : goodsList) {
19                 String json = JSON.toJSONString(goods);
20                 System.out.println(json);
21                 String key = "goods:" + goods.getGoodsId();
22                 jedis.set(key , json);
23             }
24         } catch (Exception e) {
25             e.printStackTrace();
26         }finally {
27             jedis.close();
28         }
29     }
30 
31     public static void main(String[] args) {
32         new CacheSample();
33         System.out.printf("请输入要查询的商品编号:");
34         String goodsId = new Scanner(System.in).next();
35         Jedis jedis = new Jedis("127.0.0.1");
36         try{
37 //            jedis.auth("12345");
38             jedis.select(3);
39             String key = "goods:" + goodsId;
40             if(jedis.exists(key)){
41                 String json = jedis.get(key);
42                 System.out.println(json);
43                 Goods g = JSON.parseObject(json, Goods.class);
44                 System.out.println(g.getGoodsName());
45                 System.out.println(g.getPrice());
46             }else{
47                 System.out.println("您输入的商品编号不存在,请重新输入!");
48             }
49         }catch(Exception e){
50             e.printStackTrace();
51         }finally {
52             jedis.close();
53         }
54     }
55 }

 

posted @ 2020-10-08 16:27  mingmingn  阅读(139)  评论(0)    收藏  举报