Redis Getting Started Notes 1

Redis

Knowledge points in this chapter

Three Redis Getting Started

3.1 Database theory


按照早期的数据库理论,比较流行的数据库模型有三种,分别为层次式数据库、网络式数据库和关系型数据库(前两者已经基本消失)。
 而当今的互联网中,最常用的数据库模型主要是两种即 关系型数据库 和 非关系型数据库。

关系型数据库:关系型数据库以行和列的形式存储数据,以便于用户理解。这一系列的行和列被称为表,一组表组成了数据库。(列是固定 行动态添加)。常见的关系型数据库MySQLOracleSQLserver

非关系型数据库:非关系型数据库也被称为NoSQL数据库,NoSQL的本意是“Not Only SQL”,指的是非关系型数据库,而不是“No SQL”的意思(没有SQL语句?),因此,NoSQL的产生并不是要彻底否定关系型数据库,而是作为传统关系型数据库的一个有效补充。NoSQL数据库在特定的场景下可以发挥出难以想象的高效率和高性能。

insert image description here

3.2 Redis database service

Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理。它支持[字符串][哈希表][列表][集合][有序集合][位图][hyperloglogs]等数据类型。内置复制、[Lua脚本]、LRU收回、[事务]以及不同级别磁盘持久化功能,同时通过Redis Sentinel提供高可用,通过Redis Cluster提供自动[分区]。

① redis是开源的 底层是c语言。    
② redis的存储是基于内存的。
      电脑:  内存 16G   Redis数据是存储到内存中  可以进行持久化 
             硬盘 500G  MySQL数据存储是存储到硬盘
    Redis为什么比MySQLRedis直接从内存中查数据  
    MySQL需要先从硬盘中将数据加载到内存再查询数据
 
③ 作为数据库。  
     我们使用Redis 用来存储的是 临时数据 缓存数据
    
④ 作为消息队列
      Redis可以做消息队列 但是不擅长
    
⑤ 支持5种数据类型
  关系型数据库:行和列的形式进行存储  
  非关系型数据库中的Redis是以key-value的形式进行存储 类似于我们JavaHashMap
     
  strings, hashes, lists, sets, sorted sets
    
  StringMap<String,String>  
  List:    Map<String,List>  
  Hash:    Map<String,Map>   
  Set:     Map<String,Set>

⑥ 内置 复制、[Lua脚本]、LRU(最少做小算法 FIFO)收回、[事务])以及不同级别磁盘持久化功能
 (redis不仅可以存储数据到内存 也可以持久化到硬盘rdb和aof)

⑦Redis Sentinel提供高可用---哨兵机制  

⑧ Redis Cluster 集群   redis集群版

⑨ redis以前是单线程的  6.0以后变成了多线程      Redis最新版本7.0

3.3 Advantages of Redis

Redis 与其他 key - value 缓存产品有以下三个特点:

①Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。

②Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。

 Redis存储形式时key-value的形式 但是value可以是 基本类型 也可以是list set  hash  zset

③Redis支持数据的备份,即master-slave模式的数据备份。

Redis 优势

①性能极高 – Redis能读的速度是110000/s,写的速度是81000/s 。

②丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, SetsOrdered Sets 数据类型操作。

③原子 – Redis的所有操作都是原子性的,意思就是要么成功执行要么失败完全不执行。单个操作是原子性的。多个操作也支持事务,即原子性,通过MULTI和EXEC指令包起来。

④丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性。 

Redis与其他key-value存储有什么不同?

① Redis有着更为复杂的数据结构并且提供对他们的原子性操作,这是一个不同于其他数据库的进化路径。Redis的数据类型都是基于基本数据结构的同时对程序员透明,无需进行额外的抽象。      

② Redis运行在内存中但是可以持久化到磁盘,所以在对不同数据集进行高速读写时需要权衡内存,因为数据量不能大于硬件内存。在内存数据库方面的另一个优点是,相比在磁盘上相同的复杂的数据结构,在内存中操作起来非常简单,这样Redis可以做很多内部复杂性很强的事情。同时,在磁盘格式方面他们是紧凑的以追加的方式产生的,因为他们并不需要进行随机访问。

3.4 Redis installation

克隆一台linux主机  修改ip地址 hostname  重启
安装gcc-c++ : yum  -y  install gcc-c++
在home下创建一个redis的文件夹: mkdir  /home/redis
移动到这个文件夹下:   cd  /home/redis
下载redis : wget  https://download.redis.io/releases/redis-5.0.9.tar.gz
解压redis: tar -zxvf  redis-5.0.9.tar.gz
进入到redis解压目录: cd  redis-5.0.9
编译并运行:make  && make install PREFIX=/usr/local/redis
    
    
移动到redis的安装目录:  cd  /usr/local/redis
启动redis :  ./bin/redis-server
当我们直接这样启动的时候 此时我们当前xshell窗口已经被redis占用,我们使用ctrl+z 此时会停止redis。    
    
    
修改守护线程(相当于让redis服务启动的时候 能在后台运行)
将配置文件拷贝到bin目录下:cp /home/redis/redis-5.0.9/redis.conf /usr/local/redis/bin
修改bin下的配置文件: vim /usr/local/redis/bin/redis.conf 
将136行改为 yes   开启守护进程   底行模式 :136 确定

    
再次启动redis的时候 需要我们用配置文件启动
运行redis: ./bin/redis-server  ./bin/redis.conf
客户端连接: ./bin/redis-cli
    
在连接上redis之后 如果想关闭redis 服务: 
shutdown
shutdown save
    
ctrl+c  退出连接
    
ps -ef | grep  redis  找到进程号  kill -9  进程号    

3.5 Redis types and common commands

3.2.1 Redis operation GEO

add location

GEOADD  china  116.23128 40.22077 bj  121.48941 31.40527 sh 113.27324 23.15792 gz 113.88308 22.55329 sz

Get the coordinates of Shenzhen

GEOPOS  china  sz

Calculate the distance between Shenzhen and Shanghai

GEODIST  china  sz  sh  km

Get the area within 800km of 116.23128 40.22077

GEORADIUS  china  116  40  800  km

Get the cities within 800km of Shenzhen

GEORADIUSBYMEMBER  china  sz  800  km

Get the hash value of Shenzhen

GEOHASH  china  sz

3.2.2 Strings in Redis is equivalent to Map<String, String> in java

添加key-value    set   key  value 
SET   a   123456
获取redis中的所有key
keys   *
获取对应key的数据  get  key
GET   a
删除对应key的数据  del  key
DEL  a

Interview question: How do you store Java objects in redis? (To store data in redis in java, hello world set a hello world People object)

A hash

B json (convert the java object into a json string, and when it is taken out from redis in the future, it will still be a json string, and then convert the json string into a java object)

C serialization (serialize and encode java objects, store the code in redis, take it out in the future or encode it, and restore it to java objects after deserialization)

D Logic key 1 Zhangsan 18 Beijing

set  test  abcdefg

获取test子字符                               Getrange  test  1  3
给test重新赋值qwerdf并获取以前的值             getset  test  qwerdf
获取多个key的值                              mget  test  test1
创建test2 并且5秒后过期                       setex test2  5  hahaha  
设置test3 如果没有就设定                      setnx test3  hehehe  
将test第三位后面改成lalala                    setrange  test  3  lala
获取test的长度                               strlen  test
同时设定多个key-value                         mset  t1 a t2 b  t3 c  
Expire test 10

Set  index  0
给index原子性自增1                            INCR  index
给index原子性自减1                            DECR  index
给index原子性自增2                            INCrBY index  2      
给index原子性自增2.5                          INCEBYFLOAT  index  2.5
给index追加haha                              APPEND  index 33

3.2.3 hash Map<String,Map> in redis

Redis hash is a mapping table of field (field) and value (value) of string type, and hash is especially suitable for storing objects.

Each hash in Redis can store 232 - 1 key-value pairs (more than 4 billion).

添加一个set数据        hset       stu  id    1
                     Hset       stu  name 张三
                     Hset       stu  age   18
添加多个数据           hmset      stu  address  北京  grade 一年级
获取全部内容           hgetall    stu
根据关键字获取值        hget       stu   age
获取多个               hmget      stu  id  name  age
获取所有的关键字        hkeys      stu
获取所有的值           hvals      stu
获取长度               hlen      stu
删除某个或多个关键字     hdel      stu   id  name  age
删除stu这个hash        del       stu

3.2.4 list Map<String,List> in redis

Redis lists are simply lists of strings, sorted by insertion order. You can add an element to the head (left) or tail (right) of the list

A list can contain up to 232 - 1 elements (4294967295, over 4 billion elements per list).

左添加数据到list中     lpush    no   1   2   3  4  5  
右添加数据到list中     rpush   no    6  7  8  9   10 
获取长度               llen     no
获取指定索引的数据      lindex   no   3
获取指定范围内的数据    lrange   no    0  -1
在1的前面添加aa       linsert   no  before  1  aa
指定索引位置添加数据   lset    no   1  haha
左删除第一个数据       lpop   no
右删除第一个数据       rpop   no
裁剪指定区域数据       ltrim   no  3   6 

Application scenarios: MQ such as Weibo follower list, message queue, etc.

3.2.5 Redis 的 Set Map<String,Set>

Is an unordered collection of String types. Set members are unique, which means that no duplicate data can appear in the set.

Collections in Redis are implemented through hash tables, so the complexity of adding, deleting, and searching is O(1).

The maximum number of members in a collection is 232 - 1 (4294967295, each collection can store more than 4 billion members).

添加数据到set中     sadd  haha  aa  bb  cc  dd  aa
获取长度            scard  haha
获取所有的元素      smembers   haha  
删除set中的元素     spop   haha  2

Sadd  hehe   aa  bb  cc  dd  ee  
查看第一个集合和其他集合的差异     sdiff    hehe   haha
查看两个集合的交际               sinter   haha   hehe
查看两个集合的交际并存储          sinterstore   heihei  haha  hehe

Application Scenario: Common fans

3.2.6 Redis ordered set (sorted set)

Like collections, Redis ordered collections are also collections of string type elements, and duplicate members are not allowed. The difference is that each element will be associated with a score of type double. Redis uses scores to sort the members of the set from small to large. The members of the ordered set are unique, but the score (score) can be repeated. The collection is implemented through a hash table, so the complexity of adding, deleting, and searching is O(1). The maximum number of members in a collection is 232 - 1 (4294967295, each collection can store more than 4 billion members).

添加数据  zadd  jifen   1.2 zhangsan  2.5 lisi 3.6 wangwu 4.8 zhaoyun 5.9 maliu
查看属性  zcard  jifen
更新数据  zadd  jifen   4.4  zhangsan
查看积分榜   zrangebyscore    jifen   1    9
查看1-5分的数据数量    zcount  jifen   1  5
查询索引0-3之间的数据  zrange  jifen   0  3

Application Scenario: Ranking Points System

3.6 Java Link Redis

1 导入jar包
    <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>

 2  Redis 配置文件 需要配置 
    bind   192.168.150.130 

 3 启动Redis   ./redis-server   redis.conf

 4 java程序 链接Redis完成数据的存储
public class JavaTest {
    
    

    public static void main(String[] args) {
    
    
        
        Jedis jedis = new Jedis("192.168.150.130", 6379);
        jedis.set("hahaha","666666");

    }
}

Guess you like

Origin blog.csdn.net/Liu_wen_wen/article/details/127314401