关于memcached原理及安装部署

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

[root@www ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/lnmp/mysql/bin:/root/bin
[root@www ~]# vim /etc/profile
@@@@@@
79 PATH=$PATH:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
@@@@@@
[root@www ~]# source /etc/profile
[root@www ~]# tar zxf memcache-2.2.5.tgz
[root@www ~]# cd memcache-2.2.5
[root@www memcache-2.2.5]# phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[root@www memcache-2.2.5]# ./configure --enable-memcache
[root@www memcache-2.2.5]# make && make install
[root@www 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@www memcache-2.2.5]# vim /usr/local/lnmp/php/etc/php.ini
@@@@@@
845 extension=memcache.so
@@@@@@
[root@www memcache-2.2.5]# /etc/init.d/fpm reload
[root@www memcache-2.2.5]# cd /usr/local/lnmp/nginx/html
[root@www html]# vim index.php
@@@@@@
1 <?php
2 phpinfo()
3 ?>
@@@@@@

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


[root@www 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@www html]# nginx -s reload
[root@www ~]# php -m | grep memcache ##说明 php 中添加 memcache 模块成功
memcache

安装memcached

[root@wwwwww ~]# yum install -y memcached
[root@wwwwww ~]# /etc/init.d/memcached start
Starting memcached:                                        [  OK  ]
[root@wwwwww ~]# vi /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
~     
[root@wwwwww ~]# 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安装成功

原文地址:https://www.cnblogs.com/aallenn/p/6700617.html