Table of Contents
①. Data type
③. Transactions Transactions
♬ For code usage
♬ Extension
①. Comparison of four NoSQL databases:
②. Usage scenarios of rpoplpush
Home Backend Development PHP Tutorial Some ways to use Redis in PHP

Some ways to use Redis in PHP

Mar 03, 2018 pm 01:22 PM
php redis Instructions

Redis has great advantages for program developers. Learning these key knowledges will definitely improve our abilities. This article mainly shares with you some methods of using Redis in PHP, hoping to help everyone.

①. Data type

  • First we need to understand it based on the 7 data types provided by Redis, namely String, Hash, List, Set, Sorted Set, Pub/Sub, and Transactions

②. Related understanding of single thread:

Some ways to use Redis in PHP

③. Transactions Transactions

  • We generally think that NoSQL databases do not have transactions, I am afraid we will disappoint you. Redis supports transactions, but they are not transactions in our general sense. If you execute the exec command and the power is cut off or the server hangs up on the way, we will still find that part of it is inserted in Redis and part of it is not.

  • However, Redis provides the WATCH command. We can watch a certain key and then execute Transactions. If the value of the Watch is modified, the Transactions will find out and refuse to execute.

Commonly used commands include MULTI, EXEC, and DISCARD.

1

2

3

4

5

MULTI:标记一个事务块的开始;

EXEC:执行所有事务块内的命令;

DISCARD:取消事务,放弃执行事务块内的所有命令;

UNWATCH:取消 WATCH 命令对所有 key 的监视;

WATCH key:监视 key,如果在事务执行之前 key 被其他命令所改动,那么事务将被打断

Copy after login
  • Redis As a database, many developers can also use it alone. In fact, more often than not, Redis is used as a middleware between the database and the code. If you find that there is a bottleneck in your current database, then you can optimize it through Redis

♬ For code usage

  • , you can refer to the video - [php operation redis]

  • Personal simple code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

 $redis new \Redis(); $redis->pconnect('192.168.236.131''6379'); #连接redis服务

 $redis->set('say','moTzxx say hello !');

 //echo $redis-&gt;get('say'); $redis-&gt;delete('myname'); $redis-&gt;set('myname','moTzxx'); echo $redis-&gt;get('myname').'<br>'; # 返回:moTzxx

 

 var_dump($redis-&gt;del('myname'));# 返回 TRUE(1)

 var_dump($redis-&gt;get('myname')); # 返回 bool(false)

 

 //$redis-&gt;set('tag',10); $redis-&gt;delete('tag'); $redis-&gt;incr('tag');

 //$redis-&gt;incrBy('tag',3);

 var_dump($redis-&gt;get('tag')); echo '<hr>'$redis-&gt;delete('list1'); $redis-&gt;lPush('list1','hello'); $redis-&gt;rPush('list1','miss');

 var_dump($redis-&gt;rPop('list1')); #string(4) "miss"

 echo '<hr>'$redis-&gt;delete('set1'); $redis-&gt;sAdd('set1','a'); $redis-&gt;sAdd('set1',2); $redis-&gt;sAdd('set1','ok'); $redis-&gt;sAdd('set1','ok');//TODO 集合的唯一性 $val = $redis-&gt;sCard('set1');

 var_dump($val); #int(3)

 $val2 $redis-&gt;sMembers('set1');

 var_dump($val2);#array(3) { [0]=&gt; string(2) "ok" [1]=&gt; string(1) "2" [2]=&gt; string(1) "a" }

 echo '<br>'$redis-&gt;set('key1''value', 111);

 var_dump($redis-&gt;get('key1')); $redis-&gt;getSet('key1','value11');

 var_dump($redis-&gt;mget(['tag','key1']));

 var_dump($redis-&gt;type('tag')); echo '<hr>'$redis-&gt;delete('driver1'); $redis-&gt;hSet("driver1",'name','moTzxx'); $redis-&gt;hSet("driver1",'age',12); $redis-&gt;hSet('driver1','gender',1); $val $redis-&gt;hGet("driver1",'name');

 var_dump($val);#string(6) "moTzxx"

 $val $redis-&gt;hMGet("driver1",['name','gender']);

 var_dump($val); #array(2) { ["name"]=&gt; string(6) "moTzxx" ["gender"]=&gt; string(1) "1" }

 echo '<hr>'$redis-&gt;delete('pats'); $redis-&gt;zAdd('pats',1,'cat1');  # 构建一个zset (sorted set) // int(1)

 $redis-&gt;zAdd('pats',3,'dog3'); $redis-&gt;zAdd('pats',2,'monkey2'); $redis-&gt;zAdd('pats',2,'pig2'); $val $redis-&gt;zRange('pats',0,-1); #从低到高

 var_dump($val); //array(4) { [0]=&gt; string(4) "cat1" [1]=&gt; string(7) "monkey2" [2]=&gt; string(4) "pig2" [3]=&gt; string(4) "dog3" } $val2 = $redis-&gt;zRevRange('pats',0,-1); #从高到低

 var_dump($val2);//array(4) { [0]=&gt; string(4) "dog3" [1]=&gt; string(4) "pig2" [2]=&gt; string(7) "monkey2" [3]=&gt; string(4) "cat1" }

 var_dump($redis-&gt;TYPE('pats')); //zset / int(4)

Copy after login
  • Execution effect screenshot:

♬ Extension

①. Comparison of four NoSQL databases:

②. Usage scenarios of rpoplpush

##Related recommendations:

How to operate Redis with PHP Example sharing

PHP installation redis extension example under Windows

PHP using Redis example explanation

The above is the detailed content of Some ways to use Redis in PHP. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles