Try Redis: getting started with Redis
Opening
Redis is a NoSQL database that stores data using key-value pairs.
Key-value pairs store data based on a key. Then you can use this key to retrieve the stored value. AvailableSET
The command uses the key 'servername' to store the value 'fid ':
SET servername 'fido'
In this way, the data is stored and can be used later.GET
Retrieve the stored data:
GET servername // return "fido"
There are also some basic commands for data operations, suchINCR
AndDEL
.
INCR
UsedAtomic locationIncrements a value. WhileDEL
Delete a value.
SET connections 10
INCR connections // return 11
INCR connections // returns 12
DEL connections
INCR connections // return 1
Specify a life cycle for a value
You can useEXPIRE
Set the survival time of a value. After this time, the value will be deleted. PassTTL
You can view the survival time of the value.
ForTTL
,
If no survival time is set for a valueTTL
Will return-1
, Indicating that the value will not expire (this is the default life of the value: Changsheng );
If a value has a survival time set, useTTL
Returns the remaining life time;
If you use a value that does not exist or that has exceeded the survival time (will be deleted ),TTL
, Will return-2
.
Note thatSET
When setting a valueTTL
Will be reset to the default value.
Example:
SET resource:lock 'Redis Demo 1'
TTL resource: lock // return-1
EXPIRE resource: lock 120 // set the survival time to 120 seconds
// 7 seconds later
TTL resource: lock // 113 is returned
// 120 seconds later
TTL resource: lock // returns-2
SET resource:lock 'Redis Demo 2'
TTL resource: lock // return-1
List)
Redis also supports some complex/composite (complex) data structures. The first one here is the list. A list is a set of ordered values.
Several important ways to interact with a list are as follows:RPUSH
,LPUSH
,LLEN
,LRANGE
,LPOP
AndRPOP
.
RPUSH
AndLPUSH
Used to insert data to the right and left of the list.
LLEN
The length of the returned list.
LRANGE
Returns a child list that receives two parameters that identify the first and last elements of the Child sequence in the original sequence.
If the second element is-1, it indicates to the end of the sequence.
LPOP
AndRPOP
Delete and return the first element at both ends of the stack (same as the stack pop ).
Example (you do not need to explicitly create a list. When you insert a value to a list that does not exist, the list is automatically created. When the last element in the list is pop, the list will be automatically deleted ):
RPUSH friends "Alice" // create a friends list and add an element "Alice" to it"
RPUSH friends "Bob" // Add the element "Bob" to friends"
LPUSH friends "Sam" // Add the element "Sam" to friends"
LRANGE friends 0-1 // return 1) "Sam", 2) "Alice", 3) "Bob"
LRANGE friends 01 // return 1) "Sam", 2) "Alice"
LRANGE friends 12 // return 1) "Alice", 2) "Bob"
LLEN friends // returns 3
LPOP friends // return "Sam"
RPOP friends // return "Bob"
LLEN friends // return 1
LRANGE friends 0-1 // return 1) "Alice"
Set )()
The set is similar to the list, but the set is unordered and the elements in the set are unique.
Several Common commands of the set are:SADD
,SREM
,SISMEMBER
,SMEMBERS
AndSUNION
.
SADD
Add a value to the set.
SREM
Deletes a given value from the set.
SISMEMBER
Receives a parameter to determine whether the value of this parameter is in the set. If 1 is returned in the Set, 0 is returned.
If no parameter is specified, the entire list is returned.
SMEMBERS
Returns all elements in the set.
SUNION
Merge two sets.
Example (like the list, the set does not need to be explicitly created ):
SADD superpowers "flight"
SADD superpowers "x-ray vision"
SADD superpowers "reflexes"
SREM superpowers "reflexes"
SISMEMBER superpowers "flight" // return 1
SISMEMBER superpowers "reflexes" // return 0
SMEMBERS superpowers // return 1) "flight", 2) "x-ray vision"
SADD birdpowers "pecking"
SADD birdpowers "flight"
SUNION superpowers birdpowers // return 1) "pecking", 2) "x-ray vision", 3) "flight"
Sorted Sets)
A set is a very useful data structure, but it is not convenient to use in some cases because it is unordered. So Redis 1.2 introduces an ordered set.
The command for an ordered set isZ
For example, the data inserted in an ordered set isZADD
InsteadSADD
.
An ordered set is similar to a regular set. However, each value of an ordered set has an associated score, which is used to sort the elements in the set.
Here is an example:
ZADD hackers 1940"Alan Kay"
ZADD hackers 1906"Grace Hopper"
ZADD hackers 1953"Richard Stallman"
ZADD hackers 1965"Yukihiro Matsumoto"
ZADD hackers 1916"Claude Shannon"
ZADD hackers 1969"Linus Torvalds"
ZADD hackers 1957"Sophie Wilson"
ZADD hackers 1912"Alan Turing"
In this example, the first parameter (Year of birth) is the sorting score. The following shows the elements whose index values are 2 to 4 (starting from 0 ):
ZRANGE hackers 24 // return 1) "clude Shannon", 2) "Alan Kay", 3) "Richard Stallman"
Hashes
Hashes
Is the ing between string fields and string values. So it is the best data type of the object:
HSET user:1000 name "John Smith"
HSET user:1000 email "john.smith@example.com"
HSET user:1000 password "s3cret"
UseHGETALL
Obtain the stored data (return all field names and Field Values ):
HGETALL user:1000
You can also set the attributes of an object once:
HMSET user:1001 name "Mary Jones" password "hidden" email "mjones@example.com"
Obtain a specific field:
HGET user: 1001 name // return "Mary Jones"
The value type is also very useful in hash fields. For example, it is possible to enter an atomic number:
HSET user:1000 visits 10
HINCRBY user: 1000 visits 1 // return 11
HINCRBY user: 1000 visits 10 // return 21
HDEL user:1000 visits
HINCRBY user: 1000 visits 1 // return 1
End
So far, the try redis tutorial has ended. For more information, see the following link:
- Redis Documentation
- Command Reference
- Implement a Twitter Clone in Redis
- Introduction to Redis Data Types
You may also like the following articles about Redis. For details, refer:
Install and test Redis in Ubuntu 14.04
Basic configuration of Redis master-slave Replication
Redis cluster details
Install Redis in Ubuntu 12.10 (graphic explanation) + Jedis to connect to Redis
Redis series-installation, deployment, and maintenance
Install Redis in CentOS 6.3
Learning notes on Redis installation and deployment
Redis. conf
Redis details: click here
Redis: click here
This article permanently updates the link address: