search
Home类库下载Other librariesPHP+Redis: hash hash type

PHP+Redis: hash hash type

Oct 20, 2016 pm 02:04 PM

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"
    ]
]

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

We add a non-existent key-value pair

hset hash-key sub-key2 value2 // 返回 1

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,由于该键已经存在

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

hget hash-key sub-key1 // "value1"

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

hgetall hash-key

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"

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

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

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

hgetall hash-key

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

"sub-key1""value1"

PHP version demonstration

The first step is to link the redis database

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

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,元素插入成功,之前不存在该键

We insert the same key-value pair again

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

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

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

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'

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');

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)

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

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

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

$redis->hgetall('hash-key');

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

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

The reason is that sub-key1 has just been deleted


Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.