在PHP中使用redis
这里是在Mac OS上安装redis,并在PHP中开启redis。
在Mac OS上安装redis
首先是安装,,它会默认安装到/usr/local/bin下
复制代码 代码如下:
cd /tmp
wget
tar -zxf redis-2.6.9.tar.gz
cd redis-2.6.9
make
sudo make install
然后下载一些配置文件(主要就是把deamon打开之类的,没对比与默认配置的区别)
复制代码 代码如下:
wget https://github.com/ijonas/dotfiles/raw/master/etc/redis.conf
sudo mv redis.conf /etc/redis.conf
sudo /usr/local/bin/redis-server redis.conf
说到这里备注下,如果没有目录权限,是无法建立
/var/log/redis/redis.log
/var/lib/redis/
导致redis启动失败
ok,现在已经大功告成,你的redis已经成功运行起来了。
试试看吧!
复制代码 代码如下:
/opt/redis/redis-cli
#会看到提示 redis 127.0.0.1:6379>说明已经连接服务。
set anythink helloworld
get anythink
exit
good 看到了helloworld,说明一切正常。
如果我需要停止redis或者需要重新启动呢?
复制代码 代码如下:
cat /opt/redis/redis.pid
#cat后会得到一个pid,我的是44277
sudo kill 44277
# 启动方法和之前一样。
设置开机自启动、后台运行
然后以root身份做以下事情:
在/Library/LaunchDaemons下新建com.redis.plist,内容如下:
复制代码 代码如下:
之后运行
复制代码 代码如下:
sudo launchtcl load /Library/LaunchDaemons/com.redis.plist
sudo launchtcl start com.redis
检查一下情况:
复制代码 代码如下:
$ cat /var/run/redis.pid
如果出来pid的数字,说明就运行了~
安装php-redis扩展
如果你需要在PHP中使用redis,那么请继续往下看
复制代码 代码如下:
curl -O https://nodeload.github.com/nicolasff/phpredis/zip/master
tar -zxf master
cd phpredis-master/
phpize
./configure
make
sudo make install
# 这时候会提示一个路径
# /usr/lib/php/extensions/no-debug-non-zts-20090626/
# 表示已经将扩展放置在该位置
vim /etc/php.ini
#增加如下内容
extension=redis.so
#重启apache
sudo httpd -k restart
#查看扩展安装情况
php -m |grep redis
#出现 redis 表示安装成功。
如果执行phpize提示如下错误
Cannot find autoconf. Please check your autoconf installation
and the $PHP_AUTOCONF environment variable.
Then, rerun this script.
请分别下载M4,autoconf编译安装
复制代码 代码如下:
curl -O
curl -O
注意,以上使用的apache、php均为MacOS自带的,如果是自己安装的phpize请指定绝对路径。
图形管理工具
另:redis还有一个基于WEB的图形界面管理工具,叫phpRedisAdmin,如果刚开启服务会出现一些Undefined index,过一会就好了。如果想试试可以使用如下命令安装(git推荐使用SourceTree安装)该管理工具支持String、Hash、List、Set、Zset
复制代码 代码如下:
git clone https://github.com/ErikDubbelboer/phpRedisAdmin.git
cd phpRedisAdmin/
git clone https://github.com/nrk/predis.git

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

Redis uses hash tables to store data and supports data structures such as strings, lists, hash tables, collections and ordered collections. Redis persists data through snapshots (RDB) and append write-only (AOF) mechanisms. Redis uses master-slave replication to improve data availability. Redis uses a single-threaded event loop to handle connections and commands to ensure data atomicity and consistency. Redis sets the expiration time for the key and uses the lazy delete mechanism to delete the expiration key.

Steps to solve the problem that redis-server cannot find: Check the installation to make sure Redis is installed correctly; set the environment variables REDIS_HOST and REDIS_PORT; start the Redis server redis-server; check whether the server is running redis-cli ping.

To view all keys in Redis, there are three ways: use the KEYS command to return all keys that match the specified pattern; use the SCAN command to iterate over the keys and return a set of keys; use the INFO command to get the total number of keys.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.
