【Redis3】Redis数据库的学习与实践—Redis的常用命令及高级应用
keys * keys my* #存在返回1 127.0.0.1:6379 exists name (integer) 1 #不存在返回0 127.0.0.1:6379 exists you (integer) 0 127.0.0.1:6379 del name (integer) 1 127.0.0.1:6379 del you (integer) 0 127.0.0.1:6379 expire name 10 (integer) 1 127.0.0.1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Redis数据库的学习与实践—Redis的常用命令及高级应用
一:Redis的常用命令
Redis提供了丰富的命令对数据库和各种数据库类型进行操作,这些命令可以在Linux终端使用。1:键值相关命令。
(1)keys返回满足给定pattern的所有Key;
(2)exists
确认一个key是否存在;
(3)del
删除一个key;
(4)expire
设置一个key的过期时间;
(5)move
将当前数据库中的key转移到其他数据库当中;
(6)persist
移除给定key的过期时间;
(7)randomkey
随机返回key空间的一个key;
(8)rename
重命名key;
(9)type
返回值的类型。
2:服务器相关命令。
(1)ping测试连接是否存活;
(2)echo
在命令行打印内容;
(3)select
选择数据库,Redis数据库编号从0~15,我们可以选择任意一个数据库来进行数据的存取;
(4)quit
退出连接;
(5)dbsize
(6)info
redis服务器的信息;
(7)configget
实时转储收到的请求,返回相关配置的参数;
(8)flushdb
删除当前选择数据库中的所有key;
(9)flushall
删除所有数据库中的所有key;
二:Redis高级实用特性
(1)安全性
设置客户端连接后进行任何其他指定前需要使用的密码。警告:因为redis速度相当快,所以在一台比较好的服务器下,一个外部的用户可以在一秒钟进行150k次的密码尝试,这意味着你需要指定非常非常强大的密码来防止暴力破解。
(2)主从复制
Redis主从复制配置和使用都非常简单。通过主从复制可以允许多个slave server拥有和master server 相同的数据库副本。
特点:
Master可以拥有多个slave;
多个slave可以连接同一个master,还可以连接到其他slave;
主从复制不会阻塞master,在同步数据时,master可以继续处理client请求;
提高系统的伸缩性。
主从复制过程:
Slave与master建立连接,发送sync同步命令
Master会启动一个后台进程,将数据库快照保存到文件中,同时master住进程会开始收集新的写命令并缓存。
后台完成保存后,就将此文件发送给slave
Slave将此文件保存到硬盘上
配置主从服务器
在slave的配置文件(redis.conf)中加入以下配置:
slaveof 192.168.1.1 6379 #指定master的ip和端口 masterauth lamp #这是主机的密码 #设置完成后,可以通过info命令来查看主从信息(role:master/slave) |
Redis对事务的支持目前还比较简单。Redis只能保证一个client发起的事务中的命令可以连续的执行,而中间不会插入其他client的命令。当一个client在一个连接中发出multi命令时,这个连接会进入一个事务上下文,该连接后续的命令不会立即执行,而是先放到一个队列中,当执行exec命令时,redis会顺序的执行队列中的所有命令。
简单事务处理:
127.0.0.1:6379> get age "30" 127.0.0.1:6379> multi OK 127.0.0.1:6379> set age 10 QUEUED 127.0.0.1:6379> set age 20 QUEUED 127.0.0.1:6379> exec 1) OK 2) OK 127.0.0.1:6379> get age "20" |
取消一个事务:
127.0.0.1:6379> get age "20" 127.0.0.1:6379> multi OK 127.0.0.1:6379> set age 30 QUEUED 127.0.0.1:6379> set age 50 QUEUED 127.0.0.1:6379> discard OK 127.0.0.1:6379> get age "20" |
Redis事务的缺陷,通常来说,事务中的每一个事务都要执行成功,事务里的所有操作才会生效。但是Redis事务的缺陷就是,当事务中的操作,有一个没有成功的时候,另外一个操作照样成功。这样事务在某一个操作错误的时候,没有了回滚的机制,这是有待改进的地方。
乐观锁:复杂事务控制
大多是基于数据版本(version)的记录机制实现的。即为数据增加一个版本表示,在基于数据库表的版本解决方案中,一般是通过为数据库表添加一个“version”字段来实现读取出数据时,将此版本号一同读出,之后更新时,对此版本号加1。此时,将提交数据的版本号与数据库表对应记录的当前版本号进行比对,如果提交的数据版本号大于数据库当前版本号,则予以更新,否则认为是过期数据。
Redis乐观锁实例:
假设有一个age的key,我们开2个session来对age进行赋值操作,我们来看一下结果。
#第一步session1 127.0.0.1:6379> get age "20" 127.0.0.1:6379> watch age OK 127.0.0.1:6379> multi OK |
#第二步session2 127.0.0.1:6379> set age 30 OK 127.0.0.1:6379> get age "30" |
#第三步session1 127.0.0.1:6379> set age 20 QUEUED 127.0.0.1:6379> exec (nil) 127.0.0.1:6379> get age "30" |
Watch命令会见识给定的key,当exec时,如果监视的key从调用watch后发生过变化,则整个事务会失败。也可以调用watch多次监视多个key。这样就可以对指定的key加乐观锁了。注意watch的key是对整个连接有效的,事务也一样。如果连接断开,监视和事务都会被自动清除。当然了exec,discard,unwatch命令都会清除连接中的所有监视。
(4)持久化机制Redis是一个支持持久化的内存数据库,也就是说redis需要经常将内存中的数据同步到硬盘来保证持久化。
Redis支持两种持久化方式:snapshotting(快照)也是默认方式;Append-only file(缩写aof)的方式。
(5)发布订阅消息发布订阅(pub/sub)是一种消息通信模式,主要的目的是接触消息发布者和消息订阅者之间的耦合,Redis作为一个pub/sub的server,在订阅者和发布者之间起到了消息路由的功能。订阅者可以通过subscribe和psubscribe命令向redis server订阅自己感兴趣的消息类型,redis将信息类型称为通道(channel)。当发布者通过publish命令向redis server发送特定类型的信息时,订阅该信息类型的全部client都会受到此消息。
(6)虚拟内存的使用Redis的虚拟内存与操作系统的虚拟内存不是一回事,但是思路和目的都是相同的。都是暂时把不经常访问的数据从内存交换到磁盘中,从而腾出宝贵的内存空间用于其他需要访问的数据。尤其是对于redis这样的内存数据库,内存总是不够用的。除了可以将数据分割到多个redisserver外。另外能够提高数据库容量的办法就是使用虚拟内存把那些不经常访问的数据交换到磁盘上。
Vm-enabled yes #开启vm功能 Vm-swap-file /tmp/redis.swap #交换出来的value保存的文件路径 Vm-max-memory 1000000 #redis使用的最大内存上限 Vm-page-size 32 #每个页面的大小32字节 Vm-pages 13417728 #最多使用多少页面 Vm-max-threads4 #用于执行value对象换入患处的工作线程数量 注:Redis系列博客均是结合兄弟连Redis学习视频的笔记和自己的实践总结而成。 |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

There are two types of Redis data expiration strategies: periodic deletion: periodic scan to delete the expired key, which can be set through expired-time-cap-remove-count and expired-time-cap-remove-delay parameters. Lazy Deletion: Check for deletion expired keys only when keys are read or written. They can be set through lazyfree-lazy-eviction, lazyfree-lazy-expire, lazyfree-lazy-user-del parameters.
