zoukankan      html  css  js  c++  java
  • Redis(一)Redis基础

      一、Redis是什么

      Redis是一种基于键值对(key-value)的NoSQL数据库,与很多键值对数据库不同的是,Redis中的值可以是由string(字符串)、hash(哈希)、list(列表)、set(集合)、zset(有序集合)、Bitmaps(位图)、HyperLogLog、GEO(地理信息定位)等多种数据结构和算法组成,因此Redis可以满足很多的应用场景,而且因为Redis会将所有数据都存放在内存中,所以它的读写性能非常惊人。

      不仅如此,Redis还可以将内存的数据利用快照和日志的形式保存到硬盘上,这样在发生类似断电或者机器故障的时候,内存中的数据不会“丢失”。

      除了上述功能以外,Redis还提供了键过期、发布订阅、事务、流水线、Lua脚本等附加功能。

      二、Redis特性

    • 速度快:①所有数据都是存放在内存中;②使用C语言实现的;③使用单线程架构,预防了多线程可能产生的竞争问题;④源码集性能和优雅于一身。
    • 基于键值对的数据结构服务器:Redis中的值不仅可以是字符串,还可以是具体的数据结构。五种数据结构,同时还有Bigmaps、HyperLogLog和GEO。
    • 丰富的功能:键过期实现缓存、发布订阅实现消息系统、Lua脚本功能、事务功能、流水线(Pipeline)功能
    • 简单稳定:①Redis源码很少;②Redis使用单线程;③Redis不需要依赖于操作系统中的类库。
    • 客户端语言多:Redis提供了简单的TCP通信协议,同时支持Redis的客户端语言几乎涵盖了主流的编程语言。
    • 持久化:将数据放在内存中是不安全的,一旦发生断电或者机器故障,重要的数据可能就会丢失,所以Redis提供两种持久化方式:RDB和AOF,可以将内存的数据保存在硬盘中。
    • 主从复制:Redis提供了复制功能,实现了多个相同数据的Redis副本。
    • 高可用性和分布式:实现了高可用性的Redis Sentinel和分布式的Redis Cluster。

      三、Redis可以做什么

    • 缓存:Redis提供了键值过期时间设置,并且也提供了灵活控制最大内存和内存溢出后的淘汰策略。
    • 排行榜系统:Redis提供了列表和有序集合数据结构,合理地使用这些数据结构可以很方便地构建各种排行榜系统。
    • 计数器应用:Redis天然支持技术功能而且技术的性能也非常好,可以说是计数器系统的重要选择。
    • 社交网络:赞/踩、粉丝、共同好友、推送、下拉刷新等是社交网络的必备功能,由于社交网站访问量通常比较大,而且传统的关系型数据不太适合保存这种类型的数据,Redis提供的数据结构可以相对比较容易地实现这些功能。
    • 消息队列系统:Redis提供了发布订阅功能和阻塞队列的功能,对于一般的消息队列功能基本可以满足。

      四、Redis安装与启动

      1.在Redis官网https://redis.io/上下载源码redis-4.0.9.tar.gz

      2.执行以下命令:

    1 tar xzf redis-4.0.9.tar.gz
    2 ln -s redis-4.0.9 redis
    3 cd redis
    4 make
    5 make install

      3.安装后,查看Redis的版本:

    root@myubuntu:/home/software/redis# redis-cli -v
    redis-cli 4.0.9

      4.查看redis可执行文件:

    root@myubuntu:/usr/local/bin# ls -l
    总用量 23612
    -rwxr-xr-x 1 root root 2701600 5月  31 09:20 redis-benchmark                  Redis基准测试工具
    -rwxr-xr-x 1 root root 6191088 5月  31 09:20 redis-check-aof           Redis AOF持久化文件检测和修复工具
    -rwxr-xr-x 1 root root 6191088 5月  31 09:20 redis-check-rdb           Redis RDB持久化文件检测和修复工具
    -rwxr-xr-x 1 root root 2894704 5月  31 09:20 redis-cli               Redis命令行客户端
    lrwxrwxrwx 1 root root      12 5月  31 09:20 redis-sentinel -> redis-server   启动Redis Sentinel
    -rwxr-xr-x 1 root root 6191088 5月  31 09:20 redis-server             启动Redis

      5.启动Redis有三种方法:默认配置、运行配置、配置文件启动

      (1)默认配置

      从打印日志可以看出,当前版本4.0.9,默认端口6379,进程号6985.

      直接启动无法自定义配置,所以这种方式不建议使用。

    root@myubuntu:/usr/local/bin# redis-server 
    6985:C 31 May 09:38:46.046 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    6985:C 31 May 09:38:46.046 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=6985, just started
    6985:C 31 May 09:38:46.046 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
    6985:M 31 May 09:38:46.047 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                    _._                                                  
               _.-``__ ''-._                                             
          _.-``    `.  `_.  ''-._           Redis 4.0.9 (00000000/0) 64 bit
      .-`` .-```.  ```/    _.,_ ''-._                                   
     (    '      ,       .-`  | `,    )     Running in standalone mode
     |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
     |    `-._   `._    /     _.-'    |     PID: 6985
      `-._    `-._  `-./  _.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |           http://redis.io        
      `-._    `-._`-.__.-'_.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |                                  
      `-._    `-._`-.__.-'_.-'    _.-'                                   
          `-._    `-.__.-'    _.-'                                       
              `-._        _.-'                                           
                  `-.__.-'                                               
    
    6985:M 31 May 09:38:46.047 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    6985:M 31 May 09:38:46.047 # Server initialized
    6985:M 31 May 09:38:46.047 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
    6985:M 31 May 09:38:46.047 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    6985:M 31 May 09:38:46.047 * DB loaded from disk: 0.000 seconds
    6985:M 31 May 09:38:46.047 * Ready to accept connections

      (2)运行启动

      可以通过这种方式修改配置名和值,但是如果需要修改的配置较多或者希望将配置保存到文件中,不建议使用这种方式。

    root@myubuntu:/usr/local/bin# redis-server --port 6380

      (3)配置文件启动

      通常是使用读取配置文件来启动Redis的方式,因为这种方式可以提供更大的灵活性,或者应用在一台机器上启动多个Redis的场景下。

    root@myubuntu:/home/software/redis# redis-server /home/software/redis/redis.conf 

      6.Redis命令行客户端

      在启动Redis服务后,使用redis-cli连接、操作Redis服务。redis-cli可以使用两种方式连接Redis服务器:

      (1)交互式方式

    bigjun@myubuntu:~/下载$ redis-cli -h 127.0.0.1 -p 6379
    127.0.0.1:6379> set hello world
    OK
    127.0.0.1:6379> get hello
    "world"
    127.0.0.1:6379> get hello
    "world"
    127.0.0.1:6379> set myname bigjun
    OK
    127.0.0.1:6379> get myname
    "bigjun"
    127.0.0.1:6379> shutdown
    not connected> redis-cli shutdown
    Could not connect to Redis at 127.0.0.1:6379: Connection refused
    not connected> redis-cli 
    Could not connect to Redis at 127.0.0.1:6379: Connection refused

      (2)命令方式

    bigjun@myubuntu:~/下载$ redis-cli -h 127.0.0.1 -p 6379 get hello
    “world”

       7.停止Redis服务

      除了通过shutdown命令关闭Redis服务以外,还可以通过kill进程号的方式关闭掉Redis。一般来说,断开与客户端的连接、持久化文件生成,是一种相对优雅的关闭方式。

    redis-cli shutdown
  • 相关阅读:
    svn hooks post-commit钩子自动部署
    curl post数据
    php 操作提示框
    php分页类 可直接调用
    微信web端生成支付二维码
    php 数据库类
    虚拟机中的CentOS 7设置固定IP连接最理想的配置
    多并发时支付如何保持账户余额的一致性?
    Spring核心机制:依赖注入
    .net 系列:并发编程之一【并发编程的初步理论】
  • 原文地址:https://www.cnblogs.com/BigJunOba/p/9114861.html
Copyright © 2011-2022 走看看