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

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.