如今互联网崛起的时代,各大网站都面临着一个大数据流问题,怎么提高网站访问速度,减少对数据库的操作;作为PHP开发人员,我们一般能想到的方法有页面静态化处理、防盗链、CDN内容分发加速访问、mysql数据库优化建立索引、架设apache服务器集群()、还有就是现在流行的各种分布式缓存技术:如memcached/redis;
1.什么是Memcached?
a.Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。
b.Memcached的键key一般是字符串,该值不能重复;value可以放入字符串、数组、数值、对象、布尔,二进制数据和图片视频
c.Memcached默认服务端口是11211
2.PHP使用Memcached步骤
准备:下载Memcached服务安装包:memcached-1.2.6-win32-bin.7z和访问Memcached服务的dll库:php_memcache.dll
www.memcached.org(官网进不去好像,可以从其他地方下载)
解压包memcached-1.2.6-win32-bin.7z(可以解压完复制放到web服务器同级目录),然后操作cmd,进入到刚才解压的目录用命令安装:memcached.exe -d install
安装完(判断是否安装完毕可以到服务列表里面查看是否有memcached服务),然后cmd用命令启动:memcached.exe -d start
具体操作如下:
启动完memcached服务后,再把下载的php_memcache.dll放到web服务器php5目录下的ext目录下
在php.ini里面修改,加载扩展库php_memcache.dll,然后重启apache服务器
开始实践,memcached主要有crud操作(即创建、读取、更新、删除值操作,具体可以查阅手册),下面弄个简单的设置值,然后读取值的操作
a.设置值页面
<?phpheader ("Content-type:text/html;charset=utf-8");//创建Memcache对象$mem = new Memcache(); //连接Memcache服务器if(!$mem->connect("127.0.0.1")) { echo "连接Memcache服务器失败!";}//设置,'myword'参数代表键key,'hello world'代表存放的值,MEMCACHE_COMPRESSED代表压缩内容,50代表存放时间,单位秒if ($mem->set('myword','hello world',MEMCACHE_COMPRESSED,50)){ echo "设置值成功!";}?>
b.读取值页面
<?phpheader ("Content-type:text/html;charset=utf-8");$mem = new Memcache(); if(!$mem->connect("127.0.0.1")) { echo "连接Memcache服务器失败!";}//读取键myword值$value = $mem->get('myword');if(!$value){ echo "读取失败!";}else{ echo "读取的值=".$value;}
c.删除、更新例子:
<?phpheader ("Content-type:text/html;charset=utf-8");//创建Memcache对象$mem = new Memcache(); //连接Memcache服务器if(!$mem->connect("127.0.0.1")) { echo "连接Memcache服务器失败!";}//设置,'myword'参数代表键key,'hello world'代表存放的值,MEMCACHE_COMPRESSED代表压缩内容,50代表存放时间,单位秒if ($mem->set('myword','hello world',MEMCACHE_COMPRESSED,50)){ echo "设置值成功!";}//读取键myword值$value = $mem->get('myword');if(!$value){ echo "读取失败!";}else{ echo "读取的值=".$value;}//更新键值$mem->replace('myword','hello everybody!');$value = $mem->get('myword');if(!$value){ echo "读取失败!";}else{ echo "读取的值=".$value;}//删除键myword值$mem->delete('myword');$value = $mem->get('myword');if(!$value){ echo "读取失败!";}else{ echo "读取的值=".$value;}//关闭$mem->close(); ?>
注:mem对象下还有许多方法,可以通过翻阅手册了解。
多个memcached服务器设置,其实就比一个memcached服务器改变一点点,就是把多个memcached的服务器通过方法addserver添加到连接池中,这样设置完后,crud操作时,内部就会通过相应算法均衡连接相应服务器并执行相应操作中。
<?phpheader ("Content-type:text/html;charset=utf-8");//创建Memcache对象$mem = new Memcache(); //添加多台memcached服务器$mem->addserver('192.168.0.1',11211); $mem->addserver('192.168.0.2',11211);$mem->addserver('192.168.0.3',11211);$mem->addserver('192.168.0.4',11211);//设置,'myword'参数代表键key,'hello world'代表存放的值,MEMCACHE_COMPRESSED代表压缩内容,50代表存放时间,单位秒if ($mem->set('myword','hello world',MEMCACHE_COMPRESSED,50)){ echo "设置值成功!";}//读取键myword值$value = $mem->get('myword');if(!$value){ echo "读取失败!";}else{ echo "读取的值=".$value;}?>
memcache的访问是无用户状态,安全性需要考虑,一般通过放在内网,并通过防火墙限制外网访问memcache端口来达到安全
通过修改php.ini,可以把session的值放入memcache服务器中
session.save_handler = files改成session.save_handler = memcached
session.save_path = "N;MODE;/path"改成 session.save_path = "tcp://127.0.0.1:11211"

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools