linux缓存工具redis

1. Redis 简介

redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便。 [1]
Redis支持主从同步。数据可以从主服务器向任意数量的从服务器上同步,从服务器可以是关联其他从服务器的主服务器。这使得Redis可执行单层树复制。存盘可以有意无意的对数据进行写操作。由于完全实现了发布/订阅机制,使得从数据库在任何地方同步树时,可订阅一个频道并接收主服务器完整的消息发布记录。同步对读取操作的可扩展性和数据冗余很有帮助。
redis的官网地址,非常好记,是redis.io。(域名后缀io属于国家域名,是british Indian Ocean territory,即英属印度洋领地),Vmware在资助着redis项目的开发和维护。

2. Redis的优势和特点

Redis的特点:
内存数据库,速度快,也支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
Redis支持数据的备份,即master-slave模式的数据备份。
支持事务
Redis的优势:
性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s 。
丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。
原子 – Redis的所有操作都是原子性的,同时Redis还支持对几个操作合并后的原子性执行。(事务)
丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性。
Redis与其他key-value存储有什么不同?
Redis有着更为复杂的数据结构并且提供对他们的原子性操作,这是一个不同于其他数据库的进化路径。Redis的数据类型都是基于基本数据结构的同时对程序员透明,无需进行额外的抽象。
Redis运行在内存中但是可以持久化到磁盘,所以在对不同数据集进行高速读写时需要权衡内存,因为数据量不能大于硬件内存。在内存数据库方面的另一个优点是,相比在磁盘上相同的复杂的数据结构,在内存中操作起来非常简单,这样Redis可以做很多内部复杂性很强的事情。同时,在磁盘格式方面他们是紧凑的以追加的方式产生的,因为他们并不需要进行随机访问。

3. Redis安装及配置

[root@xian ~]# wget  http://download.redis.io/releases/redis-6.0.6.tar.gz
[root@xian ~]# tar xf redis-6.0.6.tar.gz 
[root@xian ~]# ls
[root@xian ~]# cd redis-6.0.6
[root@xian redis-6.0.6]# make
server.c:5209:176: error: ‘struct redisServer’ has no member named ‘maxmemory’
错误原因
gcc版本问题,新版本的。redis6.0以上

//查看gcc版本
[root@xian ~]# gcc -v
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
//解决办法
//升级到 5.3及以上版本
[root@xian ~]# yum -y install centos-release-scl
[root@xian ~]# yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
 
[root@xian ~]# scl enable devtoolset-9 bash
 
//注意:scl命令启用只是临时的,推出xshell或者重启就会恢复到原来的gcc版本。
#如果要长期生效的话,执行如下:
[root@xian ~]# echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile

[root@xian ~]# cd redis-6.0.6
[root@xian redis-6.0.6]# make
Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/root/redis-6.0.6/src'

//需要执行:

[root@xian redis-6.0.6]# sudo make distclean

[root@xian redis-6.0.6]# sudo make

[root@xian redis-6.0.6]# make test

[root@xian ~]# rm -rf redis-6.0.6
[root@xian ~]# tar xf redis-6.0.6.tar.gz 
[root@xian ~]# cd redis-6.0.6
[root@xian redis-6.0.6]# make
[root@xian redis-6.0.6]# cd src/
[root@xian src]# ls

redis-cli              redis-server
[root@xian ~]# cp redis-6.0.6/src/redis-server redis-6.0.6/src/redis-cli /usr/bin/
[root@xian ~]# which redis-cli
/usr/bin/redis-cli
[root@xian ~]# redis-server
2026:C 14 Aug 2020 09:39:27.537 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2026:C 14 Aug 2020 09:39:27.537 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=2026, just started
2026:C 14 Aug 2020 09:39:27.537 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
2026:M 14 Aug 2020 09:39:27.538 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 2026
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

2026:M 14 Aug 2020 09:39:27.538 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2026:M 14 Aug 2020 09:39:27.538 # Server initialized
2026:M 14 Aug 2020 09:39:27.538 # 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.
2026:M 14 Aug 2020 09:39:27.538 # 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.
2026:M 14 Aug 2020 09:39:27.538 * Ready to accept connections
[root@xian ~]# ss -antl|grep 6379
LISTEN     0      128          *:6379                     *:*                  
LISTEN     0      128         :::6379                    :::* 

4. redis基础操作

//进入redis操作
[root@xian ~]# redis-cli
127.0.0.1:6379> set z 222
OK
127.0.0.1:6379> get z
"222"
127.0.0.1:6379> quit

//设置密码
[root@xian ~]# cp redis-6.0.6/redis.conf /etc/
[root@xian ~]# vim /etc/redis.conf

requirepass 123456    //修改密码

//重启服务
[root@xian ~]# nohup redis-server /etc/redis.conf &
[1] 2117
[root@xian ~]# nohup: ignoring input and appending output to ‘nohup.out’

[root@xian ~]#
[root@xian ~]# redis-cli
127.0.0.1:6379> set df 15
(error) NOAUTH Authentication required.  
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> set df 15
OK
//常用命令
//显示拥有的key
127.0.0.1:6379> keys *
1) "ba"
2) "df"
3) "fd"
4) "z"
//导出key
127.0.0.1:6379> dump z
"\x00\xc1\xde\x00\t\x00\xc7\xdd\xbe<;\x1d\xc5^"
//删除key
127.0.0.1:6379> del fd
(integer) 1
127.0.0.1:6379> keys *
1) "ba"
2) "df"
3) "z"
//查询key是否存在
127.0.0.1:6379> exists a
(integer) 0
127.0.0.1:6379> exists z
(integer) 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值