memcache php_如何在PHP中使用Memcache

memcache php

memcache php_如何在PHP中使用Memcache_第1张图片
memcache php_如何在PHP中使用Memcache_第2张图片

Memcache with PHP. Today I have new article for PHP. Last time I did post about Memcache with PHP. Today we will talking about caching in PHP again. I will show you how you can use Memcache in PHP. We will prepare useful class for working with Memcache for us and several examples. Memcache itself providing procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load in dynamic web applications.

使用PHP的Memcache。 今天,我有关于PHP的新文章。 上次我确实使用PHP发布了有关Memcache的文章。 今天,我们将再次讨论使用PHP进行缓存。 我将向您展示如何在PHP中使用Memcache 。 我们将为我们准备有用的类以及与Memcache一起使用的一些示例。 Memcache本身为memcached,高效的缓存守护程序提供了面向过程和面向对象的接口,该接口专门用于减少动态Web应用程序中的数据库负载。

I don`t have online demo for today, just because haven`t installed Memcache at our hosting. But I will include samples of using our new library in this article too (as I did for our previous APC-related post). Also, pay attention that Memcache extension not bundled with PHP by default. This extension available in PECL. Additional information you can find here.

我今天没有在线演示,仅因为还没有在我们的主机上安装Memcache。 但是我也会在本文中包含使用我们的新库的示例(就像我以前与APC相关的文章所做的那样)。 另外,请注意,默认情况下,Memcache扩展未与PHP捆绑在一起。 此扩展在PECL中可用。 您可以在此处找到更多信息。

[sociallocker]

[社交储物柜]

打包下载

[/sociallocker]

[/ sociallocker]

Now – download the source files and lets start coding !

现在–下载源文件并开始编码!

步骤1. PHP (Step 1. PHP)

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

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

classes / memcache.caching.php (classes/memcache.caching.php)


oCache = new Memcache();
            $this->bEnabled = true;
            if (! $this->oCache->connect('localhost', 11211))  { // Instead 'localhost' here can be IP
                $this->oCache = null;
                $this->bEnabled = false;
            }
        }
    }
    // get data from cache server
    function getData($sKey) {
        $vData = $this->oCache->get($sKey);
        return false === $vData ? null : $vData;
    }
    // save data to cache server
    function setData($sKey, $vData) {
        //Use MEMCACHE_COMPRESSED to store the item compressed (uses zlib).
        return $this->oCache->set($sKey, $vData, 0, $this->iTtl);
    }
    // delete data from cache server
    function delData($sKey) {
        return $this->oCache->delete($sKey);
    }
}
?>

oCache = new Memcache();
            $this->bEnabled = true;
            if (! $this->oCache->connect('localhost', 11211))  { // Instead 'localhost' here can be IP
                $this->oCache = null;
                $this->bEnabled = false;
            }
        }
    }
    // get data from cache server
    function getData($sKey) {
        $vData = $this->oCache->get($sKey);
        return false === $vData ? null : $vData;
    }
    // save data to cache server
    function setData($sKey, $vData) {
        //Use MEMCACHE_COMPRESSED to store the item compressed (uses zlib).
        return $this->oCache->set($sKey, $vData, 0, $this->iTtl);
    }
    // delete data from cache server
    function delData($sKey) {
        return $this->oCache->delete($sKey);
    }
}
?>

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)


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

 'table',
    'color' => 'brown',
    'size' => array(
        'x' => 200,
        'y' => 120,
        'z' => 150,
    ),
    'strength' => 10,
);
require_once('classes/memcache.caching.php');
$oCache = new CacheMemcache();
echo 'Initial data: 
'; // lets see what we have
print_r($aData);
echo '
'; if ($oCache->bEnabled) { // if Memcache enabled $oCache->setData('my_object', $aData); // saving data to cache server $oCache->setData('our_class_object', $oCache); // saving object of our class into cache server too echo 'Now we saved all in cache server, click here to check what we have in cache server'; } else { echo 'Seems Memcache 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)


bEnabled) { // if Memcache enabled
    $aMemData = $oCache->getData('my_object'); // getting data from cache server
    $aMemData2 = $oCache->getData('our_class_object'); // getting data from cache server about our class
    echo 'Data from cache server: 
'; // lets see what we have from cache server
    print_r($aMemData);
    echo '
'; echo 'Data from cache server of object of CacheMemcache class:
';
    print_r($aMemData2);
    echo '
'; echo 'As you can see - all data read successfully, now lets remove data from cache server and check results, click here to continue'; } else { echo 'Seems Memcache not installed, please install it to perform tests'; } ?>

bEnabled) { // if Memcache enabled
    $aMemData = $oCache->getData('my_object'); // getting data from cache server
    $aMemData2 = $oCache->getData('our_class_object'); // getting data from cache server about our class
    echo 'Data from cache server: 
'; // lets see what we have from cache server
    print_r($aMemData);
    echo '
'; echo 'Data from cache server of object of CacheMemcache class:
';
    print_r($aMemData2);
    echo '
'; echo 'As you can see - all data read successfully, now lets remove data from cache server and check results, click here to continue'; } else { echo 'Seems Memcache 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)


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

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

结论 (Conclusion)

Today, I told you how we can use Memcache. I hope you got new thoughts on optimizing your website(s). Good luck in your work!

今天,我告诉您如何使用Memcache。 希望您对优化网站有新的想法。 祝您工作顺利!

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

memcache php

你可能感兴趣的:(java,php,python,mysql,linux)