当前位置:主页 > 服务器 > Nginx >

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

时间:2021-05-10 08:46:57 | 栏目:Nginx | 点击:

本文实例讲述了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 libevent
lrwxrwxrwx  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.2
lrwxrwxrwx  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.a
lrwxrwxrwx  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.la
lrwxrwxrwx  1 root root    26 07-21 03:33 libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx  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.la
lrwxrwxrwx  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.la
lrwxrwxrwx  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 value
Delete 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 value
Fetch key:key3 data:This is c third value
Fetch key:key1 data:This is c first value
Delete key1 success
Delete key2 success
Delete key3 success

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

7. 查看Memcache进程

[root@localhost html]# ps aux | grep memcached
root   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.pid
root   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缓存程序设计有所帮助。

您可能感兴趣的文章:

相关文章