


php memory cache implementation method, php memory cache
The example in this article describes the implementation method of php memory cache. Share it with everyone for your reference. The details are as follows:
Caching in php is divided into many types, such as memory cache, file cache, and page cache. This article is going to talk about some methods of memory caching in php. Here we will introduce the Memcached cache and the APC caching method that comes with php.
1.Memcached cache.
Memcached is a high-performance distributed memory cache server. It caches database query results and reduces the number of database accesses to improve the speed of dynamic web applications. Memcached uses the "Key=>Value" method to organize data, which allows different Multiple users on the host access this cache system at the same time. It is generally used for large websites. Memcached uses memory to cache data, so it is volatile. When the server is restarted or the memcached process is terminated, the data will be lost, so memcached cannot be used. to persist data.
Anyone who has used php_memcache will think that PHP memory cache is a very complicated thing. In fact, memcached is an efficient and fast distributed memory object caching system, mainly used to accelerate WEB dynamic applications.
Here we introduce the configuration and use of memcached under WIN32.
1. Configuration of PHP memory cache, WIN32 environment
1. Download php_memcache.rar
Unzip the compressed package: php_memcache.rar
The main files included in the php_memcache.rar compressed package are:
/memcached-1.2.1-win32/memcached.exe
/php_memcache/php_memcache.dll
2. Open the command prompt, point to the path where memcached.exe is located, and run memcached.exe -d start.
3. Copy the php_memcache.dll file to the folder of PHP’s dynamic file library.
4. Add a line extension=php_memcache.dll to the php.ini file.
5. Restart Apache, and then check phpinfo. If there is memcache, it means the installation is successful!
For example, the code is as follows:
require_once('memcached-client.php');
//Option settings
$options = array(
'servers' => array('www.jb51.net:11211'),//The address and port of the memcached service
'debug' => true,//Whether to turn on debug
'compress_threshold' => 10240, //Compress when the data exceeds the number of bytes
'persistant' => false//Whether to use persistent connections
);
//Instantiate memcached object
$memcached = new memcached($options);
$sql = 'SELECT * FROM table1';
$key = md5($sql);
//If there is no cached data in memcached, write the cached data to memcached
if(!($datas = $memcached->get($key)))
{
$conn = mysql_connect('localhost', 'hxsd', '123456');
mysql_select_db('hxsd');
$result = mysql_query($sql);
while($row = mysql_fetch_object($result))
{
$datas[] = $row;
}
//Save the result set data obtained from the database to memcached for use during the next access.
$memcached->add($key, $datas);
}
else
{
//Directly use cached data in memcached $datas
}
?>
Memory cache 2, comparison of APC, EC and Zend accelerator
1. APC
APC, the full name is Alternative PHP Cache, the official translation is called "Optional PHP Cache", the homepage is http://pecl.php.net/package/apc, the php help manual page: http://cn.php. net/apc
APC is an optimizer. From the day of installation, it has been silently serving your PHP application in the background. All your PHP codes will be cached for php opcode.
In addition, APC can provide a certain memory cache function. However, this function is not perfect. There are reports that frequent use of the APC cache write function will lead to unpredictable errors. If you want to use this function, you can see Look at apc_fetch, apc_store and several other functions related to apc cache.
Installation, the code is as follows:
Configuration:/etc/php.inc, the code is as follows:
[apc]
apc.shm_segments = 1
apc.shm_size = 30
apc.optimization = 0
apc.ttl = 7200
apc.user_ttl = 7200
apc.num_files_hint = 1000
apc.mmap_file_mask = /tmp/apc.XXXXXX
I hope this article will be helpful to everyone’s PHP programming design.

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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

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.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
