阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Memcached原理及Linux下安装部署

143次阅读
没有评论

共计 3617 个字符,预计需要花费 10 分钟才能阅读完成。

Memcached 是一个高性能的分布式内存对象缓存系统,用于动态 Web 应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached 基于一个存储键 / 值对的 hashmap。其守护进程(daemon)是用 C 写的,但是客户端可以用任何语言来编写,并通过 memcached 协议与守护进程通信。memcached 缺乏认证以及安全管制,这代表应该将 memcached 服务器放置在防火墙后。memcache 的工作流程如下:先检查客户端的请求数据是否在 memcached 中,如有,直接把请求数据返回,不再对数据库进行任何操作;如果请求的数据不在 memcached 中,就去查数据库,把从数据库中获取的数据返回给客户端,同时把数据缓存一份到 memcached 中(memcached 客户端不负责,需要程序明确实现);每次更新数据库的同时更新 memcached 中的数据,保证一致性;当分配给 memcached 内存空间用完之后,会使用 LRU(Least Recently Used,最近最少使用)策略加上到期失效策略,失效数据首先被替换,然后再替换掉最近未使用的数据。

给 php 添加 memcache 模块

[root@linuxidc ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/lnmp/mysql/bin:/root/bin
[root@linuxidc ~]# vim /etc/profile
@@@@@@
79 PATH=$PATH:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
@@@@@@
[root@linuxidc ~]# source /etc/profile
[root@linuxidc ~]# tar zxf memcache-2.2.5.tgz
[root@linuxidc ~]# cd memcache-2.2.5
[root@linuxidc memcache-2.2.5]# phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[root@linuxidc memcache-2.2.5]# ./configure –enable-memcache
[root@linuxidc memcache-2.2.5]# make && make install
[root@linuxidc memcache-2.2.5]# ll
/usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
-rwxr-xr-x 1 root root 259040 Jan 16 14:00 memcache.so
[root@linuxidc memcache-2.2.5]# vim /usr/local/lnmp/php/etc/php.ini
@@@@@@
845 extension=memcache.so
@@@@@@
[root@linuxidc memcache-2.2.5]# /etc/init.d/fpm reload
[root@linuxidc memcache-2.2.5]# cd /usr/local/lnmp/nginx/html
[root@linuxidc html]# vim index.php
@@@@@@
1 <?php
2 phpinfo()
3 ?>
@@@@@@

在浏览器中打开这个 php 页面 可以看到 memcache 相关信息

[root@linuxidc html]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@linuxidc html]# nginx -s reload
[root@linuxidc ~]# php -m | grep memcache ## 说明 php 中添加 memcache 模块成功
memcache

安装 memcached

[root@linuxidc.com ~]# yum install -y memcached
[root@linuxidc.com ~]# /etc/init.d/memcached start
Starting memcached:                                        [OK]
[root@linuxidc.com ~]# vi /etc/sysconfig/memcached
PORT=”11211″
USER=”memcached”
MAXCONN=”1024″
CACHESIZE=”64″
OPTIONS=””
~   
[root@linuxidc.com ~]# memcached-tool 127.0.0.1:11211 stats
#127.0.0.1:11211  Field      Value
        accepting_conns          1
              auth_cmds          0
            auth_errors          0
                  bytes          0
              bytes_read        799
          bytes_written      38165
              cas_badval          0
                cas_hits          0
              cas_misses          0
              cmd_flush          0
                cmd_get          0
                cmd_set          0
            conn_yields          0
  connection_structures          11
        curr_connections          10
              curr_items          0
              decr_hits          0
            decr_misses          0
            delete_hits          0
          delete_misses          0
              evictions          0
                get_hits          0
              get_misses          0
              incr_hits          0
            incr_misses          0
          limit_maxbytes    67108864
    listen_disabled_num          0
                    pid        5154
            pointer_size          64
          rusage_system    0.121981
            rusage_user    0.068989
                threads          4
                    time  1469970117
      total_connections          93
            total_items          0
                  uptime        5637
                version      1.4.4

memcached 安装成功

CentOS 6.6 下 Memcached 源码安装配置  http://www.linuxidc.com/Linux/2015-09/123019.htm

Memcached 安装及启动脚本 http://www.linuxidc.com/Linux/2013-07/87641.htm

PHP 中使用 Memcached 的性能问题 http://www.linuxidc.com/Linux/2013-06/85883.htm

Ubuntu 下安装 Memcached 及命令解释 http://www.linuxidc.com/Linux/2013-06/85832.htm

Memcached 的安装和应用 http://www.linuxidc.com/Linux/2013-08/89165.htm

使用 Nginx+Memcached 的小图片存储方案 http://www.linuxidc.com/Linux/2013-11/92390.htm

Memcached 使用入门 http://www.linuxidc.com/Linux/2011-12/49516p2.htm

Memcached 的详细介绍 :请点这里
Memcached 的下载地址 :请点这里

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-08/133851.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计3617字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中