搜尋
首頁後端開發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
PHP和Python:解釋了不同的範例PHP和Python:解釋了不同的範例Apr 18, 2025 am 12:26 AM

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP和Python:深入了解他們的歷史PHP和Python:深入了解他們的歷史Apr 18, 2025 am 12:25 AM

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

在PHP和Python之間進行選擇:指南在PHP和Python之間進行選擇:指南Apr 18, 2025 am 12:24 AM

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

PHP和框架:現代化語言PHP和框架:現代化語言Apr 18, 2025 am 12:14 AM

PHP在現代化進程中仍然重要,因為它支持大量網站和應用,並通過框架適應開發需求。 1.PHP7提升了性能並引入了新功能。 2.現代框架如Laravel、Symfony和CodeIgniter簡化開發,提高代碼質量。 3.性能優化和最佳實踐進一步提升應用效率。

PHP的影響:網絡開發及以後PHP的影響:網絡開發及以後Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型?PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型?Apr 17, 2025 am 12:25 AM

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP如何處理對象克隆(克隆關鍵字)和__clone魔法方法?PHP如何處理對象克隆(克隆關鍵字)和__clone魔法方法?Apr 17, 2025 am 12:24 AM

PHP中使用clone關鍵字創建對象副本,並通過\_\_clone魔法方法定制克隆行為。 1.使用clone關鍵字進行淺拷貝,克隆對象的屬性但不克隆對象屬性內的對象。 2.通過\_\_clone方法可以深拷貝嵌套對象,避免淺拷貝問題。 3.注意避免克隆中的循環引用和性能問題,優化克隆操作以提高效率。

PHP與Python:用例和應用程序PHP與Python:用例和應用程序Apr 17, 2025 am 12:23 AM

PHP適用於Web開發和內容管理系統,Python適合數據科學、機器學習和自動化腳本。 1.PHP在構建快速、可擴展的網站和應用程序方面表現出色,常用於WordPress等CMS。 2.Python在數據科學和機器學習領域表現卓越,擁有豐富的庫如NumPy和TensorFlow。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境