目錄
2. Redis installation
4. Simple use
5. Specific operations
1. Key operation
Redis string can contain any data , including jpg pictures or serialized objects
3. Introduction and use of List linked list type
How to operate the linked list?
5. SortSet sorted set type operation
首頁 後端開發 php教程 從零入手Redis緩存

從零入手Redis緩存

Jun 04, 2018 am 09:48 AM
redis 獲得 快取

這篇文章主要介紹了關於從零入手Redis緩存,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

  1. ##Redis介紹

Redis是一個開源的使用ANSI C語言編寫、支援網路、可基於記憶體亦可持久化的日誌型、Key-Value#資料庫,並提供多種語言的#API。從2010315日起,Redis的開發工作由VMware主持。從20135#月開始,Redis##的開發由 Pivotal贊助。

redis

是一個key-value儲存系統。和Memcached類似,它支援儲存的value類型相對更多,包括string( 字串)list(鍊錶)set(集合)zset(sorted set --##有序集合)hash(哈希類型)。這些資料型別都支援push/popadd/remove及取交集並集和差集及更豐富的操作,而且這些操作都是原子性的。在此基礎上,redis支援各種不同方式的排序。與memcached一樣,為了保證效率,資料都是快取在記憶體中。區別的是redis會週期性的把更新的資料寫入磁碟或把修改操作寫入追加的記錄文件,並且在此基礎上實現了master-slave(主從)同步。

Redis is a high-performance key-value database. The emergence of redis has largely compensated for memcached of this typekey/valueInsufficient storage can play a very good supplementary role to the relational database in some situations. It provides Java, C/C , C#, PHP,JavaScript,Perl,Object-C , Python, Ruby, Erlang and other clients, use very convenient. [1]

Redis supports master-slave synchronization. Data can be synchronized from the master server to any number of slave servers, and the slave server can be a master server associated with other slave servers. This enables Redis to perform single-level tree replication. Saving can write data intentionally or unintentionally. Since the publication/ subscription mechanism is fully implemented, when the slave database synchronizes the tree anywhere, it can subscribe to a channel and receive the complete message publishing record of the master server. Synchronization is helpful for scalability and data redundancy of read operations. The official website address of

redis is very easy to remember, it is redis.io. (I checked specifically and found that the domain name suffix io belongs to the national domain name, which is british Indian Ocean territory, that is, the British Indian Ocean Territory)

Currently, Vmware is funding the development and maintenance of the redis project.

2. Redis installation

Extract the installation file

##After decompression

Execute make to compile

Compile OK,

Enter the src directory


Redis-cli terminal operation

Redis-server file to start redis service

Redis-benchmark pressure Test file

Redis-check-xx Car inspection backup file script

Create the Redis running directory and copy the two running files to the past

Copy the configuration file to the past

3. Start the Redis service

Signs of successful front-end startup of the redis service

./redis-server

There is a problem at this time. The currently started one cannot be turned off. It will be gone once it is turned off, so it needs to be modified.

Stop the service Ctrl z

Stop the redis service first

Use the background to start the redis service

vim redis.conf

Change to yes to save and set Start redis in the background

Start again

I find that it is still started in the front end

Bring it on when starting Start the configuration file together

Check the redis process

It is found that redis has been started

4. Simple use

Three variables are set, and these three variables are stored in memory.

How to read?

Get!

5. Specific operations

1. Key operation

In redis, "\n" and spaces cannot be used as In addition to the component content of the name, other content can be used as the name part of the key. There is no requirement for the name length

In other words, it is the variable name

##DbsizeExpire key seconds Ttl key##Select db-indexMove key db-indexFlushdbFlushall

Code

Function

Exists key

Does it exist

Del key1 key2….

Delete the specified key

Type key

Returns the value type of the given key

Key pattern

Return all keys that match the specified pattern

##Rename oldkey newkey

Change name

##Return the number of keys in the current database

Specify the expiration time for the key

Returns the expiration seconds of key

Select database

Move key from the current database to the specified database

Delete all keys in the current database

Delete all keys in all databases

Here is an example of using it
2. String type operation

String is the most basic type of redis

Redis string can contain any data , including jpg pictures or serialized objects

The maximum limit of a single value is 1G bytes

Code##Set the value corresponding to the key to a string type value##Set the values ​​of multiple keys at onceGet the values ​​of multiple keys at one timeOperation on key value##Decr keySame as above – operation

Function

##Set key value

Mset key1 value1…keyN valueN

Mget key1 key2 … keyN

Incr key

##Incrby key integer

Same as incr plus specified value

Decrby key integer

Same as decr minus the specified value

Append key value

Append value to the string of the specified key

##Substr key start end

Return the string value of the intercepted key

3. Introduction and use of List linked list type

The List type is actually a doubly linked list

If you want to query The top 10 latest users,

have to be checked one by one, which consumes too many resources

List linked list example:

Through list The linked list stores the information of the latest 5 users who logged into the system

New users come in and old users are kicked out

How to operate the linked list?

Add a string element to the head of the list corresponding to the keyDeletes an element from the end of the list and returns the deleted element. If the key value does not exist, 0 is returned. If the corresponding type of key is not list returns error ##Rpush key string at the end Lpop keyLtrim key start end4. Set collection type

##Code

##Function

Lpush key string

Rpop key

Lien key returns the length of the list corresponding to key

Same as above, add

Deletes the element from the head of the list and returns the deleted element

Intercept the list and keep the elements in the specified range

Redis’ set is an unordered collection of string type.

Set elements can contain up to (2 to the 32nd power -1) elements

Each element in each set cannot be repeated

CodeFunctionSadd key memberSrem key memberSmove p1 p2 memberScard keySismember key memberSinter key1 key2.. . . Sunion key1 key2Sdiff key1 key2. Smembers key


There is a key with five elements in it

Then add a Linken

5. SortSet sorted set type operation

Like set, sorted set is also a collection of string elements

The difference is that each element is associated with a weight

The elements in the set can be obtained in order through the weight value

Case:

Use sort set to obtain the top 5 most popular posts for learning

Each element in the sorted set is a combination of value and weight

Add a string element to the set collection corresponding to key and return success 1

Removes the given element from the key corresponding set and returns 1 successfully

Remove member from the corresponding set of p1 and add it to the corresponding set of p2

Returns the number of elements in the set

Determine whether the member exists in the set

Returns the intersection of all given keys

Returns the union of all given keys

Return the difference set of all given keys

Returns all elements of the set corresponding to the key, and the result is unordered

Delete the specified element, 1 successful, 0 does not exist##Zincrby key incr member##Zrank key memberZrange key start endZrevrange key start endZcard keyZscore key elemet##Zremrangebyrank key min max

Code

Function

##Zadd key score member

Add elements to the collection, and update the corresponding score if the element exists in the collection

##Zrem key member

Increase the score value of the corresponding member according to the incr range and return the score value

Returns the ranking (subscript) of the specified element in the set. The elements in the set are sorted from small to large by score

Similar to the Irange operation, the elements in the specified range are specified from the collection, and the ordered results are returned

Same as above, the return is in reverse order

Returns the number of elements in the collection

Returns the score corresponding to the given element

#Delete the elements in the set that are ranked in the given point interval


6. Snapshot persistence

It takes 900 seconds for one key change to be saved (snapshot)

It takes 300 seconds for ten key changes to be saved ( Snapshot) Save

Ten thousand key changes per minute (snapshot) Save

The benefits of such control:

The frequency of data modification is very high, and the frequency of backup is also high ,

The frequency of data modification is low, and the frequency of backup is also low.

The name of the snapshot persistence



##Manually initiate snapshot persistence

If for this machine:

7. AOF persistence

Essence: Back up every "write" command (add, delete, modify) executed by the user to a file, and execute the specific "write" command when restoring

Turning on AOF persistence will clear the inside of redis Data

Enable AOF persistence

The configuration file is modified and restart the service

View the redis process: Ps – A | grep redis

-9Force kill the process

Start a new process

Aof adds persistent backup frequency

Always forces writing to the disk immediately after receiving a write command. It is very slow, but can maintain complete persistence.

Everysec forces writing to the disk once per second. It has done a good job in terms of performance and persistence. A compromise, recommended

No relies entirely on OS for the best performance, but persistence is not guaranteed

8. Master-slave mode

In order to reduce the load of each redis server, you can set Several, and in master-slave mode

One server load "writes"

Other server loads "read"

The master server will automatically synchronize to the slave server


Modify IP address and port number

以上是從零入手Redis緩存的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1421
52
Laravel 教程
1315
25
PHP教程
1266
29
C# 教程
1239
24
redis集群模式怎麼搭建 redis集群模式怎麼搭建 Apr 10, 2025 pm 10:15 PM

Redis集群模式通過分片將Redis實例部署到多個服務器,提高可擴展性和可用性。搭建步驟如下:創建奇數個Redis實例,端口不同;創建3個sentinel實例,監控Redis實例並進行故障轉移;配置sentinel配置文件,添加監控Redis實例信息和故障轉移設置;配置Redis實例配置文件,啟用集群模式並指定集群信息文件路徑;創建nodes.conf文件,包含各Redis實例的信息;啟動集群,執行create命令創建集群並指定副本數量;登錄集群執行CLUSTER INFO命令驗證集群狀態;使

redis數據怎麼清空 redis數據怎麼清空 Apr 10, 2025 pm 10:06 PM

如何清空 Redis 數據:使用 FLUSHALL 命令清除所有鍵值。使用 FLUSHDB 命令清除當前選定數據庫的鍵值。使用 SELECT 切換數據庫,再使用 FLUSHDB 清除多個數據庫。使用 DEL 命令刪除特定鍵。使用 redis-cli 工具清空數據。

redis怎麼讀取隊列 redis怎麼讀取隊列 Apr 10, 2025 pm 10:12 PM

要從 Redis 讀取隊列,需要獲取隊列名稱、使用 LPOP 命令讀取元素,並處理空隊列。具體步驟如下:獲取隊列名稱:以 "queue:" 前綴命名,如 "queue:my-queue"。使用 LPOP 命令:從隊列頭部彈出元素並返回其值,如 LPOP queue:my-queue。處理空隊列:如果隊列為空,LPOP 返回 nil,可先檢查隊列是否存在再讀取元素。

centos redis如何配置Lua腳本執行時間 centos redis如何配置Lua腳本執行時間 Apr 14, 2025 pm 02:12 PM

在CentOS系統上,您可以通過修改Redis配置文件或使用Redis命令來限制Lua腳本的執行時間,從而防止惡意腳本佔用過多資源。方法一:修改Redis配置文件定位Redis配置文件:Redis配置文件通常位於/etc/redis/redis.conf。編輯配置文件:使用文本編輯器(例如vi或nano)打開配置文件:sudovi/etc/redis/redis.conf設置Lua腳本執行時間限制:在配置文件中添加或修改以下行,設置Lua腳本的最大執行時間(單位:毫秒)

redis命令行怎麼用 redis命令行怎麼用 Apr 10, 2025 pm 10:18 PM

使用 Redis 命令行工具 (redis-cli) 可通過以下步驟管理和操作 Redis:連接到服務器,指定地址和端口。使用命令名稱和參數向服務器發送命令。使用 HELP 命令查看特定命令的幫助信息。使用 QUIT 命令退出命令行工具。

redis計數器怎麼實現 redis計數器怎麼實現 Apr 10, 2025 pm 10:21 PM

Redis計數器是一種使用Redis鍵值對存儲來實現計數操作的機制,包含以下步驟:創建計數器鍵、增加計數、減少計數、重置計數和獲取計數。 Redis計數器的優勢包括速度快、高並發、持久性和簡單易用。它可用於用戶訪問計數、實時指標跟踪、遊戲分數和排名以及訂單處理計數等場景。

redis過期策略怎麼設置 redis過期策略怎麼設置 Apr 10, 2025 pm 10:03 PM

Redis數據過期策略有兩種:定期刪除:定期掃描刪除過期鍵,可通過 expired-time-cap-remove-count、expired-time-cap-remove-delay 參數設置。惰性刪除:僅在讀取或寫入鍵時檢查刪除過期鍵,可通過 lazyfree-lazy-eviction、lazyfree-lazy-expire、lazyfree-lazy-user-del 參數設置。

如何優化debian readdir的性能 如何優化debian readdir的性能 Apr 13, 2025 am 08:48 AM

在Debian系統中,readdir系統調用用於讀取目錄內容。如果其性能表現不佳,可嘗試以下優化策略:精簡目錄文件數量:盡可能將大型目錄拆分成多個小型目錄,降低每次readdir調用處理的項目數量。啟用目錄內容緩存:構建緩存機制,定期或在目錄內容變更時更新緩存,減少對readdir的頻繁調用。內存緩存(如Memcached或Redis)或本地緩存(如文件或數據庫)均可考慮。採用高效數據結構:如果自行實現目錄遍歷,選擇更高效的數據結構(例如哈希表而非線性搜索)存儲和訪問目錄信

See all articles