Home > 类库下载 > Other libraries > PHP+Redis: hash hash type

PHP+Redis: hash hash type

高洛峰
Release: 2016-10-20 14:04:04
Original
2699 people have browsed it

3. Hash type

Introduction

Hash is similar to a small Redis database

A hash can contain multiple key-value pairs

Each key of the hash cannot be repeated and is different. Unordered arrangement

The value can be a string or a numeric value

For a numeric value, you can perform an increment or decrement operation

Hash structure

In the structure below, user represents the key name, which can contain multiple Different key-value pairs

[
    "user":[
        "name1":"yanying1",
        "name2":"yanying2",
        "name3":"yanying3"
    ]
]
Copy after login

How-to

PHP+Redis: hash hash type

Demo (Command Line + PHP Demo)

Command Line:

First, we add a key-value pair to the hash. If successful, return 1; if the key already exists, return 0

hset hash-key sub-key1 value1 // 返回 1
Copy after login

We add a non-existent key-value pair

hset hash-key sub-key2 value2 // 返回 1
Copy after login

At this time, the keys sub-key1 and sub-key2 already exist in the hash, and then we add Add an identical key sub-key1 to the hash and see what happens:

hset hash-key sub-key1 value1 // 返回 0,由于该键已经存在
Copy after login

Below we get the value of a given key sub-key1 through hget.

hget hash-key sub-key1 // "value1"
Copy after login

Successfully obtained a value. Let’s get all the added elements to see which values ​​were just added

hgetall hash-key
Copy after login
Copy after login

The results are as follows. We found that the first result is the key of the first key-value pair, the second result is the value of the key-value pair, and so on, with every two as a group.

"sub-key1""value1""sub-key2""value2"
Copy after login

Let’s try to delete a key. We will find that if the key previously existed in the hash, then 1 will be returned when deleting it. Otherwise, 0 will be returned.

hdel hash-key sub-key1 // 该键之前存在于散列,返回1
Copy after login

Let’s try to delete sub-key1 again, It is found that when deleting a non-existent key, the return result is 0

hdel hash-key sub-key1
Copy after login

We get all the key-value pairs again to see what is left.

hgetall hash-key
Copy after login
Copy after login

The result is that there are two less results. After all, a key-value pair has just been deleted

"sub-key1""value1"
Copy after login

PHP version demonstration

The first step is to link the redis database

$redis = new Redis();$redis->connect('127.0.0.1', 6379);
Copy after login

We first insert a key-value pair into hash-key hash. 1 means the insertion is successful; 0 element means it already exists

$redis->hset('hash-key','sub-key1','value1'); // int 1,元素插入成功,之前不存在该键
Copy after login

We insert the same key-value pair again

$redis->hset('hash-key','sub-key1','value1'); // int 0,元素插入失败,该键已经存在
Copy after login

For the following demonstration, we continue to insert some other values

$redis->hset('hash-key','sub-key2','value2'); // int 0
Copy after login

Now we use hget to get the value corresponding to the key. Let's try to get the value of sub-key2.

$redis->hget('hash-key','sub-key2'); // 'value2'
Copy after login

After getting one, we try to use hgetall to get all the values ​​​​just inserted to see which key-value pairs are contained in it

$redis->hgetall('hash-key');
Copy after login
Copy after login

The result is an array containing the complete key value

array (size=2)  'sub-key1' => string 'value1' (length=6)  'sub-key2' => string 'value2' (length=6)
Copy after login

After viewing all the key values Right, let's try to delete one of the keys. If the key exists in the hash before, it returns 1. Otherwise, it returns 0

$redis->hdel('hash-key','sub-key1'); // 该键之前存在,返回int 1
Copy after login

We try to delete the sub-key1 key again and find that it returns 0. This means that the key does not exist in the set and the deletion failed

$redis->hdel('hash-key','sub-key1'); // 该键不存在,返回int 0
Copy after login

Next we Use hgetall to check all key-value pairs remaining in the hash

$redis->hgetall('hash-key');
Copy after login
Copy after login

and find that only an array containing one key-value pair is returned.

array (size=1)  'sub-key2' => string 'value2' (length=6)
Copy after login

The reason is that sub-key1 has just been deleted


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template