mau*_*ris 51
您可以查看phpinfo()或检查memcache的任何功能是否可用.最后,检查Memcache
班级是否存在.
例如
if(class_exists('Memcache')){
// Memcache is enabled.
}
Run Code Online (Sandbox Code Playgroud)
Bij*_*gta 27
使用此代码不仅可以检查是否启用了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';
}
?>
Run Code Online (Sandbox Code Playgroud)
hla*_*sso 20
我知道这是一个旧线程,但我发现另一种方法对任何扩展都有用.
跑
php -m | grep <module_name>
在这种特殊情况下:
php -m | grep memcache
如果要列出所有PHP模块,那么:
php -m
根据您的系统,您将得到类似于此的输出:
[PHP Modules]
apc
bcmath
bz2
... lots of other modules ...
mbstring
memcache
... and still more modules ...
zip
zlib
[Zend Modules]
Run Code Online (Sandbox Code Playgroud)
您可以看到memcache在此列表中.
mgu*_*utt 10
你有几个选择;)
$memcache_enabled = class_exists('Memcache');
$memcache_enabled = extension_loaded('memcache');
$memcache_enabled = function_exists('memcache_connect');
Run Code Online (Sandbox Code Playgroud)
ant*_*xic 10
需要注意的是所有的class_exists
,extensions_loaded
以及function_exists
只检查链路之间PHP
和memcache
包装.
要实际检查是否安装了memcache,您必须:
编辑2:好的,实际上这是一个更简单的完整解决方案:
if (class_exists('Memcache')) {
$memcache = new Memcache;
$isMemcacheAvailable = @$memcache->connect('localhost');
}
if ($isMemcacheAvailable) {
//...
}
Run Code Online (Sandbox Code Playgroud)
编辑:实际上你必须强制PHP首先在警告上抛出错误.看看这个SO问题的答案.
然后您可以通过以下方式测试连接:
try {
$memcache->connect('localhost');
} catch (Exception $e) {
// well it's not here
}
Run Code Online (Sandbox Code Playgroud)
通过命令行查看它是否在PHP中运行可能是相关的 -
<path-to-php-binary>php -i | grep memcache
Run Code Online (Sandbox Code Playgroud)
我合并、缩小和扩展(更多检查)@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');
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
97119 次 |
最近记录: |