Try Redis: getting started with Redis

Source: Internet
Author: User
Tags stack pop redis cluster install redis redis tutorial

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. AvailableSETThe command uses the key 'servername' to store the value 'fid ':

  1. SET servername 'fido'

In this way, the data is stored and can be used later.GETRetrieve the stored data:

  1. GET servername // return "fido"

There are also some basic commands for data operations, suchINCRAndDEL.

INCRUsedAtomic locationIncrements a value. WhileDELDelete a value.

  1. SET connections 10
  2. INCR connections // return 11
  3. INCR connections // returns 12
  4. DEL connections
  5. INCR connections // return 1
Specify a life cycle for a value

You can useEXPIRESet the survival time of a value. After this time, the value will be deleted. PassTTLYou can view the survival time of the value.

ForTTL,

  • If no survival time is set for a valueTTLWill 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, useTTLReturns 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 thatSETWhen setting a valueTTLWill be reset to the default value.

Example:

  1. SET resource:lock 'Redis Demo 1'
  2. TTL resource: lock // return-1
  3. EXPIRE resource: lock 120 // set the survival time to 120 seconds
  4. // 7 seconds later
  5. TTL resource: lock // 113 is returned
  6. // 120 seconds later
  7. TTL resource: lock // returns-2
  8. SET resource:lock 'Redis Demo 2'
  9. 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,LPOPAndRPOP.

  • RPUSHAndLPUSHUsed to insert data to the right and left of the list.

  • LLENThe length of the returned list.

  • LRANGEReturns 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.

  • LPOPAndRPOPDelete 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 ):

  1. RPUSH friends "Alice" // create a friends list and add an element "Alice" to it"
  2. RPUSH friends "Bob" // Add the element "Bob" to friends"
  3. LPUSH friends "Sam" // Add the element "Sam" to friends"
  4. LRANGE friends 0-1 // return 1) "Sam", 2) "Alice", 3) "Bob"
  5. LRANGE friends 01 // return 1) "Sam", 2) "Alice"
  6. LRANGE friends 12 // return 1) "Alice", 2) "Bob"
  7. LLEN friends // returns 3
  8. LPOP friends // return "Sam"
  9. RPOP friends // return "Bob"
  10. LLEN friends // return 1
  11. 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,SMEMBERSAndSUNION.

  • SADDAdd a value to the set.

  • SREMDeletes a given value from the set.

  • SISMEMBERReceives 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.

  • SMEMBERSReturns all elements in the set.

  • SUNIONMerge two sets.

Example (like the list, the set does not need to be explicitly created ):

  1. SADD superpowers "flight"
  2. SADD superpowers "x-ray vision"
  3. SADD superpowers "reflexes"
  4. SREM superpowers "reflexes"
  5. SISMEMBER superpowers "flight" // return 1
  6. SISMEMBER superpowers "reflexes" // return 0
  7. SMEMBERS superpowers // return 1) "flight", 2) "x-ray vision"
  8. SADD birdpowers "pecking"
  9. SADD birdpowers "flight"
  10. 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 isZFor example, the data inserted in an ordered set isZADDInsteadSADD.
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:

  1. ZADD hackers 1940"Alan Kay"
  2. ZADD hackers 1906"Grace Hopper"
  3. ZADD hackers 1953"Richard Stallman"
  4. ZADD hackers 1965"Yukihiro Matsumoto"
  5. ZADD hackers 1916"Claude Shannon"
  6. ZADD hackers 1969"Linus Torvalds"
  7. ZADD hackers 1957"Sophie Wilson"
  8. 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 ):

  1. ZRANGE hackers 24 // return 1) "clude Shannon", 2) "Alan Kay", 3) "Richard Stallman"
Hashes

HashesIs the ing between string fields and string values. So it is the best data type of the object:

  1. HSET user:1000 name "John Smith"
  2. HSET user:1000 email "john.smith@example.com"
  3. HSET user:1000 password "s3cret"

UseHGETALLObtain the stored data (return all field names and Field Values ):

  1. HGETALL user:1000

You can also set the attributes of an object once:

  1. HMSET user:1001 name "Mary Jones" password "hidden" email "mjones@example.com"

Obtain a specific field:

  1. 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:

  1. HSET user:1000 visits 10
  2. HINCRBY user: 1000 visits 1 // return 11
  3. HINCRBY user: 1000 visits 10 // return 21
  4. HDEL user:1000 visits
  5. 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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.