侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Memcached缓存系统的介绍、安装以及应用方法详解

技术  /  管理员 发布于 7年前   171

本文实例讲述了Memcached缓存系统的介绍、安装以及应用方法。分享给大家供大家参考,具体如下:

一. memcached 是什么?

memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

memcached是一个高性能的、分布式内存对象缓存系统,应用广泛。 通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度、 提高可扩展性。

它可以应对任意多个连接,使用非阻塞的网络IO。由于它的工作机制是在内存中开辟一块空间,然后建立一个HashTable,Memcached自管理这些HashTable。还使用内置的内存块分配和哈希表算法,确保虚拟内存不会过来捣乱。

Memcached 官方网站:http://www.danga.com/memcached

二. memcached 的安装:

注:memcached 用到了libevent这个库用于Socket的处理,所以还需要安装libevent.官网:http://www.monkey.org/~provos/libevent/

1. 先安装libevent:

[root@localhost software]# tar zxvf libevent-1.4.11-stable.tar.gz[root@localhost libevent-1.4.11-stable]# ./configure Cprefix=/usr[root@localhost libevent-1.4.11-stable]# make[root@localhost libevent-1.4.11-stable]# make install

2. 测试libevent是否安装成功

[root@localhost libevent-1.4.11-stable]# ls -al /usr/lib | grep libeventlrwxrwxrwx  1 root root    22 07-10 13:10 libevent-1.1a.so.1 -> libevent-1.1a.so.1.0.2-rwxr-xr-x  1 root root  31596 2007-01-07 libevent-1.1a.so.1.0.2lrwxrwxrwx  1 root root    21 07-21 03:33 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3-rwxr-xr-x  1 root root  308088 07-21 03:33 libevent-1.4.so.2.1.3-rw-r--r--  1 root root  394474 07-21 03:33 libevent.alrwxrwxrwx  1 root root    26 07-21 03:33 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3-rwxr-xr-x  1 root root  109490 07-21 03:33 libevent_core-1.4.so.2.1.3-rw-r--r--  1 root root  148742 07-21 03:33 libevent_core.a-rwxr-xr-x  1 root root   866 07-21 03:33 libevent_core.lalrwxrwxrwx  1 root root    26 07-21 03:33 libevent_core.so -> libevent_core-1.4.so.2.1.3lrwxrwxrwx  1 root root    27 07-21 03:33 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.1.3-rwxr-xr-x  1 root root  246870 07-21 03:33 libevent_extra-1.4.so.2.1.3-rw-r--r--  1 root root  307370 07-21 03:33 libevent_extra.a-rwxr-xr-x  1 root root   873 07-21 03:33 libevent_extra.lalrwxrwxrwx  1 root root    27 07-21 03:33 libevent_extra.so -> libevent_extra-1.4.so.2.1.3-rwxr-xr-x  1 root root   831 07-21 03:33 libevent.lalrwxrwxrwx  1 root root    21 07-21 03:33 libevent.so -> libevent-1.4.so.2.1.3

安装OK。

3. 安装memcached,同时需要安装中指定libevent的安装位置

[root@localhost software]# tar zxvf memcached-1.4.0.tar.gz[root@localhost memcached-1.4.0]# ./configure Cwith-libevent=/usr[root@localhost memcached-1.4.0]# make[root@localhost memcached-1.4.0]# make intall

4. 测试是否成功安装memcached

[root@localhost memcached-1.4.0]# ls -al /usr/local/bin | grep memcached-rwxr-xr-x 1 root root 188225 07-21 03:35 memcached

安装OK。

三. 如何启动 memcached 服务:

只需要启动一个 memcached 监护进程,监护进程不需要配置文件,只要在命令行里面加三四个参数就可以了:
复制代码 代码如下:[root@localhost bin]# memcached -d -m 100 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid

-d: (run as a daemon) 选项是启动一个守护进程
-m:(max memory to use for items in megabytes (default: 64 MB))是分配给Memcache使用的内存数量,单位是MB,我这里是100MB,
-u:(assume identity of <username> (only when run as root))是运行Memcache的用户,我这里是root,
-l:(interface to listen on)是监听的服务器IP地址,如果有多个地址的话,这里指定了服务器的IP地址127.0.0.1,
-p:是设置Memcache监听的端口,这里设置了11211,最好是1024以上的端口,
-c:选项是最大运行的并发连接数,默认是1024,这里设置了256,根据服务器的负载量来设定,
-P:(save PID in <file>, only used with -d option)是设置保存Memcache的pid文件,这里是保存在 /tmp/memcached.pid

注:也可以启动多个守护进程,不过端口不能重复。

四. 安装 Memcached 的PHP扩展:

在PHP中使用Memcached,有两种方式:

一种是安装PHP的memcached扩展。该扩展是用c写的,效率较高,需要在服务器上安装。

另外一种则是直接使用客户端的php-memcached-client类库。

下面是使用PECL中Memcache的专用扩展,因为毕竟是用C写的,效率高,而且安装部署起来也比较方便。

1. 在 http://pecl.php.net/package/memcache 选择相应想要下载的memcache版本。我下载的是:memcache-2.2.5.tgz 版本。

2. 安装 memcache

[root@localhost software]# tar zxvf memcache-2.2.5.tgz[root@localhost software]# cd memcache-2.2.5[root@localhost memcache-2.2.5]# /usr/bin/phpize[root@localhost memcache-2.2.5]# ./configure Cenable-memcache Cwith-php-config=/usr/bin/php-config Cwith-zlib-dir[root@localhost memcache-2.2.5]# make[root@localhost memcache-2.2.5]# make install

这步会有类似这样的提示:Installing shared extensions: /usr/local/php/modules

3. 把/etc/php.ini中的

extension_dir = "./"

修改为:

extension_dir = "/usr/lib/php/modules"

4. 并添加: extension=memcache.so

也可执行以下shell命令,对php.ini文件的修改:
复制代码 代码如下:sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"/nextension = "memcache.so"/n#' /usr/local/webserver/php/etc/php.ini

五. 安装C/C++ Memcached客户端库:libmemcached

下载:http://download.tangent.org/libmemcached-0.32.tar.gz

1. 安装 libmemcached

[root@localhost src]# tar zxvf libmemcached-0.32.tar.gz[root@localhost src]# cd libmemcached-0.32[root@localhost libmemcached-0.32]# ./configure --prefix=/usr[root@localhost libmemcached-0.32]# make && make install

2. 检查安装结果

[root@localhost src]# ls /usr/lib/libmemcache* //库文件[root@localhost src]# ls /usr/include/libmemcached/* //头文件[root@localhost src]# ls /usr/bin/mem* //命令行工具

六. 应用:

1. 启动 memcache 服务
复制代码 代码如下:[root@localhost bin]# memcached -d -m 100 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid

2. 重启 Web 服务器

[root@localhost bin]# service httpd restart

3. 创建 demo 测试程序

<?php//连接$mem = new Memcache;$mem->connect("127.0.0.1", 11211);echo 'Memcache Version is:'.$mem->getVersion().'<br/>';//保存数据$mem->set('key1', 'This is first memcache demo', 0, 60);$val = $mem->get('key1');echo "Get key1 value: ".$val."<br/>"//关闭连接$mem->close();?>

4. Memcached客户端与tokyotyrant DB操作访问

<?php$memcache = new memcache();$memcache->addServer('127.0.0.1:1978');function microtime_float(){  list($usec, $sec) = explode(" ", microtime());  return ((float)$usec + (float)$sec);}$time_start = microtime_float();$data = 'abc123';for($i = 0;$i <= 20000 ;$i++){  $key = (string) rand(1,100);  $memcache->set($key, $data);}for($i = 0;$i <= 20000 ;$i++){  $key = (string) rand(1,100);  echo $data = $memcache->get($key).'-'.$i.'<br/>';}$time_end = microtime_float();$time = $time_end - $time_start;echo $time;$memcache->close();?>

5. C/C++ 与 Memcached 结合代码

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <libmemcached/memcached.h>int main(int argc, char *argv[]){  memcached_st *memc;  memcached_return rc;  memcached_server_st *servers;  char value[8191];  //连接服务器  memc = memcached_create(NULL);  servers = memcached_server_list_append(NULL, "127.0.0.1",11211, &rc);  rc = memcached_server_push(memc, servers);  memcached_server_free(servers);  //存储数据  strcpy(value, "This is c first value");  rc = memcached_set(memc, "key1", 4, value, strlen(value),  (time_t)180, (uint32_t)0);  if (rc == MEMCACHED_SUCCESS) {  printf("Save key:key1 data:/"%s/" success./n", value);  }  //获取数据  char return_key[MEMCACHED_MAX_KEY];  size_t return_key_length;  char *return_value;  size_t return_value_length;  char *keys[]= {"key1"};  size_t key_length[]= {4};  uint32_t flags;  rc = memcached_mget(memc, keys, key_length, 1);  return_value = memcached_fetch(memc, return_key,  &return_key_length, &return_value_length, &flags, &rc);  if (rc == MEMCACHED_SUCCESS) {  printf("Fetch key:%s data:%s/n", return_key, return_value);  }  //删除数据  rc = memcached_delete(memc, "key1", 4, (time_t)0);  if (rc == MEMCACHED_SUCCESS) {  printf("Delete Key key1 success./n");  }  //释放内存  memcached_free(memc);  return 0;}

编译源代码:

[root@localhost html]# gcc -o cmem cmem.c -lmemcached[root@localhost html]# ./cmem //执行Save key:key1 data:"This is c first value" success.Fetch key:key1 data:This is c first valueDelete Key key1 success.

6. C/C++ 与 Memcached 分布式结合代码

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <libmemcached/memcached.h>int main(int argc, char *argv[]){  memcached_st *memc;  memcached_return rc;  memcached_server_st *servers;  char value[8191];  //connect multi server  memc = memcached_create(NULL);  servers = memcached_server_list_append(NULL, "localhost", 11211, &rc);  servers = memcached_server_list_append(servers, "localhost", 11212, &rc);  rc = memcached_server_push(memc, servers);  memcached_server_free(servers);  //Save multi data  size_t i;  char *keys[]= {"key1", "key2", "key3"};  size_t key_length[]= {4, 4, 4};  char *values[] = {"This is c first value", "This is c second value", "This is c third value"};  size_t val_length[]= {21, 22, 21};  for (i=0; i <3; i++) {  rc = memcached_set(memc, keys[i], key_length[i], values[i], val_length[i], (time_t)180,(uint32_t)0);  if (rc == MEMCACHED_SUCCESS) {    printf("Save key:%s data:/"%s/" success./n", keys[i], values[i]);  }  }  //Fetch multi data  char return_key[MEMCACHED_MAX_KEY];  size_t return_key_length;  char *return_value;  size_t return_value_length;  uint32_t flags;  rc = memcached_mget(memc, keys, key_length, 3);  while ((return_value = memcached_fetch(memc, return_key, &return_key_length, &return_value_length, &flags, &rc))) {  if (rc == MEMCACHED_SUCCESS) {    printf("Fetch key:%s data:%s/n", return_key, return_value);  }  }  //Delete multi data  for (i=0; i <3; i++) {  rc = memcached_set(memc, keys[i], key_length[i], values[i], val_length[i], (time_t)180, (uint32_t)0);  rc = memcached_delete(memc, keys[i], key_length[i], (time_t)0);  if (rc == MEMCACHED_SUCCESS) {    printf("Delete %s success/n", keys[i], values[i]);  }  }  //free  memcached_free(memc);  return 0;}

编译源代码:

[root@localhost html]# gcc -o cmultmem cmultmem.c -lmemcached[root@localhost html]# ./cmultmem //执行Save key:key1 data:"This is c first value" success.Save key:key2 data:"This is c second value" success.Save key:key3 data:"This is c third value" success.Fetch key:key2 data:This is c second valueFetch key:key3 data:This is c third valueFetch key:key1 data:This is c first valueDelete key1 successDelete key2 successDelete key3 success

以上c/c++代码摘自:黑夜路人

7. 查看Memcache进程

[root@localhost html]# ps aux | grep memcachedroot   11382 0.0 0.7 55124 1896 ?    Ssl 13:06  0:00 memcached -d -m 100 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pidroot   11395 0.0 0.2  3912  664 pts/1  R+  13:08  0:00 grep memcached

8. 结束Memcache进程

[root@localhost html]# kill `cat /tmp/memcached.pid`

希望本文所述对大家memcached缓存程序设计有所帮助。


  • 上一条:
    解决IIS中应用程序池提供服务的进程无法响应Ping或进程关闭时间超过了限制
    下一条:
    分页技术原理与实现之分页的意义及方法(一)
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    • 2025-07
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客