PHP之Opcode缓存 AND Memcache缓存使用引导篇
PHP生命周期
请求--->.php--->词典扫描--->解析--->创建Opcode--->处理Opcode--->响应
即使该PHP脚本的内容没有任何变化,Zend引擎也必须重新创建该文件的Opcode.
Opcode缓存提高PHP性能
--->有缓存--->读取已缓存的Opcode--->处理Opcode--->响应
请求--->.php
--->无缓存--->词典扫描--->解析--->创建Opcode--->处理Opcode--->响应
Opcode缓存工具
APC
具体可参考PHP之APC缓存详细介绍(学习整理)
XCache
具体可参看PHP之XCache缓存使用
eACCelerator
内存缓存Memcache
简介
Memcache函数库是在PECL(PHP Extension Community Library)中,主要作用是搭建大容量的内存数据的临时存放区域
memcache也提供用于通信对话(session_handler)的处理。
更多Memcache 模块相关信息可以到 http://www.danga.com/memcached/ 查阅。
安装Memcache
可参看:http://blog.csdn.net/initphp/article/details/8039917 liunx下的Memcache安装和使用
Memcache Functions 函数列表
列表:
参考http://www.php.net/manual/zh/function.Memcache-add.php Memcache::add - 添加一个值,如果已经存在,则返回false Memcache::addServer - 添加一个可供使用的服务器地址 Memcache::close - 关闭一个Memcache对象 Memcache::connect - 创建一个Memcache对象 memcache_debug - 控制调试功能 Memcache::decrement - 对保存的某个key中的值进行减法操作 Memcache::delete - 删除一个key值 Memcache::flush - 清除所有缓存的数据 Memcache::get - 获取一个key值 Memcache::getExtendedStats - 获取进程池中所有进程的运行系统统计 Memcache::getServerStatus - 获取运行服务器的参数 Memcache::getStats - 返回服务器的一些运行统计信息 Memcache::getVersion - 返回运行的Memcache的版本信息 Memcache::increment - 对保存的某个key中的值进行加法操作 Memcache::pconnect - 创建一个Memcache的持久连接对象 Memcache::replace -对一个已有的key进行覆写操作 Memcache::set - 添加一个值,如果已经存在,则覆写 Memcache::setCompressThreshold - 对大于某一大小的数据进行压缩 Memcache::setServerParams - 在运行时修改服务器的参数 |
1、向对象添加一个服务器(注:addServer没有连接到服务器的动作,所以在memcache进程没有启动的时候,执行addServer成功也会返回true)
//参数 //@param string $host 服务器域名或IP //@param int $port 端口号,默认值11211 //@param bool $persistent 是否使用常链接,默认TURE //@param int $weight 权重,在多个服务器设置中占的比重 //@param int $timeout 连接服务器失效的秒数,修改默认值 1 时要三思,有可能失去所有缓存方面的优势导致连接变得很慢 //@param int $retry_interval 服务器连接失败时的重试频率,默认是 15 秒一次,如果设置为 -1 将禁止自动重试,当扩展中加载了 dynamically via dl() 时,无论本参数还是常连接设置参数都会失效。 每一个失败的服务器在失效前都有独自的生存期,选择后端请求时会被跳过而不服务于请求。一个过期的连接将成功的重新连接或者被标记为失败的连接等待下一次 重试。这种效果就是说每一个 web server 的子进程在服务于页面时的重试连接都跟他们自己的重试频率有关。 //@param bool $status 控制服务器是否被标记为 online,设置这个参数为 FALSE 并设置 retry_interval 为 -1 可以使连接失败的服务器被放到一个描述不响应请求的服务器池子中,对这个服务器的请求将失败,接受设置为失败服务器的设置,默认参数为 TRUE,代表该服务器可以被定义为 online。 //@param callback $failure_callback 失败时的回调函数,函数的两个参数为失败服务器的 hostname 和 port bool Memcache::addServer ( string $host [, int $port [, bool $persistent [, int $weight [, int $timeout [, int $retry_interval [, bool $status [, callback $failure_callback ]]]]]]] )
2、连接memcache服务器
//参数 //@param string $host 服务器域名或ip //@param int $post 服务器tcp端口号,默认值是11211 //@param $timeout 连接memcache进程的失效时间,在修改它的默认值1的时候要三思,以免失去所有memcache缓存的优势导致连接变得很慢 //@return bool bool Memcache::connect ( string $host [, int $port [, int $timeout ]] )案例:
/* procedural API */ $memcache_obj = memcache_connect(‘memcache_host‘, 11211); /* OO API */ $memcache = new Memcache; $memcache->connect(‘memcache_host‘, 11211);
3、以常连接方式连接服务器
bool Memcache::pconnect ( string $host [, int $port [, int $timeout ]] )
4、关闭对象 (对常连接不起作用)
bool Memcache::close ( void )案例:
/* procedural API */ $memcache_obj = memcache_connect(‘memcache_host‘, 11211); /* do something here .. */ memcache_close($memcache_obj); /* OO API */ $memcache_obj = new Memcache; $memcache_obj->connect(‘memcache_host‘, 11211); /* do something here .. */ $memcache_obj->close();
//参数 //@param string $key 缓存数据的键 其长度不能超过250个字符 //@param mixed $var 值,整型将直接存储,其他类型将被序列化存储,其值最大1M //@param int $flag 是否使用zlib压缩,当flag=MEMCACHE_COMPRESSED的时候,数据很小的时候不会采用zlib压缩,只有数据达到一定大小才对数据进行压缩 //@param int $expire 过期时间,0为永久不过期,可使用 unix 时间截格式或距离当前时间的秒数,设为秒数时不能大于2592000(30天) //@return 成功返回 TRUE,失败返回 FALSE,如果这个键已经存在,其他方面memcache:;add()的行为与memcache::set相似 bool Memcache::add ( string $key , mixed $var [, int $flag [, int $expire ]] )
案例:
$memcache_obj = memcache_connect("localhost", 11211); memcache_add($memcache_obj, 'var_key', 'test variable', false, 30); $memcache_obj->add('var_key', 'test variable', false, 30);
6、对一个已有的key进行覆写操作
//参数 //@param string $key 缓存数据的键 //@param mixed $var 值,整型将直接存储,其他类型将被序列化存储,其值最大为1M //@param int $flag 是否使用 zlib 压缩 ,当flag=MEMCACHE_COMPRESSED的时侯,数据很小的时候不会采用zlib压缩,只有数据达到一定大小才对数据进行zlib压缩。(没有具体的测试数据进行压缩的最小值是多少) //@param int $expire 过期时间,0 为永不过期,可使用 unix 时间戳格式或距离当前时间的秒数,设为秒数时不能大于 2592000(30 天) bool Memcache::replace ( string $key , mixed $var [, int $flag [, int $expire ]] )
案例:
$memcache_obj = memcache_connect("localhost", 11211); /* procedural API */ memcache_replace($memcache_obj, "test_key", "some variable", FALSE, 30); /* OO API */ $memcache_obj->replace("test_key", "some variable", FALSE, 30);
7、添加一个值,如果已经存在,则覆写
//参数 //@param string $key 缓存数据的键, 其长度不能超过250个字符 //@param mixed $var //@param int $flag //@param int $expire bool Memcache::set ( string $key , mixed $var [, int $flag [, int $expire ]] )
案例:
$memcache_obj = memcache_connect("localhost", 11211); /* set value of item with key ‘var_key‘ using 0 as flag value, compression is not used expire time is 30 second */ memcache_set($memcache_obj, ‘var_key‘, ‘some variable‘, 0, 30); echo memcache_get($memcache_obj, ‘var_key‘);

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

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

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment