The application of Redis in PHP (list article)

Source: Internet
Author: User
Tags redis version

This article is for me to read the redis reference manual in conjunction with bo Friends blog Writing, note the difference between Php_redis and REDIS-CLI (mainly return value type and parameter usage)

Directory:

I. List (lists)

1, Lpush

The Redis lpush command inserts one or more values into the list header. If the key does not exist, an empty list is created and the Lpush operation is performed. An error is returned when key exists but is not a list type. (the Lpush command, which was prior to Redis version 2.4, only accepts single value values.) )

Grammar:

Redis 127.0.0.1:6379> lpush key_name VALUE1. Valuen

Return value: The length of the list after the Lpush command is executed.

Available Versions: >= 1.0.0

Time complexity:O (1)

Specific examples:

<?PHP$redis=NewRedis ();$redisConnect (' 127.0.0.1 ', 6379);$redis-Flushall ();Var_dump($redis-Lpush (' favorite_fruit ', ' cherry '));//key does not exist, create a new list, return int 1Var_dump($redis-Lpush (' favorite_fruit ', ' banana '));//key exists. But the list type returns int 2$redisSet (' Pats ', ' dog ');Var_dump($redis-Lpush (' pats ', ' cat '));//key exists. But not a list type, returns a Boolean false

2, Lpushx

Redis lpushx inserts one or more values into the existing list header, and the operation is invalid if the list does not exist.

Grammar:

Redis 127.0.0.1:6379> lpushx key_name VALUE1. Valuen

Return value: The length of the list after the LPUSHX command executes.

Available Versions: >= 2.2.0

Time complexity:O (1)

Specific examples:

<?PHP$redis=NewRedis ();$redisConnect (' 127.0.0.1 ', 6379);$redis-Flushall ();Var_dump($redisRpush (' favorite_fruit ', ' cherry ')));Var_dump($redisRpush (' favorite_fruit ', ' banana ')));Var_dump($redisLpushx (' Favorite_fruit ', ' apple '));//returns int 3Var_dump($redisLrange (' Favorite_fruit ', 0,-1));//Array (size=3)//0 = String ' apple ' (length=5)//1 = String ' cherry ' (length=6)//2 = str ing ' banana ' (length=6)Var_dump($redis-Lpushx (' Fake_key ', ' invalid_val '));//invalid operation when the list does not exist return int (0)

3, Rpush

The Redis rpush command is used to insert one or more values into the tail of the list (rightmost). If the list does not exist, an empty list is created and the Rpush operation is performed . An error is returned when the list exists but is not a list type. (Note: The Rpush command before the Redis 2.4 version only accepts a single value value).

Grammar:

Redis 127.0.0.1:6379> rpush key_name VALUE1. Valuen

Return value: The length of the list after performing the Rpush operation.

Available Versions: >= 1.0.0

Time complexity:O (1)

Specific examples:

<?PHP$redis=NewRedis ();$redisConnect (' 127.0.0.1 ', 6379);$redis-Flushall ();Var_dump($redis-Rpush (' favorite_fruit ', ' cherry '));//key does not exist, create a new list, return int 1Var_dump($redis-Rpush (' favorite_fruit ', ' banana '));//key exists. But the list type returns int 2$redisSet (' Pats ', ' dog ');Var_dump($redis-Rpush (' pats ', ' cat '));//key exists. But not a list type, returns a Boolean false

4, Rpushx

The Redis rpushx command is used to insert one or more values into the end of a list that already exists (rightmost). If the list does not exist, the operation is invalid.

Grammar:

Redis 127.0.0.1:6379> rpushx key_name VALUE1. Valuen

Return value: The length of the list after performing the rpushx operation.

Available version: >= 2.2.0

Time complexity:O (1)

Specific examples:

<?PHP$redis=NewRedis ();$redisConnect (' 127.0.0.1 ', 6379);$redis-Flushall ();Var_dump($redisLpush (' favorite_fruit ', ' cherry ')));Var_dump($redisLpush (' favorite_fruit ', ' banana ')));Var_dump($redisRpushx (' Favorite_fruit ', ' apple '));//returns int 3Var_dump($redisLrange (' Favorite_fruit ', 0,-1));//Array (size=3)//0 = String ' banana ' (length=6)//1 = String ' cherry ' (length=6)//2 = s Tring ' Apple ' (length=5)Var_dump($redis-Rpushx (' Fake_key ', ' invalid_val '));//invalid operation when the list does not exist return int (0)

5, Lpop

The Redis lpop command is used to Remove and return the first element of a list.

Grammar:

Redis 127.0.0.1:6379> lpop key_name

Return value: The first element of a list. Returns nil when the list key does not exist.

Available version:>= 1.0.0

Time complexity:O (1)

Specific examples:

<?PHP$redis=NewRedis ();$redisConnect (' 127.0.0.1 ', 6379);$redis-Flushall ();Var_dump($redisLpush (' favorite_fruit ', ' cherry ')));Var_dump($redisLpush (' favorite_fruit ', ' banana ')));Var_dump($redisLpush (' Favorite_fruit ', ' Apple '));Var_dump($redisLpop (' favorite_fruit '));//string AppleVar_dump($redisLrange (' Favorite_fruit ', 0,-1));//Array (size=2)//0 = String ' banana ' (length=6)//1 = String ' cherry ' (length=6)

The application of Redis in PHP (list 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.