php apc缓存_如何在PHP中使用APC缓存

本文介绍了PHP中如何使用APC缓存进行性能优化。通过一个实用的类`apc.caching.php`,展示了如何存储和读取数据,包括数组和类对象。示例文件`index.php`、`index2.php`、`index3.php`分别演示了数据存储、读取和删除操作,从而利用服务器内存提高网站性能。
摘要由CSDN通过智能技术生成

php apc缓存

APC caching with PHP
APC caching with PHP

APC caching with PHP PHP APC tutorial. Today I have another interesting article for PHP. We will talking about caching, and practice of using caching in php. I will make review of APC caching and will show you how you can use APC in PHP. We will prepare useful class for working with APC for us and several examples. A little of history: Long ago, when computer networks and Web sites only evolved, most web sites were static (and all data/files stored at disks). After, people invented a ‘database’ to store the data they. This was great innovation, was comfortably hold data in database, not in the files as before. We started saving into database more and more, even small pieces of data. Years passed, the size of sites grew, and people began to notice that the performance of sites falls under the too frequent reading data from database. And we came up with new optimization way – caching (where we store data in the cache files on the server). Interesting, isn’t it? This looks like ‘Forward, to the past’ :-) But why it happened? It’s simple – the speed of hard drives has increased, even outgrown the velocity of the databases. At the moment I am talking about MySQL. Possible another types of databases still faster. But in any case, progress is not standing still. Now people have learned to use the server memory for data storage. RAM is much faster than hard disk, and the price of memory falls all the time, so let’s use all these advantages of this.

使用PHP进行APC缓存 PHP APC教程。 今天,我还有另一篇关于PHP的有趣文章。 我们将讨论缓存以及在php中使用缓存的实践。 我将回顾APC缓存,并向您展示如何在PHP中使用APC 。 我们将为我们准备一些有用的课程来与APC合作,并提供一些示例。 一段历史:很久以前,当计算机网络和网站仅在发展时,大多数网站是静态的(所有数据/文件都存储在磁盘上)。 之后,人们发明了一个“数据库”来存储他们的数据。 这是一项伟大的创新,可以轻松地将数据保存在数据库中,而不是像以前那样保存在文件中。 我们开始将越来越少的数据保存到数据库中。 几年过去了,站点的规模不断扩大,人们开始注意到站点的性能属于过于频繁地从数据库读取数据的原因。 我们提出了一种新的优化方法–缓存(将数据存储在服务器上的缓存文件中)。 有趣,不是吗? 看起来像是“前进到过去” :-)但是为什么会这样呢? 很简单–硬盘的速度提高了,甚至超过了数据库的速度。 目前,我正在谈论MySQL。 可能其他类型的数据库仍会更快。 但是无论如何,进步不是停滞不前的。 现在人们已经学会了使用服务器内存进行数据存储。 RAM比硬盘快得多,并且内存的价格一直在下降,所以让我们利用所有这些优点。

现场演示

[sociallocker]

[社交储物柜]

打包下载

[/sociallocker]

[/ sociallocker]

步骤1. PHP (Step 1. PHP)

I made this useful class for you. We will use this class to working with memory using APC caching system.

我为您做了这个有用的课程。 我们将使用此类使用APC缓存系统处理内存。

classes / apc.caching.php (classes/apc.caching.php)

<?
class CacheAPC {
    var $iTtl = 600; // Time To Live
    var $bEnabled = false; // APC enabled?
    // constructor
    function CacheAPC() {
        $this->bEnabled = extension_loaded('apc');
    }
    // get data from memory
    function getData($sKey) {
        $bRes = false;
        $vData = apc_fetch($sKey, $bRes);
        return ($bRes) ? $vData :null;
    }
    // save data to memory
    function setData($sKey, $vData) {
        return apc_store($sKey, $vData, $this->iTtl);
    }
    // delete data from memory
    function delData($sKey) {
        return (apc_exists($sKey)) ? apc_delete($sKey) : true;
    }
}
?>

<?
class CacheAPC {
    var $iTtl = 600; // Time To Live
    var $bEnabled = false; // APC enabled?
    // constructor
    function CacheAPC() {
        $this->bEnabled = extension_loaded('apc');
    }
    // get data from memory
    function getData($sKey) {
        $bRes = false;
        $vData = apc_fetch($sKey, $bRes);
        return ($bRes) ? $vData :null;
    }
    // save data to memory
    function setData($sKey, $vData) {
        return apc_store($sKey, $vData, $this->iTtl);
    }
    // delete data from memory
    function delData($sKey) {
        return (apc_exists($sKey)) ? apc_delete($sKey) : true;
    }
}
?>

I prepared here several necessary functions which we will use: getData, setData and delData. Now, lets check first example file:

我在这里准备了一些将要使用的必要功能:getData,setData和delData。 现在,让我们检查第一个示例文件:

index.php (index.php)

<?php
$aData = array(
    'name' => 'table',
    'color' => 'brown',
    'size' => array(
        'x' => 200,
        'y' => 120,
        'z' => 150,
    ),
    'strength' => 10,
);
require_once('classes/apc.caching.php');
$oCache = new CacheAPC();
echo 'Initial data: <pre>'; // lets see what we have
print_r($aData);
echo '</pre>';
if ($oCache->bEnabled) { // if APC enabled
    $oCache->setData('my_object', $aData); // saving data to memory
    $oCache->setData('our_class_object', $oCache); // saving object of our class into memory too
    echo 'Now we saved all in memory, click <a href="ndex2.php">here</a> to check what we have in memory';
} else {
    echo 'Seems APC not installed, please install it to perform tests';
}
?>

<?php
$aData = array(
    'name' => 'table',
    'color' => 'brown',
    'size' => array(
        'x' => 200,
        'y' => 120,
        'z' => 150,
    ),
    'strength' => 10,
);
require_once('classes/apc.caching.php');
$oCache = new CacheAPC();
echo 'Initial data: <pre>'; // lets see what we have
print_r($aData);
echo '</pre>';
if ($oCache->bEnabled) { // if APC enabled
    $oCache->setData('my_object', $aData); // saving data to memory
    $oCache->setData('our_class_object', $oCache); // saving object of our class into memory too
    echo 'Now we saved all in memory, click <a href="ndex2.php">here</a> to check what we have in memory';
} else {
    echo 'Seems APC not installed, please install it to perform tests';
}
?>

In this file you can see that I saving 2 objects in memory: some predefined array and class object. Now, lets check second example file:

在此文件中,您可以看到我在内存中保存了两个对象:一些预定义的数组和类对象。 现在,让我们检查第二个示例文件:

index2.php (index2.php)

<?php
require_once('classes/apc.caching.php');
$oCache = new CacheAPC();
if ($oCache->bEnabled) { // if APC enabled
    $aMemData = $oCache->getData('my_object'); // getting data from memory
    $aMemData2 = $oCache->getData('our_class_object'); // getting data from memory about our class
    echo 'Data from memory: <pre>'; // lets see what we have from memory
    print_r($aMemData);
    echo '</pre>';
    echo 'Data from memory of object of CacheAPC class: <pre>';
    print_r($aMemData2);
    echo '</pre>';
    echo 'As you can see - all data read successfully, now lets remove data from memory and check results, click <a href="index3.php">here</a> to continue';
} else {
    echo 'Seems APC not installed, please install it to perform tests';
}
?>

<?php
require_once('classes/apc.caching.php');
$oCache = new CacheAPC();
if ($oCache->bEnabled) { // if APC enabled
    $aMemData = $oCache->getData('my_object'); // getting data from memory
    $aMemData2 = $oCache->getData('our_class_object'); // getting data from memory about our class
    echo 'Data from memory: <pre>'; // lets see what we have from memory
    print_r($aMemData);
    echo '</pre>';
    echo 'Data from memory of object of CacheAPC class: <pre>';
    print_r($aMemData2);
    echo '</pre>';
    echo 'As you can see - all data read successfully, now lets remove data from memory and check results, click <a href="index3.php">here</a> to continue';
} else {
    echo 'Seems APC not installed, please install it to perform tests';
}
?>

Here we only reading data from memory. And, as we see – all data is successfully read from the memory. Now, lets check last example file:

在这里,我们仅从内存中读取数据。 而且,正如我们所见–所有数据都已成功从内存中读取。 现在,让我们检查最后一个示例文件:

index3.php (index3.php)

<?php
require_once('classes/apc.caching.php');
$oCache = new CacheAPC();
if ($oCache->bEnabled) { // if APC enabled
    $oCache->delData('my_object'); // removing data from memory
    $oCache->delData('our_class_object'); // removing data from memory
    $aMemData = $oCache->getData('my_object'); // lets try to get data again
    $aMemData2 = $oCache->getData('our_class_object');
    echo 'Data from memory: <pre>'; // lets see what we have from memory
    print_r($aMemData);
    echo '</pre>';
    echo 'Data from memory of object of CacheAPC class: <pre>';
    print_r($aMemData2);
    echo '</pre>';
    echo 'As you can see - all data successfully removed. Great !';
} else {
    echo 'Seems APC not installed, please install it to perform tests';
}
?>

<?php
require_once('classes/apc.caching.php');
$oCache = new CacheAPC();
if ($oCache->bEnabled) { // if APC enabled
    $oCache->delData('my_object'); // removing data from memory
    $oCache->delData('our_class_object'); // removing data from memory
    $aMemData = $oCache->getData('my_object'); // lets try to get data again
    $aMemData2 = $oCache->getData('our_class_object');
    echo 'Data from memory: <pre>'; // lets see what we have from memory
    print_r($aMemData);
    echo '</pre>';
    echo 'Data from memory of object of CacheAPC class: <pre>';
    print_r($aMemData2);
    echo '</pre>';
    echo 'As you can see - all data successfully removed. Great !';
} else {
    echo 'Seems APC not installed, please install it to perform tests';
}
?>

结论 (Conclusion)

Today, I told you how we can use the server’s memory with APC caching. I hope you have any thoughts on optimizing your website(s). For example you can cache some frequently-used data (for example: information about users on your system, or some settings of website etc.) Good luck in your work!

今天,我告诉您如何通过APC缓存使用服务器的内存。 希望您对优化网站有任何想法。 例如,您可以缓存一些常用数据(例如:有关系统上用户的信息或网站的某些设置等)。祝您工作顺利!

翻译自: https://www.script-tutorials.com/how-to-use-apc-caching-with-php/

php apc缓存

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值