Home Backend Development PHP Tutorial php与redis施用经验分享

php与redis施用经验分享

Jun 13, 2016 pm 01:15 PM
hash max php redis

php与redis使用经验分享
一、安装
1、redis的下载及安装:

引用

mkdir /usr/local/redis
cd /usr/local/redis
wget http://redis.googlecode.com/files/redis-2.4.2.tar.gz
tar xzf redis-2.4.2.tar.gz
cd redis-2.4.2
make
./src/redis-server

2、redis测试命令:
src/redis-cli
redis> set foo bar
OK
redis> get foo
“bar”;
Copy after login

3、接下来需要进行redis配置:
引用

daemonize yes
pidfile /usr/local/redis/var/redis.pid
port 6379
timeout 300
loglevel verbose
logfile /usr/local/redis/var/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /usr/local/redis/var
slave-serve-stale-data yes
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
vm-enabled no
vm-swap-file /tmp/redis.swap
vm-max-memory 0
vm-page-size 32
vm-pages 134217728
vm-max-threads 4
hash-max-zipmap-entries 512
hash-max-zipmap-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
activerehashing yes

4、Redis中文配置文档:
引用

1,是否以后台进程运行,默认为no,生产环境中肯定要设置成yes,并设置为自动启动
daemonize no
2,如以后台进程运行,则需指定一个pid,默认为/var/run/redis.pid
pidfile /var/run/redis.pid
3,监听端口,默认为6379
port 6379
4,绑定主机IP,默认值为127.0.0.1(注释),生产环境中一般是内外网的IP
bind 127.0.0.1
5,超时时间,默认为300(秒)
timeout 300
6,日志记录等级,有4个可选值,debug,verbose(默认值),notice,warning
loglevel verbose
7,日志记录方式,默认值为stdout
logfile stdout
8,可用数据库数,默认值为16,默认数据库为0
databases 16
9,指出在多长时间内,有多少次更新操作,就将数据同步到数据文件。这个可以多个条件配合,比如默认配置文件中的设置,就设置了三个条件。
900秒(15分钟)内至少有1个key被改变
save 900 1
300秒(5分钟)内至少有10个key被改变
save 300 10
10,存储至本地数据库时是否压缩数据,默认为yes
rdbcompression yes
11,本地数据库文件名,默认值为dump.rdb
dbfilename /root/redis_db/dump.rdb
12,本地数据库存放路径,默认值为 ./
dir /root/redis_db/
13,当本机为从服务时,设置主服务的IP及端口(注释)
slaveof
14,当本机为从服务时,设置主服务的连接密码(注释)
masterauth
15,连接密码(注释)
requirepass foobared
16,最大客户端连接数,默认不限制(注释)
maxclients 128
17,设置最大内存,达到最大内存设置后,Redis会先尝试清除已到期或即将到期的Key,当此方法处理后,任到达最大内存设置,将无法再进行写入操作。(注释)
maxmemory
18,是否在每次更新操作后进行日志记录,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认值为no
appendonly yes
19,更新日志文件名,默认值为appendonly.aof(注释)
appendfilename /root/redis_db/appendonly.aof
20,更新日志条件,共有3个可选值。no表示等操作系统进行数据缓存同步到磁盘,always表示每次更新操作后手动调用fsync()将数据写到磁盘,everysec表示每秒同步一次(默认值)。
appendfsync everysec
21,是否使用虚拟内存,默认值为no
vm-enabled yes
22,虚拟内存文件路径,默认值为/tmp/redis.swap,不可多个Redis实例共享
vm-swap-file /tmp/redis.swap
23,将所有大于vm-max-memory的数据存入虚拟内存,无论vm-max-memory设置多小,所有索引数据都是内存存储的 (Redis的索引数据就是keys),也就是说,当vm-max-memory设置为0的时候,其实是所有value都存在于磁盘。默认值为0。
vm-max-memory 0
24,虚拟内存文件以块存储,每块32bytes
vm-page-size 32
25,虚拟内在文件的最大数
vm-pages 134217728
26,可以设置访问swap文件的线程数,设置最好不要超过机器的核数,如果设置为0,那么所有对swap文件的操作都是串行的.可能会造成比较长时间的延迟,但是对数据完整性有很好的保证.
vm-max-threads 4
27,把小的输出缓存放在一起,以便能够在一个TCP packet中为客户端发送多个响应,具体原理和真实效果我不是很清楚。所以根据注释,你不是很确定的时候就设置成yes
glueoutputbuf yes
28,在redis 2.0中引入了hash数据结构。当hash中包含超过指定元素个数并且最大的元素没有超过临界时,hash将以一种特殊的编码方式(大大减少内存使用)来存储,这里可以设置这两个临界值
hash-max-zipmap-entries 64
29,hash中一个元素的最大值
hash-max-zipmap-value 512
30,开启之后,redis将在每100毫秒时使用1毫秒的CPU时间来对redis的hash表进行重新hash,可以降低内存的使用。当你的使 用场景中,有非常严格的实时性需要,不能够接受Redis时不时的对请求有2毫秒的延迟的话,把这项配置为no。如果没有这么严格的实时性要求,可以设置 为yes,以便能够尽可能快的释放内存
activerehashing yes

5、php-Redis模块安装:
如果你的机器上没有phpize,则需要先安装php-devel这个包
sudo apt-get install php-devel
Copy after login

引用

cd /usr/local/src
wget --no-check-certificate http://github.com/owlient/phpredis/tarball/master -O phpredis.tar.gz
tar zxvf phpredis.tar.gz
cd phpredis*
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
sudo make install

安装完成以后重启web服务
6、Redis的界面管理工具
一个PHP版的Redis WEB可视化管理软件
这里可以下载https://github.com/ErikDubbelboer/phpRedisAdmin/downloads
放到web目录里既可以访问
7、php代码的使用
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$redis->set('key', 'val');

echo $redis->get('key');
Copy after login
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

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 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

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

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Top 10 PHP CMS Platforms For Developers in 2024 Top 10 PHP CMS Platforms For Developers in 2024 Dec 05, 2024 am 10:29 AM

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

How to Add Elements to the End of an Array in PHP How to Add Elements to the End of an Array in PHP Feb 07, 2025 am 11:17 AM

Arrays are linear data structures used to process data in programming. Sometimes when we are processing arrays we need to add new elements to the existing array. In this article, we will discuss several ways to add elements to the end of an array in PHP, with code examples, output, and time and space complexity analysis for each method. Here are the different ways to add elements to an array: Use square brackets [] In PHP, the way to add elements to the end of an array is to use square brackets []. This syntax only works in cases where we want to add only a single element. The following is the syntax: $array[] = value; Example

Which country is the Nexo exchange from? Where is it? A comprehensive introduction to the Nexo exchange Which country is the Nexo exchange from? Where is it? A comprehensive introduction to the Nexo exchange Mar 05, 2025 pm 05:09 PM

Nexo Exchange: Swiss cryptocurrency lending platform In-depth analysis Nexo is a platform that provides cryptocurrency lending services, supporting the mortgage and lending of more than 40 crypto assets, fiat currencies and stablecoins. It dominates the European and American markets and is committed to improving the efficiency, security and compliance of the platform. Many investors want to know where the Nexo exchange is registered, and the answer is: Switzerland. Nexo was founded in 2018 by Swiss fintech company Credissimo. Nexo Exchange Geographical Location and Regulation: Nexo is headquartered in Zug, Switzerland, a well-known cryptocurrency-friendly region. The platform actively cooperates with the supervision of various governments and has been in the US Financial Crime Law Enforcement Network (FinCEN) and Canadian Finance

See all articles