如何检查是否为 PHP 安装了 memcache 或 memcached?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1463670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if memcache or memcached is installed for PHP?
提问by TheFlash
How do I test if memcacheor memcached(for PHP) is installed on my Apache webserver?
如何测试我的 Apache 网络服务器上是否安装了memcache或memcached(用于 PHP)?
Memcacheis a caching daemon designed especially for dynamic web applications to decrease database load by storing objects in memory.
Memcache是一个缓存守护进程,专为动态 Web 应用程序设计,通过将对象存储在内存中来减少数据库负载。
回答by mauris
You can look at phpinfo() or check if any of the functions of memcache is available. Ultimately, check whether the Memcacheclass exists or not.
您可以查看phpinfo() 或检查memcache 的任何功能是否可用。最后,检查Memcache该类是否存在。
e.g.
例如
if(class_exists('Memcache')){
// Memcache is enabled.
}
回答by J.C. Inacio
why not use the extension_loaded()function?
为什么不使用extension_loaded()函数?
回答by Bijay Rungta
Use this code to not only check if the memcache extension is enabled, but also whether the daemon is running and able to store and retrieve data successfully:
使用此代码不仅可以检查 memcache 扩展是否启用,还可以检查守护进程是否正在运行并能够成功存储和检索数据:
<?php
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 '<pre>';
if ($aData) {
echo '<h2>Data from Cache:</h2>';
print_r($aData);
} else {
$aData = array(
'me' => 'you',
'us' => 'them',
);
echo '<h2>Fresh Data:</h2>';
print_r($aData);
$memcache->set('data', $aData, 0, 300);
}
$aData = $memcache->get('data');
if ($aData) {
echo '<h3>Memcache seem to be working fine!</h3>';
} else {
echo '<h3>Memcache DOES NOT seem to be working!</h3>';
}
echo '</pre>';
}
}
if (!$isMemcacheAvailable) {
echo 'Memcache not available';
}
?>
回答by hlasso
I know this is an old thread, but there's another way that I've found useful for any extension.
我知道这是一个旧线程,但是我发现另一种方法对任何扩展都有用。
Run
跑
php -m | grep <module_name>
php -m | grep <module_name>
In this particular case:
在这种特殊情况下:
php -m | grep memcache
php -m | grep memcache
If you want to list all PHP modules then:
如果要列出所有 PHP 模块,则:
php -m
php -m
Depending on your system you'd get an output similar to this:
根据您的系统,您会得到与此类似的输出:
[PHP Modules]
apc
bcmath
bz2
... lots of other modules ...
mbstring
memcache
... and still more modules ...
zip
zlib
[Zend Modules]
You can see that memcache is in this list.
可以看到memcache在这个列表中。
回答by antitoxic
Note that all of the class_exists, extensions_loaded, and function_existsonlycheck the linkbetween PHPand the memcachepackage.
需要注意的是所有的class_exists,extensions_loaded以及function_exists只检查链路之间PHP和memcache包装。
To actually check whether memcache is installed you must either:
要实际检查是否安装了内存缓存,您必须:
- know the OS platform and use shell commands to check whether memcache package is installed
- or test whether memcache connection can be established on the expected port
- 了解操作系统平台,使用shell命令查看是否安装了memcache包
- 或者测试是否可以在预期端口上建立memcache连接
EDIT 2: OK, actually here's an easier complete solution:
编辑 2:好的,实际上这是一个更简单的完整解决方案:
if (class_exists('Memcache')) {
$memcache = new Memcache;
$isMemcacheAvailable = @$memcache->connect('localhost');
}
if ($isMemcacheAvailable) {
//...
}
Outdated code below
下面过时的代码
EDIT: Actually you must force PHP to throw error on warnings first. Have a look at this SO questionanswer.
编辑:实际上,您必须首先强制 PHP 在警告时抛出错误。看看这个SO 问题答案。
You can then test the connection via:
然后,您可以通过以下方式测试连接:
try {
$memcache->connect('localhost');
} catch (Exception $e) {
// well it's not here
}
回答by mgutt
You have several options ;)
你有几个选择;)
$memcache_enabled = class_exists('Memcache');
$memcache_enabled = extension_loaded('memcache');
$memcache_enabled = function_exists('memcache_connect');
回答by danno
It may be relevant to see if it's running in PHP via command line as well-
查看它是否也通过命令行在 PHP 中运行可能是相关的 -
<path-to-php-binary>php -i | grep memcache
回答by Paul Dragoonis
The best approach in this case is to use extension_loaded() or function_exists() they are equally as fast.
在这种情况下最好的方法是使用 extension_loaded() 或 function_exists() 它们同样快。
You can see evidence here:
你可以在这里看到证据:
https://github.com/dragoonis/ppi-framework/blob/master/Cache/Memcached.php#L140
https://github.com/dragoonis/ppi-framework/blob/master/Cache/Memcached.php#L140
Bear in mind that some PHP extensions such as APC have php.ini settings that can disable them even though the extension may be loaded. Here is an example of how to check against that also:
请记住,某些 PHP 扩展(例如 APC)具有 php.ini 设置,即使扩展可能已加载,也可以禁用它们。这是一个如何检查的示例:
https://github.com/dragoonis/ppi-framework/blob/master/Cache/Apc.php#L79
https://github.com/dragoonis/ppi-framework/blob/master/Cache/Apc.php#L79
Hope this helps.
希望这可以帮助。
回答by x29a
I combined, minified and extended (some more checks) the answers from @Bijay Rungta and @J.C. Inacio
我合并、缩小和扩展(更多检查)@Bijay Rungta 和 @JC Inacio 的答案
<?php
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');
回答by Nassim
this is my test function that I use to check Memcache on the server
这是我用来检查服务器上的 Memcache 的测试函数
<?php
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."<br/>\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)<br/>\n";
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";
var_dump($get_result);
}
if you see something like this
如果你看到这样的东西
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) }
it means that everything is okay
这意味着一切都很好
Cheers!
干杯!

