Home php教程 php手册 Redis系列-php怎么通过redis扩展使用redis

Redis系列-php怎么通过redis扩展使用redis

Jun 13, 2016 am 10:55 AM
php phpredis r redis use Install how Expand series pass

 

 

 

 

1、安装php扩展

 

a)安装php扩展phpredis:

 

 

[plain] 

[root@xsf002 tool]# git clone https://github.com/nicolasff/phpredis.git phpredis  

[root@xsf002 tool]# cd phpredis/  

[root@xsf002 phpredis]# /usr/local/php/bin/phpize   #假设 php目录:/usr/local/php  

[root@xsf002 phpredis]# ./configure --with-php-config=/usr/local/php/bin/php-config  

[root@xsf002 phpredis]# make  

[root@xsf002 phpredis]# make install  

 

[root@xsf002 tool]# git clone https://github.com/nicolasff/phpredis.git phpredis

[root@xsf002 tool]# cd phpredis/

[root@xsf002 phpredis]# /usr/local/php/bin/phpize   #假设 php目录:/usr/local/php

[root@xsf002 phpredis]# ./configure --with-php-config=/usr/local/php/bin/php-config

[root@xsf002 phpredis]# make

[root@xsf002 phpredis]# make install 如果顺利,将得到类似如下提示: 

 

 

[plain] 

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/  

 

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/ php扩展文件目录下,将有个redis.so文件。 

 

b)加载redis.so

 

在php.ini 文件中,添加配置:

 

 

[plain] 

[redis]  

extension=redis.so  

 

[redis]

extension=redis.so重启web服务器,echo phpinfo() ,将看到大致如下显示:

 

 

 

 2、通过扩展访问redis服务器

 

php如何使用redis服务器存取数据?无非是经过phpredis扩展模块,通过如下几个步骤:

 

1、实例化redis类   【new redis()】

 

2、调用redis类方法,连接redis服务器 【connect,pconnect】

 

3、设置连接选项【setOption】

 

4、存/取数据 

 

5、关闭连接 【close】

 

如果使用默认选项,步骤3不需要。请参看下面的代码示例:  

 

 

[php] 

$redis = new Redis(); #实例化redis类  

$redis->connect('127.0.0.1'); #连接服务器  

$redis->set('key', 'hello '); #调用方法,设置string类型值  

$redis->append('key', 'world'); #修改string类型值  

echo $redis->get('key');  #获取redis key的值,并输出显示   

echo $redis->type('key'); #获取key 的数据类型  

echo $redis->echo('will close...');# 输出字符串  

$redis->close(); #关闭连接  

 

$redis = new Redis(); #实例化redis类

$redis->connect('127.0.0.1'); #连接服务器

$redis->set('key', 'hello '); #调用方法,设置string类型值

$redis->append('key', 'world'); #修改string类型值

echo $redis->get('key');  #获取redis key的值,并输出显示 

echo $redis->type('key'); #获取key 的数据类型

echo $redis->echo('will close...');# 输出字符串

$redis->close(); #关闭连接  通过上面的代码,我们基本完成一个简单redis的存取操作。下面罗列一些Redis类的一些属性及方法 

 

a)连接redis server:

 

 

connect :连接server

pconnect :长连接

auth :权限验证

select :选择DB

close : 关闭连接

  setOption : 设置 client 选项

getOption : 获取client选项

ping : ping redis server

 echo : 输出 字符串

 

注意,如果频繁操作redis,不停地connect 和close会很耗性能的,这个时候,建议用pconnect 建立个长连接

 

b)字符串读写函数

 

 

append  :在值的后面追加值

decr :递减一个key的值

incr :递增一个key的值

 

get :获取一个值

set :设置一个值

getSet :设置值,并返回老值

mGet :批量获取值

mSet :批量设置值

strlen :获取值长度

 

注意:如果能用批量操作尽量用批量,减少频繁连接redis数据库性能

 

c)hash读写函数

 

 

hDel :删除一个多个域

hExists :判断一个hash域是否存在

hGet :获取hash域的值

hGetAll :获取所有域值

hIncrBy :自增长一个hash int域的值

hKeys :获取hash 所有域

hLen :获取域个数

hMGet :批量获取域的值

hMSet :批量设置域的值

hSet :设置域的值

hVals:得到所有域的值

 

d)list读写函数

 

 

lInsert:插入元素

lLen:list长度

lPop:移除并获取第一个颜色

lPush:插入一个元素

lRem:移除元素

lSet:设置元素值

 

e)set

 

 

sAdd:增加一个或多个成员

sIsMember:是否包含

sMembers:得到成员

sMove:移动成员

sPop:移除成员

sRandMember:得到随机成员

sRem:删除

 

f)sorted set

 

 

zAdd:增加一个或多个

zCard:成员个数

zIncrBy:递增成员score

zRange:返回索引范围内的成员

zRangeByScore :返回score范围内的成员

zScore:获取成员score

zRem:移除一个或多个成员

 

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 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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? Apr 19, 2025 pm 08:03 PM

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

Redis's Role: Exploring the Data Storage and Management Capabilities Redis's Role: Exploring the Data Storage and Management Capabilities Apr 22, 2025 am 12:10 AM

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Why is the return value empty when using RedisTemplate for batch query? Why is the return value empty when using RedisTemplate for batch query? Apr 19, 2025 pm 10:15 PM

Why is the return value empty when using RedisTemplate for batch query? When using RedisTemplate for batch query operations, you may encounter the returned results...

Redis: Understanding Its Architecture and Purpose Redis: Understanding Its Architecture and Purpose Apr 26, 2025 am 12:11 AM

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

Why does the redisTemplate.opsForList().leftPop() method not support passing in parameters to pop up multiple values ​​at once? Why does the redisTemplate.opsForList().leftPop() method not support passing in parameters to pop up multiple values ​​at once? Apr 19, 2025 pm 10:27 PM

Regarding the reason why RedisTemplate.opsForList().leftPop() does not support passing numbers. When using Redis, many developers will encounter a problem: Why redisTempl...

See all articles