为了账号安全,请及时绑定邮箱和手机立即绑定

Windows环境下安装和配置WNMP的Memcached

标签:
PHP Nginx

PHP开发不是我最擅长的服务端语言,但是因为接触WordPress很早,了解到一个叫做 WPJam - https://blog.wpjam.com/article/wordpress-memcached/ 的插件,后来他更新了一个支持Memcached的功能,从而使我了解到除了常用的 RedisMongoDB 以外,还有 MemcacheMemcached 这样两个先辈。

PS:说得好像我其他语言开发服务端就很厉害,其实只是基于对javascript和python的熟悉程度,所以用 NodeJSPython 两种开发更快。

说回来主角 Memcached ,其实在熟悉了以后,我就经常使用他作为一些小型项目的高速缓存系统。它的优势在于配置十分迅速,使用起来与 MongoDB 无异,开发效率极高,同时也能够应对小型项目的压力。

由于我同时要兼任 策划、美术、开发 多个职务,本地环境一直都是Windows,但对windows服务器环境不熟悉,导致了 Memcached 始终不能正常运行。最近帮朋友处理一个项目,考虑到后续如果有人接手,恐怕还是 PHP 最快最容易找到人,所以把解决 Memcached 如何在Windows WNMP环境下跑起来又提上了日程。

PS2:继续吐槽度娘的技术文章搜索功能,80%的结果都帮不上忙,还有20%连边都不沾。最后还是靠谷哥出面解决了问题。可长点心吧。

文章里所有的文件我都尽可能提供了下载地址,同时也在 二机联盟-知识星球 存了一份。

Memcache 与 Memcached

Memcache 是一个原生版本,完全是在 PHP 框架内开发的,支持 面向对象 和 面向过程 两套接口并存,而 Memcached 是建立在 libmemcached 的基础上的,只支持 面向对象 接口。这就意味着在安装 Memcache 扩展的时候不要求安装其他的东西,但是在安装 Memcached 的时候会要求你安装 libmemcached

Memcached 支持 Binary Protocol,而 Memcache 不支持,这意味着 Memcached 会有更高的性能。

网上90%的文章都是打着 Memcached 的旗号在讲 Memcache ,真的是浪费大家时间。

安装 Memcached 服务

其实我很早就安装并测试好了 memcached 服务,再回顾一下。

下载 memcached.exe

我只用到了1.4.4版本,因为1.4.5开始就不能作为服务来启动了。而我本来就是测试用途,没有必要搞那么麻烦。

下载地址:

解压出来,推荐放到系统盘以外,免得什么时候重装或者恢复了系统又要重新下载。

安装为服务

管理员身份打开cmd,cd 到 memcached.exe 所在目录。

执行命令:

> memcached.exe -d install

其他相关命令可以用 -h 参数来查看。

> memcached.exe -h
memcached 1.4.4-14-g9c660c0
-p <num>      TCP port number to listen on (default: 11211)
-U <num>      UDP port number to listen on (default: 11211, 0 is off)
-s <file>     UNIX socket path to listen on (disables network support)
-a <mask>     access mask for UNIX socket, in octal (default: 0700)
-l <ip_addr>  interface to listen on (default: INADDR_ANY, all addresses)
-s <file>     unix socket path to listen on (disables network support)
-a <mask>     access mask for unix socket, in octal (default 0700)
-l <ip_addr>  interface to listen on, default is INADDR_ANY
-d start          tell memcached to start
-d restart        tell running memcached to do a graceful restart
-d stop|shutdown  tell running memcached to shutdown
-d install        install memcached service
-d uninstall      uninstall memcached service
-r            maximize core file limit
-u <username> assume identity of <username> (only when run as root)
-m <num>      max memory to use for items in megabytes (default: 64 MB)
-M            return error on memory exhausted (rather than removing items)
-c <num>      max simultaneous connections (default: 1024)
-k            lock down all paged memory.  Note that there is a
            limit on how much memory you may lock.  Trying to
            allocate more than that would fail, so be sure you
            set the limit correctly for the user you started
            the daemon with (not for -u <username> user;
            under sh this is done with 'ulimit -S -l NUM_KB').
-v            verbose (print errors/warnings while in event loop)
-vv           very verbose (also print client commands/reponses)
-vvv          extremely verbose (also print internal state transitions)
-h            print this help and exit
-i            print memcached and libevent license
-P <file>     save PID in <file>, only used with -d option
-f <factor>   chunk size growth factor (default: 1.25)
-n <bytes>    minimum space allocated for key+value+flags (default: 48)
-L            Try to use large memory pages (if available). Increasing
            the memory page size could reduce the number of TLB misses
            and improve the performance. In order to get large pages
            from the OS, memcached will allocate the total item-cache
            in one large chunk.
-D <char>     Use <char> as the delimiter between key prefixes and IDs.
            This is used for per-prefix stats reporting. The default is
            ":" (colon). If this option is specified, stats collection
            is turned on automatically; if not, then it may be turned on
            by sending the "stats detail on" command to the server.
-t <num>      number of threads to use (default: 4)
-R            Maximum number of requests per event, limits the number of
            requests process for a given connection to prevent
            starvation (default: 20)
-C            Disable use of CAS
-b            Set the backlog queue limit (default: 1024)
-B            Binding protocol - one of ascii, binary, or auto (default)
-I            Override the size of each slab page. Adjusts max item size
            (default: 1mb, min: 1k, max: 128m)

检查启动状况

使用 telnet 来查看 memcached 是否正常运作。首先确保已经安装了 telnet 客户端,没有安装的到 打开或关闭 Windows 功能 里安装即可。输入 telnet 命令后,先回车一个,出现了 Error 再输入 stats 命令。

>telnet 127.0.0.1 11211

ERROR

>stats

STAT get_hits 3096
STAT get_misses 1269
STAT delete_misses 0
STAT delete_hits 4
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 364075
STAT bytes_written 824270
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 71129
STAT curr_items 189
STAT total_items 1054
STAT evictions 0
END

这样就是跑起来了。

当然也可以到 windows 的服务面板里查看 memcached 服务是否运行,或者在任务管理器面板里查看服务标签。

配置php环境

我使用的是 PhpStudy 的 WNMP 集成环境。PHP版本为7.4,x64位架构,非线程安全。

下载相关dll

废话不多说,两种方式下载:

这里特别要注意版本问题,要完全对上运行环境才行。

下载以后,将 php_memcached.dll 放到 DISK:\your\path\to\php\ext 目录下,将 libmemcached.dll 放到 C:\Windows 目录下。

配置php.ini

这个就简单了,在所用的php版本的 php.ini 文档末尾加入 extension=php_memcached.dll 即可。

测试

phpinfo() 查看状态:

当然,也可以写代码来检测:

<?php
if (class_exists('Memcached')){
  echo 'Memcached';
}else{
  echo 'No Memcached';
}

// [OUTPUT] Memcached
?>

清理缓存:

telnet localhost 11211
flush_all

[2022年12月19日原始发布于本作者博客]

👇点击“阅读原文”可恢复文章内所有链接哦!

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
0
获赞与收藏
0

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消