Table of Contents
redis 简介
项目中的经验
redis作为写文件的缓存
redis使用感受
Home Database Mysql Tutorial redis 应用实践

redis 应用实践

Jun 07, 2016 pm 04:38 PM
redis practice application Open source Introduction key value

redis 简介 redis 是一个很火的开源键值内存数据库。官方网站是 http://redis.io/。 redis的应用很广泛,就我的感觉来说,web app用的比较多。 项目中的经验 我在项目中用到了redis来作为缓存和pubsub功能,体会到了它的一些强大,这里做一下总结。 刚接触re

redis 简介

redis 是一个很火的开源键值内存数据库。官方网站是 http://redis.io/。
redis的应用很广泛,就我的感觉来说,web app用的比较多。

项目中的经验

我在项目中用到了redis来作为缓存和pubsub功能,体会到了它的一些强大,这里做一下总结。

刚接触redis时的两个命令是 get/set,很简单,set key val,就在redis的某个编号的数据库中存放了一个键值对,通过get key 可以获取这个键对应的值。和平时使用的mysql等关系型数据库有很大的区别,设置值,读取值太简洁了,这也是内存数据库的一个特点,要能够快速,简洁的添加、读取、删除值。

在我的项目中,把redis当做了一个pubsub工具来用,同时也用作了写文件的缓存。下面将分别描述一下如何使用的。

redis作为写文件的缓存

我有大概四千个文件需要被写入,源数据都是一条数据只有58字节,一秒钟大概有1000条这样的数据产生,也就是说,一秒钟要打开关闭1000个文件并写入数据,对系统和磁盘来说,都是个压力,并且这样的解决方法很有问题,很自然的就想到了用缓存。缓存了一定量的数据后,再写入文件中,减少磁盘操作,同时也提高了写入效率。

如何做缓存,如果自己来维护,就要维护4000份不同文件的缓存,用程序来维护4000份缓存,并且根据不同的缓存大小触发写文件的操作,想想都觉得困难有点大。这个时候自然地就想到了redis这个内存数据库。
redis有一个操作室 append,它向一个key追加内容,如果key不存在就创建一个,同时返回变更后的字符串长度,这个命令实在是太好用了,一下子就解决了我的问题。
通过在redis中维护4000个key,根据接收到的不同的数据,追加到不同的key上,根据返回的数据长度,当达到预期的长度后,就取出来并清空原来的值,把取出来的值写入文件中,整个过程,逻辑清晰明白。

在实际编写程序的过程中,还有一些细节需要处理,比如说,对于每一个append操作,客户端都要和redis服务器进行交互,获取提交结果,那么有无办法减少这个操作,改为批量提交呢?有的。redis有个pipeline命令,就是干这个活的,客户端通过pipeline向服务器提交数据,redis服务器不立即返回结果,而是放在一个队列中,当客户端执行了execute操作后,一次性按照提交的顺序返回执行的结果。然后再对返回的结果进行对比,寻找符合长度的数据。这样的操作,可以大幅度降低耗时,提高程序运行速度和性能。
当寻找到了符合预定长度的数据时,就使用redis的 getset(key, "") 命令,这个命令取出来key中的值,同时给key赋一个新值,这就满足了原子操作性,不会出现当一个进程对这个key取数据后,赋值新数据前,另外一个进程往这个key中写数据而导致数据的丢失的问题。找到了需要写入文件的数据后,如何通知程序写入文件呢?有同步和异步两种方法,同步就是在同一个进程/线程中继续写文件,等文件写完毕后,再做其他操作,这样的好处也许是程序实现相对简单,但是在高性能环境下,却不一定是可以接受的方法,耗时!异步呢?实现异步的方式有很多,一种是启一个新的线程去做这个时,另外一种就是用消息队列。在这个场景中,起一个新的线程去做这个事不讨好,并且也不够稳定。有一个celery的消息队列,网站自身的介绍是近乎实时的分布式消息队列。

正好,把celery拿过来用,当程序检测到符合长度后,取出来数据,通过publish的方式,通知一个sub client,subclient 接收到这个消息后,就调用celery 的task 去执行写的操作,如此,整个业务逻辑流程就串了起来,见下图的描述。

请输入图片描述

redis使用感受

redis使用起来非常的方便,同时也容易造成滥用。任何工具都有它的适用场景,有很多人拿到一个好用的工具,干什么都要用上来。我以前也犯过类似的错误,觉得c语言就是好,写一个下载网页的程序都要用c来写,结果换用python 就是几句话的事,人生苦短,不要和自己过不去,用合适的工具,完成合适的事情。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

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

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

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.

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

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.

How to use redis lock How to use redis lock Apr 10, 2025 pm 08:39 PM

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to solve data loss with redis How to solve data loss with redis Apr 10, 2025 pm 08:24 PM

Redis data loss causes include memory failures, power outages, human errors, and hardware failures. The solutions are: 1. Store data to disk with RDB or AOF persistence; 2. Copy to multiple servers for high availability; 3. HA with Redis Sentinel or Redis Cluster; 4. Create snapshots to back up data; 5. Implement best practices such as persistence, replication, snapshots, monitoring, and security measures.

How to use the redis command line How to use the redis command line Apr 10, 2025 pm 10:18 PM

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.

See all articles