您的位置:首页 > 数据库 > Memcache

memcached 高速缓存服务器软件详解

2019-04-30 22:31 1771 查看

系统环境
redhat rhel6.5
web架构
lnmp

memcached介绍

越来越多的 Web 应用程序开始使用 memcached 这个高速的缓存服务器软件。然而,
memcached 的基础知识远远未能像其他 Web 技术那样普及,memcached 在国内的大规模应用也鲜为人知。而日本的 mixi(http://mixi.jp)则在这方面走在了前面,不仅大规模使用 memcached 作为缓存来加速 Web 应用,而且自行开发了 Tokyo Cabinit、Tokyo Tyrant 等一系列相关的软件。

  • memcached 是什么
    memcached 是以 LiveJournal 旗下 Danga Interactive 公司的 Brad Fitzpatric 为首开发的一款软件。现在已成为 mixi、hatena、Facebook、Vox、LiveJournal 等众多服务中提高 Web 应用扩展性的重要因素。许多 Web 应用都将数据保存到 RDBMS 中,应用服务器从中读取数据并在浏览器中显示。但随着数据量的增大、访问的集中,就会出现 RDBMS 的负担加重、数据库响应恶化、网站显示延迟等重大影响。这时就该memcached 大显身手了。memcached 是高性能的分布式内存缓存服务器。一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态 Web 应用的速度、提高可扩展性。
  • MemCache的工作流程如下:
    先检查客户端的请求数据是否在memcached中,如有,直接把请求数据返回,不再对数据库进行任何操作;如果请求的数据不在memcached中,就去查数据库,把从数据库中获取的数据返回给客户端,同时把数据缓存一份到 memcached中(memcached客户端不负责,需要程序明确实现);每次更新数据库的同时更新memcached中的数据,保证一致性;当分配给memcached内存空间用完之后,会使用LRU(Least Recently Used,最近最少使用)策略加上到期失效策略,失效数据首先被替换,然后再替换掉最近未使用的数据

  • memcached 的特征
  • 协议简单
    memcached 的服务器客户端通信并不使用复杂的 XML 等格式,而使用简单的基于文本行的协议。因此,通过 telnet 也能在 memcached 上保存数据、取得数据。下面是例子。
  • 基于 libevent 的事件处理
    libevent 是个程序库,它将 Linux 的 epoll、BSD 类操作系统的 kqueue 等事件处理功能封装成统一的接口。即使对服务器的连接数增加,也能发挥 O(1)的性能。memcached 使用这个 libevent 库,因此能在 Linux、BSD、Solaris 等操作系统上发挥其高性能。关于事件处理这里就不再详细介绍,可以参考 Dan Kegel 的 The C10K Problem。
  • 内置内存存储方式
    为了提高性能,memcached 中保存的数据都存储在 memcached 内置的内存存储空间中。由于数据仅存在于内存中,因此重启 memcached、重启操作系统会导致全部数据消失。另外,内容容量达到指定值之后,就基于 LRU(Least Recently Used)算法自动删除不使用的缓存。memcached 本身是为缓存而设计的服务器,因此并没有过多考虑数据的永久性问题。
  • memcached 不互相通信的分布式
    memcached 尽管是“分布式”缓存服务器,但服务器端并没有分布式功能。各个 memcached 不会互相通信以共享信息。
    那么,怎样进行分布式呢?这完全取决于客户端的实现。

memcached 的使用方法

  • memcache对php页面访问的加速
  1. 解压memcache源码包:
    运行 memcached 需要本文开头介绍的 libevent 库。
    yum install libevent libevent­devel -y
    下载 memcached:http://www.danga.com/memcached/download.bml
    [root@lnmp ~]# tar zxf memcache-2.2.5.tgz
  2. 将前边php编译完成的二进制命令加入环境变量中,保证可以直接调用php命令

[root@lnmp ~]# vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
[root@lnmp ~]# source ~/.bash_profile [root@lnmp ~]# cd
/usr/local/lnmp/php/ [root@lnmp php]# cd bin/ [root@lnmp bin]# ls pear
peardev pecl phar phar.phar php php-cgi php-config phpize

  1. 创建一个预编译环境并进行编译汇编memcache源码包
    phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块,比如你想在原来编译好的php中加入memcached或者ImageMagick等扩展模块,可以使phpize。
    或者说,phpize的作用可以这样理解:侦测环境(phpize工具是在php安装目录下,基于这点phpize对应了当时的php环境,所以是要根据该php的配置情况生成对应的configure文件),建立一个configure文件。必须在一个目录下去运行phpize。那么phpize就知道你的的环境是哪个目录,并且configure文件建立在该目录下。

[root@lnmp memcache-2.2.5]# ls config9.m4 memcache.c
memcache_queue.h config.m4 memcache_consistent_hash.c
memcache_session.c config.w32 memcache.dsp
memcache_standard_hash.c CREDITS memcache.php
php_memcache.h example.php memcache_queue.c README
[root@lnmp memcache-2.2.5]# phpize Configuring for: PHP Api Version:
20131106 Zend Module Api No: 20131226 Zend Extension Api No:
220131226 [root@lnmp memcache-2.2.5]# ls acinclude.m4 configure.in
memcache.php aclocal.m4 config.w32
memcache_queue.c autom4te.cache CREDITS
memcache_queue.h build example.php
memcache_session.c config9.m4 install-sh
memcache_standard_hash.c config.guess ltmain.sh
missing config.h.in Makefile.global mkinstalldirs
config.m4 memcache.c php_memcache.h config.sub
memcache_consistent_hash.c README configure memcache.dsp
run-tests.php

  1. memcached 安装与一般应用程序相同,configure、make、make install 就行了。

[root@lnmp memcache-2.2.5]# ./configure [root@lnmp memcache-2.2.5]#
make && make install

  1. 编译完成后,提示我们进入php/extensions/no-debug-non-zts-20131226/目录,在这个目录中的我们可以 查看php中已经添加的模块,发现还没有我们的memcache模块,因此我们去到php的配置文件中添加memcache模块。

[root@lnmp memcache-2.2.5]# cd
/usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226/
[root@lnmp no-debug-non-zts-20131226]# ls memcache.so opcache.a
opcache.so [root@lnmp no-debug-non-zts-20131226]# php -m | grep
memcache [root@lnmp no-debug-non-zts-20131226]# cd
/usr/local/lnmp/php/etc/ [root@lnmp etc]# vim php.ini 873
extensi 3ff7 on=memcache.so [root@lnmp etc]# /etc/init.d/php-fpm reload
Reload service php-fpm done [root@lnmp etc]# php -m | grep memcache
memcache

  1. 安装memcache服务端

[root@lnmp ~]# yum install memcached -y [root@lnmp ~]#
/etc/init.d/memcached start Starting memcached:
[ OK ] [root@lnmp ~]# netstat -tnlp | grep memcached tcp 0
0 0.0.0.0:11211 0.0.0.0:* LISTEN
4389/memcached tcp 0 0 :::11211
::😗 LISTEN 4389/memcached

  • 测试
    [root@lnmp ~]# telnet localhost 11211
    set name 0 0 6
    westos
    STORED
    get name
    VALUE name 0 6
    westos
    END
    quit
    Connection closed by foreign host.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: