這篇文章主要介紹了關於從零入手Redis緩存,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
Redis是一個開源的使用ANSI C語言編寫、支援網路、可基於記憶體亦可持久化的日誌型、Key-Value#資料庫,並提供多種語言的#API。從2010年3月15日起,Redis的開發工作由VMware主持。從2013年5#月開始,Redis##的開發由 Pivotal贊助。
redis是一個key-value儲存系統。和Memcached類似,它支援儲存的value類型相對更多,包括string( 字串)、list(鍊錶)、set(集合)、zset(sorted set --##有序集合)和hash(哈希類型)。這些資料型別都支援push/pop、add/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.
Extract the installation file
##After decompression Execute make to compile Compile OK, Enter the src directory./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
Three variables are set, and these three variables are stored in memory.
How to read?
Get!
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
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 db-index |
Select database |
Move key db-index |
Move key from the current database to the specified database |
Flushdb |
Delete all keys in the current database |
| Flushall
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
The maximum limit of a single value is 1G bytes
Function |
##Set key value | |||||||||||||||||||||||||||||||||||||||||||||||||||||
Mset key1 value1…keyN valueN |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Mget key1 key2 … keyN | |||||||||||||||||||||||||||||||||||||||||||||||||||||
Incr key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
##Decr key | Same as above – operation | |||||||||||||||||||||||||||||||||||||||||||||||||||||
##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 typeThe 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?
Redis’ set is an unordered collection of string type. Set elements can contain up to (2 to the 32nd power -1) elementsEach element in each set cannot be repeated
|
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 |
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
| Delete the specified element, 1 successful, 0 does not exist|
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 |
##Zremrangebyrank key min max |
#Delete the elements in the set that are ranked in the given point interval |
|
以上是從零入手Redis緩存的詳細內容。更多資訊請關注PHP中文網其他相關文章!