php 查看memcache,apache-如何检查是否为PHP安装了memcache或memcached?

apache-如何检查是否为PHP安装了memcache或memcached?

如何测试Apache Web服务器上是否安装了memcache或memcached(对于PHP)?

Memcache是一个缓存守护程序,专门为 动态Web应用程序以减少数据库负载 将对象存储在内存中。

10个解决方案

49 votes

您可以查看phpinfo()或检查memcache的任何功能是否可用。 最终,检查Memcache类是否存在。

例如

if(class_exists('Memcache')){

// Memcache is enabled.

}

mauris answered 2020-06-22T15:21:51Z

26 votes

为什么不使用extension_loaded()函数?

J.C. Inacio answered 2020-06-22T15:22:11Z

26 votes

使用此代码不仅可以检查是否已启用memcache扩展,还可以检查守护程序是否正在运行以及是否能够成功存储和检索数据:

if (class_exists('Memcache')) {

$server = 'localhost';

if (!empty($_REQUEST['server'])) {

$server = $_REQUEST['server'];

}

$memcache = new Memcache;

$isMemcacheAvailable = @$memcache->connect($server);

if ($isMemcacheAvailable) {

$aData = $memcache->get('data');

echo '

';

if ($aData) {

echo '

Data from Cache:

';

print_r($aData);

} else {

$aData = array(

'me' => 'you',

'us' => 'them',

);

echo '

Fresh Data:

';

print_r($aData);

$memcache->set('data', $aData, 0, 300);

}

$aData = $memcache->get('data');

if ($aData) {

echo '

Memcache seem to be working fine!

';

} else {

echo '

Memcache DOES NOT seem to be working!

';

}

echo '

';

}

}

if (!$isMemcacheAvailable) {

echo 'Memcache not available';

}

?>

Bijay Rungta answered 2020-06-22T15:22:33Z

20 votes

我知道这是一个旧线程,但是对于其他扩展,我发现还有另一种有用的方法。

php -m

在这种情况下:

php -m

如果要列出所有PHP模块,则:

php -m

根据您的系统,您将获得类似以下的输出:

[PHP Modules]

apc

bcmath

bz2

... lots of other modules ...

mbstring

memcache

... and still more modules ...

zip

zlib

[Zend Modules]

您可以看到此列表中有memcache。

hlasso answered 2020-06-22T15:23:28Z

10 votes

您有几种选择;)

$memcache_enabled = class_exists('Memcache');

$memcache_enabled = extension_loaded('memcache');

$memcache_enabled = function_exists('memcache_connect');

mgutt answered 2020-06-22T15:23:48Z

10 votes

请注意,所有class_exists、extensions_loaded和function_exists仅检查PHP和memcache程序包之间的链接。

要实际检查是否已安装内存缓存,您必须执行以下任一操作:

了解OS平台并使用Shell命令检查是否已安装memcache软件包

或测试是否可以在预期的端口上建立Memcache连接

编辑2:好,实际上这是一个更简单的完整解决方案:

if (class_exists('Memcache')) {

$memcache = new Memcache;

$isMemcacheAvailable = @$memcache->connect('localhost');

}

if ($isMemcacheAvailable) {

//...

}

下面的代码过时

编辑:实际上,您必须强制PHP首先在警告上引发错误。 看看这个SO问题的答案。

然后,您可以通过以下方式测试连接:

try {

$memcache->connect('localhost');

} catch (Exception $e) {

// well it's not here

}

antitoxic answered 2020-06-22T15:25:07Z

7 votes

可能还需要通过命令行查看它是否在PHP中运行-

php -i | grep memcache

danno answered 2020-06-22T15:25:27Z

4 votes

在这种情况下,最好的方法是使用extension_loaded()或function_exists()一样快。

您可以在此处查看证据:

[HTTPS://GitHub.com/dragoon is/屁屁-framework/blob/master/cache/memcached.PHP#l140]

请记住,某些PHP扩展(例如APC)具有php.ini设置,即使可能已加载该扩展,也可以禁用它们。这是一个如何与此同时进行检查的示例:

[HTTPS://GitHub.com/dragoon is/屁屁-framework/blob/master/cache/APC.PHP#l79]

希望这可以帮助。

Paul Dragoonis answered 2020-06-22T15:26:09Z

4 votes

我结合,缩小和扩展了(更多检查)@Bijay Rungta和@ J.C的答案。 伊纳西奥

if(!extension_loaded('Memcache'))

{

die("Memcache extension is not loaded");

}

if (!class_exists('Memcache'))

{

die('Memcache class not available');

}

$memcacheObj = new Memcache;

if(!$memcacheObj)

{

die('Could not create memcache object');

}

if (!$memcacheObj->connect('localhost'))

{

die('Could not connect to memcache server');

}

// testdata to store in memcache

$testData = array(

'the' => 'cake',

'is' => 'a lie',

);

// set data (if not present)

$aData = $memcacheObj->get('data');

if (!$aData)

{

if(!$memcacheObj->set('data', $testData, 0, 300))

{

die('Memcache could not set the data');

}

}

// try to fetch data

$aData = $memcacheObj->get('data');

if (!$aData)

{

die('Memcache is not responding with data');

}

if($aData !== $testData)

{

die('Memcache is responding but with wrong data');

}

die('Memcache is working fine');

x29a answered 2020-06-22T15:26:30Z

2 votes

这是我用来检查服务器上Memcache的测试功能

public function test()

{

// memcache test - make sure you have memcache extension installed and the deamon is up and running

$memcache = new Memcache;

$memcache->connect('localhost', 11211) or die ("Could not connect");

$version = $memcache->getVersion();

echo "Server's version: ".$version."
\n";

$tmp_object = new stdClass;

$tmp_object->str_attr = 'test';

$tmp_object->int_attr = 123;

$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");

echo "Store data in the cache (data will expire in 10 seconds)
\n";

$get_result = $memcache->get('key');

echo "Data from the cache:
\n";

var_dump($get_result);

}

如果看到这样的话

Server's version: 1.4.5_4_gaa7839e

Store data in the cache (data will expire in 10 seconds)

Data from the cache:

object(stdClass)#3 (2) { ["str_attr"]=> string(4) "test" ["int_attr"]=> int(123) }

这意味着一切都很好

干杯!

Nassim answered 2020-06-22T15:27:07Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值