热门标签 | 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!

干杯!


推荐阅读
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • Windows服务与数据库交互问题解析
    本文探讨了在Windows 10(64位)环境下开发的Windows服务,旨在定期向本地MS SQL Server (v.11)插入记录。尽管服务已成功安装并运行,但记录并未正确插入。我们将详细分析可能的原因及解决方案。 ... [详细]
  • 本文详细介绍了 Apache Jena 库中的 Txn.executeWrite 方法,通过多个实际代码示例展示了其在不同场景下的应用,帮助开发者更好地理解和使用该方法。 ... [详细]
  • 本文详细介绍了如何准备和安装 Eclipse 开发环境及其相关插件,包括 JDK、Tomcat、Struts 等组件的安装步骤及配置方法。 ... [详细]
  • HBase运维工具全解析
    本文深入探讨了HBase常用的运维工具,详细介绍了每种工具的功能、使用场景及操作示例。对于HBase的开发人员和运维工程师来说,这些工具是日常管理和故障排查的重要手段。 ... [详细]
  • PHP 过滤器详解
    本文深入探讨了 PHP 中的过滤器机制,包括常见的 $_SERVER 变量、filter_has_var() 函数、filter_id() 函数、filter_input() 函数及其数组形式、filter_list() 函数以及 filter_var() 和其数组形式。同时,详细介绍了各种过滤器的用途和用法。 ... [详细]
  • 本文详细介绍了 org.apache.commons.io.IOCase 类中的 checkCompareTo() 方法,通过多个代码示例展示其在不同场景下的使用方法。 ... [详细]
  • 深入理解Play Framework 1.2.7中的缓存机制
    本文探讨了Play Framework 1.2.7版本中提供的缓存解决方案,包括Ehcache和Memcached的集成与使用。文章详细介绍了缓存相关的类及其功能,以及如何通过配置选择合适的缓存实现。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文详细介绍了如何解决Uploadify插件在Internet Explorer(IE)9和10版本中遇到的点击失效及JQuery运行时错误问题。通过修改相关JavaScript代码,确保上传功能在不同浏览器环境中的一致性和稳定性。 ... [详细]
  • This guide provides a comprehensive step-by-step approach to successfully installing the MongoDB PHP driver on XAMPP for macOS, ensuring a smooth and efficient setup process. ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 作为一名新手,您可能会在初次尝试使用Eclipse进行Struts开发时遇到一些挑战。本文将为您提供详细的指导和解决方案,帮助您克服常见的配置和操作难题。 ... [详细]
  • 本文详细分析了Hive在启动过程中遇到的权限拒绝错误,并提供了多种解决方案,包括调整文件权限、用户组设置以及环境变量配置等。 ... [详细]
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社区 版权所有