热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

HowdoItestifmemcacheormemcached(forPHP)isinstalledonmyApachewebserver?如何测试我的Apache网络

How do I test if memcache or memcached (for PHP) is installed on my Apache webserver?

如何测试我的Apache网络服务器上是否安装了memcache或memcached(用于PHP)?

Memcache is a caching daemon designed especially for dynamic web applications to decrease database load by storing objects in memory.

Memcache是​​一个缓存守护进程,专门为动态Web应用程序设计,通过在内存中存储对象来减少数据库负载。

9 个解决方案

#1


38  

You can look at phpinfo() or check if any of the functions of memcache is available. Ultimately, check whether the Memcache class exists or not.

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

e.g.

例如

if(class_exists('Memcache')){
  // Memcache is enabled.
}

#2


22  

why not use the extension_loaded() function?

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

#3


22  

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扩展,还可以检查守护程序是否正在运行并且能够成功存储和检索数据:

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'; } ?>

#4


16  

I know this is an old thread, but there's another way that I've found useful for any extension.

我知道这是一个旧线程,但我发现另一种方法对任何扩展都有用。

Run

php -m | grep

php -m | grep

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在此列表中。

#5


10  

Note that all of the class_exists, extensions_loaded, and function_exists only check the link between PHP and the memcache package.

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

To actually check whether memcache is installed you must either:

要实际检查是否安装了memcache,您必须:

  • know the OS platform and use shell commands to check whether memcache package is installed
  • 了解操作系统平台并使用shell命令检查是否安装了memcache包
  • or test whether memcache connection can be established on the expected port
  • 或者测试是否可以在预期的端口上建立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 question answer.

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

You can then test the connection via:

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

try {
    $memcache->connect('localhost');
} catch (Exception $e) {
    // well it's not here
}

#6


8  

You have several options ;)

你有几个选择;)

$memcache_enabled = class_exists('Memcache');
$memcache_enabled = extension_loaded('memcache');
$memcache_enabled = function_exists('memcache_connect');

#7


5  

It may be relevant to see if it's running in PHP via command line as well-

通过命令行查看它是否在PHP中运行可能是相关的 -

php -i | grep memcache

#8


4  

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.

希望这可以帮助。

#9


1  

this is my test function that I use to check Memcache on the server

这是我用来检查服务器上的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); }

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!

干杯!


推荐阅读
author-avatar
横着走觉察生活_915
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有