redis basic and advanced 01

Redis use

1. What is the Redis

	Redis是一个no sql的数据库
	1.1.什么是NOSQL
		NoSQL(NoSQL = Not Only SQL),意即“不仅仅是SQL”,是一项全新的数据库理念,泛指非关系型的数据库。
		随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的web2.0纯动态网站已经显得力不从心,暴露了很多难以克服的问题,而非关系型的数据库则由于其本身的特点得到了非常迅速的发展。NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,尤其是大数据应用难题。
	1.1.1.	NOSQL和关系型数据库比较
		优点:
			1)成本:nosql数据库简单易部署,基本都是开源软件,不需要像使用oracle那样花费大量成本购买使用,相比关系型数据库价格便宜。
			2)查询速度:nosql数据库将数据存储于缓存之中,关系型数据库将数据存储在硬盘中,自然查询速度远不及nosql数据库。
			3)存储数据的格式:nosql的存储格式是key,value形式、文档形式、图片形式等等,所以可以存储基础类型以及对象或者是集合等各种格式,而数据库则只支持基础类型。
			4)扩展性:关系型数据库有类似join这样的多表查询机制的限制导致扩展很艰难。

		缺点:
			1)维护的工具和资料有限,因为nosql是属于新的技术,不能和关系型数据库10几年的技术同日而语。
			2)不提供对sql的支持,如果不支持sql这样的工业标准,将产生一定用户的学习和使用成本。
			3)不提供关系型数据库对事务的处理。

	1.1.2.	非关系型数据库的优势:
		1)性能NOSQL是基于键值对的,可以想象成表中的主键和值的对应关系,而且不需要经过SQL层的解析,所以性能非常高。
		2)可扩展性同样也是因为基于键值对,数据之间没有耦合性,所以非常容易水平扩展。

	1.1.3.	关系型数据库的优势:
		1)复杂查询可以用SQL语句方便的在一个表以及多个表之间做非常复杂的数据查询。
		2)事务支持使得对于安全性能很高的数据访问要求得以实现。对于这两类数据库,对方的优势就是自己的弱势,反之亦然。

	1.1.4.	总结
		关系型数据库与NoSQL数据库并非对立而是互补的关系,即通常情况下使用关系型数据库,在适合使用NoSQL的时候使用NoSQL数据库,
		让NoSQL数据库对关系型数据库的不足进行弥补。
		一般会将数据存储在关系型数据库中,在nosql数据库中备份存储关系型数据库的数据

1.2.主流的NOSQL产品
	•	键值(Key-Value)存储数据库
			相关产品: Tokyo Cabinet/Tyrant、Redis、Voldemort、Berkeley DB
			典型应用: 内容缓存,主要用于处理大量数据的高访问负载。 
			数据模型: 一系列键值对
			优势: 快速查询
			劣势: 存储的数据缺少结构化
	•	列存储数据库
			相关产品:Cassandra, HBase, Riak
			典型应用:分布式的文件系统
			数据模型:以列簇式存储,将同一列数据存在一起
			优势:查找速度快,可扩展性强,更容易进行分布式扩展
			劣势:功能相对局限
	•	文档型数据库
			相关产品:CouchDB、MongoDB
			典型应用:Web应用(与Key-Value类似,Value是结构化的)
			数据模型: 一系列键值对
			优势:数据结构要求不严格
			劣势: 查询性能不高,而且缺乏统一的查询语法
	•	图形(Graph)数据库
			相关数据库:Neo4J、InfoGrid、Infinite Graph
			典型应用:社交网络
			数据模型:图结构
			优势:利用图结构相关算法。
			劣势:需要对整个图做计算才能得出结果,不容易做分布式的集群方案。
1.3 什么是Redis
	Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库,官方提供测试数据,50个并发执行100000个请求,读的速度是110000次/s,写的速度是81000次/s ,且Redis通过提供多种键值数据类型来适应不同场景下的存储需求,目前为止Redis支持的键值数据类型如下:
		1) 字符串类型 string
		2) 哈希类型 hash
		3) 列表类型 list
		4) 集合类型 set
		5) 有序集合类型 sortedset
	1.3.1 redis的应用场景
		•	缓存(数据查询、短连接、新闻内容、商品内容等等)
		•	聊天室的在线好友列表
		•	任务队列。(秒杀、抢购、12306等等)
		•	应用排行榜
		•	网站访问统计
		•	数据过期处理(可以精确到毫秒
		•	分布式集群架构中的session分离

2, redis can do

1、做数据缓存(缓存)

2、购物车数据

3、评论的存储

4、抽奖

5、商城中评分

6、最热的商品

7、秒杀

8、好友关注

9、实现session共享

10、分布式锁

3, Redis features

1、Redis是基于内存的

2、速度快

3、如果出现了断电 那么内存数据会发生丢失

4、Redis的使用场景 一定是对数据的要求不是严格的

5、Redis的所有存储结构中都是以Key---Value的形式进行存储的

6、提供了五种不同的数据类型来满足不同的开发场景

​      String   Hash    List    Set     Sorted Set

7、Redis中还提供了3中持久化模式来保证数据的持久化(将内存的数据放到硬盘)

8、Redis还提供多种淘汰策略来满足缓存的数据一定是最新的

9、Redis中还提供了大量的命令集、来完成我们开发中的操作

10、Redis从提供了三种架构(主从、哨兵、集群)

4, Redis five kinds of data structures are long-sawed

Here Insert Picture Description

6, Redis installation

1、将redis的安装文件复制到 /usr/local目录下去
cp redis-5.0.7.tar.gz /usr/local/
2、下载Redis运行的时候所需要的运行环境
yum install gcc
3、解压上面的Redis文件
tar -zxvf redis-5.0.7.tar.gz
4、进入Redis的根目录进行编译
cd redis-5.0.7
make
5、进入到src目录下进行安装
cd /usr/local/redis-5.0.7/src
make install
6、创建运行的命令的目录和配置文件的目录
mkdir -p /usr/local/redis/bin
mkdir -p /usr/local/redis/etc
7、移动配置文件到 /usr/local/redis/etc目录下
cp redis.conf /usr/local/redis/etc/
8、将运行命令放到 /usr/local/redis/bin目录下去
cp redis-server /usr/local/redis/bin/
cp redis-cli /usr/local/redis/bin/
    
redis-server :Redis的服务端的启动程序
redis-cli:Redis的客户端的启动程序

客户端启动之后 就可以对数据库的数据进行操作
cd /usr/local/redis/bin
./redis-server /usr/local/redis/etc/redis.conf

netstat -apn | grep 6379   :查看6379的端口使用情况

7, to learn the basic commands of Redis

可以使用 RedisDesktopManager进行redis  连接操作

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ocMBgZJv-1583508024913)(pic/TIM截图20200304174129.png)]

在Redis难道有多个数据库?

在redis中默认是16个数据库、怎么在这些数据库之间 进行切换呢 ?

这个数据库的下标  0----15

默认选中的是 第0号数据库  默认数据存储也是存储到第0号数据库的

1, and key related commands

   keys * :查看当前数据库中存在的键
select 数据库的下标   选中某一个数据库
del key :删除某一条数据
exists key :判定某一个key是否存在
    token需要使用
    所有缓存的地方都需要使用这个命令
expire key  过期时间 :这个命令可以用在前后分离项目中token设置的过期时间   过期之后 key会被删除
ttl key :查看这个key剩余的时间
          场景:这个主要就是key如果是需要续期的话 就可以使用这个命令
          返回-2 这个key不存在
          返回-1 表示没有过期时间永远都有效
move key 数据库索引  :将一个key--value移动到另外一个数据库中去 
          场景:一般情况下做数据库的迁移(基本用的很少)
randomkey 随机返回一个key 

2, and the associated String

set key  value   :在数据库中设置一个键值对的数据
get key          :获取某一个值

Here Insert Picture Description

Scene: data storage objects

set user:1:userName xiaobobo

set user:1:password 123

set user:1:count 300

get user: 1: userName only access to username

mset k v k1 v1  :同时设置多个键值对
mget k v k1 v1  :通古斯获取多个键值对的数据

Scene: an object data storage

mset user:1:userName xiaobobo user:1:password 123 user:1:count 100

mget user:1:userName user:1:password user:1:count

incr key  ;自增
decr key  :自减
incrby key 步长  :一次性增加多少
decrby key 步长  :一次性减去多少

Scene: very much

Here Insert Picture Description

Here Insert Picture Description

how to use?

set weibo-readcount-{文章id} 0

incr weibo-readcount-1101

setnx key value:表示的是如果这个key不存在的话  那么就设置这个键值对

** Scene: ** mainly used in the distributed lock above

Here Insert Picture Description

3, and related Hash

hset 集合的名字  键的名字  值
hget 集合的名字  键的名字  值
hlen 集合的名字  键的名字  获取当前集合中值的数量
hdel 集合的名字  键的名字
hincrby 集合的名字 键的名字 增加的数量
hgetall 集合的名字  直接实现全选的功能
hexists 集合的名字   键的名字  (判断某个人是否有购物车)
hkeys 集合的名字  获取当前这个人的所有的购物车中的商品
hvals 集合的名字   取出当前集合中所有的值

Scene : Shopping Cart

Here Insert Picture Description

Store shopping cart data

hset cart:100 100001 1
hset cart:100 10998 2

command

hmset 集合的名字   键的名字  值的名字   键的名字  值的名字
hmget 集合的名字  键的名字 键的名字

Scene : object data storage

Here Insert Picture Description

hmset user 1:userName xiaobobo 1:password 123

hmset user 2:userName xiaowangzi 2:password 123

hmget user 2:userName 2:password

id hmset dept departments: Department of field values

4, and the associated List

lpush  键  值  :表示的是将一个或者多个值 插入到列表的表头
lpop  键      :移除并返回这个列表的头元素   
rpush 键  值  :在列表的表位添加元素
rpop 键   从列表的右侧弹出元素
lrange 键 start  stop
BLPOP 键:从列表的表头弹出一个元素  如果列表的表头没有元素  那么就阻塞等待
BRPOP 键:从列表的右侧弹出一个元素 如果没有元素 那么就阻塞等待

Illustrating

Here Insert Picture Description

Scene: Stack of play

LPUSH + LPOP

Scene: construction queue

LPUSH +RPOP

Scene: blocking queue

LPUSH+BRPOP

The actual application scenarios

Here Insert Picture Description

Bobo attention Modtech also concerned about the microblogging open platform

Suppose now Mobtech message message id: 10086

List structure to design how to set up

Requirements: Bobo now focus on the two platforms both platforms when sending a message just sent to the attention of the users of this platform now mobtech to send a message to Bobo teacher so that messages are now sent mobtech how to use the results list Bobo is designed to allow the teacher when he landed the system to automatically take this news?

lpush mobtech:msg:{波波老师的id}  发送这条数据的id

lpush mobtech:msg:{刘磊老师的id}  发送这条数据的id   

lpush weibo:msg:{波波老师id} 发送这条的id

lrange  mobtech:msg:{波波老师id} 0 50

5, and the associated Set

command

sadd  键  值  :就是向set集合中添加一个值  (可以用在点赞上)
srem  键  值  :删除这个键中的某一个值  (可以用在取消点赞上)
sismember 键  值  检查某一个值是否在这个集合中存在
smembers key :表示的是获取这个集合中的所有数据
srandmemebr key count :从集合中选出count个元素 ,元素不从key中删除(值是随机的)
spop key count :从集合中选中count个元素 元素从集合中删除
sinter key :做做交集运算
sinterstore desternation key:将交集的结果存入新的集合
sunion key :并集运算
sunionstore desternation key :将并集的结果存入这个集合
sdiff key :差集运算
adiffstore desternation key :将差集的结果存入这个集合
scard key  :计算当前set集合中的元素的个数

Applicable scene

Here Insert Picture Description

I want to use this brother sadd to store the current mood of thumbs up

How to design the thumbs up is the most reasonable?

like

sadd weixin:like:{消息的id}  用户的id
sadd  集合的名字   集合的值

Cancel thumbs up

srem weixin:like:10010 10   取消某一个人的点赞

Check whether an individual is past Chan

sismember weixin:10010 120  :检查id是120的用户是否点赞过这条消息

Users get a list of thumbs up

smembers weixin:like:10010 

Get all of the user's current points over praise

scard weixin:like:10010

Operation between the plurality of sets

Here Insert Picture Description

Scenes:

Here Insert Picture Description

Scenes

Bobo: Follow the boboSet = {liulei dashixing}

Liu Lei: Follow the liuleiSet = {bobo dashixing yangjianhui hehanyu}

Yang Jianhui: Follow the yangjianhuiSet = {dashixing bobo liulei chengjunbo}

Xin attention of the world: {liulei yangjianhui bobo}

The question is:

Bobo people of common concern and Yang Jianhui

Intersection

​              sinter boboSet yangjianhuiSet

Bobo people are concerned also concerned about him (Liu Lei)

1, found Bobo who paid attention (to remove Liu Lei)

dashixing liulei

2: find a collection of the world Xin

dashixinSet liuleiSet

3, need only determine whether a set of Shi Xin Liu Lei inside

​              sismember  dashixinSet liulei

​              sismember  liuleiSet    liulei  

Bobo May Know

And other collections differencing set on it (who?)

1, to obtain a set of wave of concern

​                 smembers boboSet

2, concerned about the wave of wave of people's collections and collection sets differencing

​                 sdiff dashixingSet boboSet

​                sdiff liuleiSet boboSet

6, and the Sorted Set associated

Note: Sorted Set is automatically sorted based on scoring

Look command

zadd 集合的名字  打分  键的名字   :向Sorted Set中添加一个数据
zrange 集合的名字 开始的位置  结束的位置  获取集合中某一个区间的值
zincrby 集合的名字 加的分值  key的名字   //给某一个值添加分值
zrevrank 集合的名字 键的值   :查看当前数据的排名
zrem 集合的名字 键的名字     :删除某一条数据
zscore 集合的名字  键的名字 
zcount 集合的名字   开始  结束   :获取得分在某一个区间类的数据的个数

Here Insert Picture Description

Scenes:

It is to give time to do the hottest commodities or goods by popularity

What is the hottest commodity and popular item?

The hottest commodity: the largest number of popular item purchased (the most hits, the most collection of)

// assume the hottest commodity

1: As long as the purchase of goods produced

​         zadd good:hot  10   商品id

It has generated purchase

加分:zincrby 集合的名字 加的分值 key的名字

Joe Smith to see their gift in the rankings live platform?

​        zrevrank 集合的名字 键的值   :查看当前数据的排名

Product off the shelf does not sell?

​        zrem  good:hot   goodId1     :删除某一条数据

Popularity score (heat)

Heat: Number (algorithm) + barrage of different different gifts to score points + time + number of fans to play

​        zscore good:hot goodId3

to be continued. . . .

Published 50 original articles · won praise 21 · views 3702

Guess you like

Origin blog.csdn.net/wmlwml0000/article/details/104706978