memcache – 苏demo的别样人生 https://www.libaocai.com 行走于凡尘俗世,活得别样人生 Fri, 15 Jul 2016 03:28:01 +0000 zh-CN hourly 1 https://wordpress.org/?v=6.2.4 在windows上编译memcached v1.4.24 (用cygwin) https://www.libaocai.com/3353.html https://www.libaocai.com/3353.html#respond Sun, 03 May 2015 05:07:49 +0000 http://www.libaocai.com/?p=3353 编译环境

cygwin x86 当前最新 win7 x64 sp1

成品的依赖关系(ldd输出)

$ ldd /usr/local/bin/memcached
  ntdll.dll => /cygdrive/c/Windows/SysWOW64/ntdll.dll (0x77780000)
  kernel32.dll => /cygdrive/c/Windows/syswow64/kernel32.dll (0x76fa0000)
  KERNELBASE.dll => /cygdrive/c/Windows/syswow64/KERNELBASE.dll (0x76b30000)
  ADVAPI32.DLL => /cygdrive/c/Windows/syswow64/ADVAPI32.DLL (0x75770000)
  msvcrt.dll => /cygdrive/c/Windows/syswow64/msvcrt.dll (0x75810000)
  sechost.dll => /cygdrive/c/Windows/SysWOW64/sechost.dll (0x769e0000)
  RPCRT4.dll => /cygdrive/c/Windows/syswow64/RPCRT4.dll (0x770d0000)
  SspiCli.dll => /cygdrive/c/Windows/syswow64/SspiCli.dll (0x75110000)
  CRYPTBASE.dll => /cygdrive/c/Windows/syswow64/CRYPTBASE.dll (0x75100000)
  cygwin1.dll => /usr/bin/cygwin1.dll (0x61000000)
  cyggcc_s-1.dll => /usr/bin/cyggcc_s-1.dll (0x6fdb0000)
  cygevent-2-0-5.dll => /usr/local/bin/cygevent-2-0-5.dll (0x63ec0000)

可以看到依赖了libevent

编译libevent

cd /tmp
wget https://sourceforge.net/projects/levent/files/libevent/libevent-2.0/libevent-2.0.22-stable.tar.gz
tar xf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure --prefix=/usr/local
make all
make  install

全程无异常通过

编译memcached

cd /tmp
wget wget http://memcached.org/latest
tar xf latest
cd memcached-1.4.24/
chmod 777 configure
./configure

configure 执行完毕后,需要修改Makefile,不然编译会失败

大概是326行,删掉-Werror, 结果如下

CFLAGS = -g -O2 -pthread -pthread -Wall -pedantic -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls

继续执行剩余的编译

make
make install

完成,启动一下

memcached -vv

输出一堆log,然后用telnet访问一下,正常,搞定.

转载请注明:苏demo的别样人生 » 在windows上编译memcached v1.4.24 (用cygwin)

]]>
https://www.libaocai.com/3353.html/feed 0
memcached 1.4.23 发布,集中式缓存系统 https://www.libaocai.com/3195.html https://www.libaocai.com/3195.html#respond Mon, 27 Apr 2015 01:30:47 +0000 http://www.libaocai.com/?p=3195 memcached 1.4.23 发布,此版本现已提供下载: http://www.memcached.org/files/memcached-1.4.23.tar.gz

此版本更新内容如下:

Bug 修复

  • spinlocks removed since they never seem to improve performance.

  • flush_all was not thread safe.

  • better handle items refcounted in tail by unlinking them from the LRU’s

新特性

重写了 memcached 的核心 LRU 算法:

  • global cache_lock is gone, LRU’s are now independently locked.

  • LRU’s are now split between HOT, WARM, and COLD LRU’s. New items enter the HOT LRU.

  • LRU updates only happen as items reach the bottom of an LRU. If active in HOT, stay in HOT, if active in WARM, stay in WARM. If active in COLD, move to WARM.

  • HOT/WARM each capped at 32% of memory available for that slab class. COLD is uncapped.

  • Items flow from HOT/WARM into COLD.

  • A background thread exists which shuffles items between/within the LRU’s as capacities are reached.

主要目标是保护“scanning”的 active items,其次是为了改进延迟。

更多改进内容请看 发行说明

memcached是一套分布式的快取系统,当初是Danga Interactive为了LiveJournal所发展的,但目前被许多软件(如MediaWiki)所使用。这是一套开放源代码软件,以BSD license授权释出。

memcached缺乏认证以及安全管制,这代表应该将memcached服务器放置在防火墙后。

memcached 的API使用三十二位元的循环冗余校验(CRC-32)计算键值后,将资料分散在不同的机器上。当表格满了以后,接下来新增的资料会以LRU机制替换掉。 由于memcached通常只是当作快取系统使用,所以使用memcached的应用程式在写回较慢的系统时(像是后端的数据库)需要额外的程式码更新 memcached内的资料。

memcached具有多种语言的客户端开发包,包括:Perl/PHP/JAVA/C/Python/Ruby/C#/MySQL/

客户端包的下载地址是: http://code.google.com/p/memcached/wiki/Clients

转载请注明:苏demo的别样人生 » memcached 1.4.23 发布,集中式缓存系统

]]>
https://www.libaocai.com/3195.html/feed 0
开启了memcache怎么没有内存清理按钮 https://www.libaocai.com/1355.html https://www.libaocai.com/1355.html#respond Sun, 04 Jan 2015 06:49:27 +0000 http://www.libaocai.com/?p=1355 配置config中的IP地址为127.0.0.1,端口使用实际使用的memcached监听的端口

开启了memcache怎么没有内存清理按钮。

ssh里面执行,让其启动

/usr/local/bin/memcached -d -u www -m 128

转载请注明:苏demo的别样人生 » 开启了memcache怎么没有内存清理按钮

]]>
https://www.libaocai.com/1355.html/feed 0
PHP-MemCache部署 linux https://www.libaocai.com/1337.html https://www.libaocai.com/1337.html#respond Sun, 04 Jan 2015 06:43:49 +0000 http://www.libaocai.com/?p=1337 Memcache是一个linux下面高速缓存的程序,可以大大提速动态程序的运行。

需要安装以下三个程序
Memcached memcached-1.2.4.tar.gz
Libevent    libevent-1.3e.tar.gz
Php-memcache memcache-2[1].2.3.tar.gz
一、memcached安装
首先是下载memcached了,目前最新版本是1.1.12,直接从官方网站即可下载到memcached-1.2.4.tar.gz。除此之外,memcached用到了libevent,我下载的是libevent-1.3e.tar.gz。
接下来是分别将libevent-1.3e.tar.gz和memcached-1.2.4.tar.gz解开包、编译、安装:

# tar zxvf libevent-1.3e.tar.gz


# cd libevent-1.3e
# ./configure --prefix=/usr
# make
# make install

测试libevent是否安装成功
# ls -al /usr/lib | grep libevent


# tar zxvf memcached-1.2.4.tar.gz
# cd memcached-1.2.4
# ./configure --prefix=/usr
# make
# make install

测试memcached是否安装成功
# ls -al /usr/bin/mem*

 

安装完成之后,memcached应该在/usr/bin/memcached
运行memcached守护进程:
/usr/bin/memcached -d -m 128 -l localhost -p 11211 -u root –c 526 –P/tmp/memcached.pid
 -d以守护程序(daemon)方式运行memcached;
 -m设置memcached可以使用的内存大小,单位为M;
 -l设置监听的IP地址,如果是本机的话,通常可以不设置此参数;
 -p设置监听的端口,默认为11211,所以也可以不设置此参数;
 -u指定用户;
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
 -P是设置保存Memcache的pid文件,我这里是保存在/tmp/memcached.pid,
如果遇到运行memcached时遇到问题,错误提示如下:
/usr/bin/memcached/bin/memcached: error while loading shared libraries: libevent-1.3e.so.1: cannot open shared object file: No such file or directory   
便运行以下命令
# LD_DEBUG=libs /usr/bin/memcached –v
然后给libevent-1.3d.so.1建立一个链接:
# ln -s /usr/lib/libevent-1.3e.so.1 /lib64/libevent-1.3e.so.1
二、php-memcache安装
# tar zxvfmemcache-2[1].2.3.tar.gz
# /usr/bin/phpize
# ./configure –enable-memcache –with-zlib-dir
# make
# make install
在php.ini里加入extension=memcache.so
在/etc/rc.local每次启动的时候自动开启memcache
/usr/bin/memcached -d -u apache -m 1024 -c 8192 -l 192.168.20.120 -p 11211 –P /tmp/memcached.pid
/usr/bin/memcached -d -u apache -m 1024 -c 8192 -l 192.168.20.120 -p 11212
关掉memcached的进程
# kill ‘cat /tmp/memcached.pid’
<?php
$mem = new Memcache;
$mem->connect(“127.0.0.1”, 11211)or die (“Could not connect”);
$mem->set(‘key’, ‘This is a test!’, 0, 60);
$val = $mem->get(‘key’);
echo $val;

?>
PS:安装完成后,可以使用phpinfo查看是否配置成功。

转载请注明:苏demo的别样人生 » PHP-MemCache部署 linux

]]>
https://www.libaocai.com/1337.html/feed 0
Win 下面配置 memcache https://www.libaocai.com/1335.html https://www.libaocai.com/1335.html#respond Sun, 04 Jan 2015 06:43:27 +0000 http://www.libaocai.com/?p=1335 开发环境win下面配置使用Memcached方法概述

再简单的事情没有做一遍都不能明白其中时候如此,今天配置Memcached就发现这个问题。帮助很全,先是在memcached for Windows获取到了需要的win下面的Memcached,按照方法:

引用

Unzip the binaries in your desired directory (eg. c:memcached)
Install the service using the command: 'c:memcachedmemcached.exe -d install' from the command line
Start the server from the Microsoft Management Console or by running the following command: 'c:memcachedmemcached.exe -d start'
Use the server, by default listening to port 11211

然后

在php.ini 加入一行 ‘extension=php_memcache.dll’

然后到 http://pecl4win.php.net/ext.php/php_memcache.dll获取php_memcache.dll

并复制到 ext 中(记住版本不要错了!)

重启Apache,发现PHPInfo就是提示出不来memcache,真是无语了,代码测试总是提示

引用

Fatal error: Class ‘Memcache’ not found in D:xampplitehtdocsmemcacheindex.php on line 20

开始觉得奇怪,于是搜索在官方网站发现了http://www.php.net/manual/zh/ref.memcache.php

Hi there:
For run memcached in a windows box: (tested with latest php,apache and memcache in xp sp2)
a) download the php_memcache.dll it can be found in the pecl file.
b) put the dll in the extension folder (c:/php/extension for example). You cannot miss this folder because they are filled with php*.dll files. In some cases the extension folder used is the system32, a non-standard way to put dll but still works.
c)configure the php.ini
; i put this like the latest extension
extension=php_memcache.dll
; i'm not sure about this but don't hurts..
[Memcache]
memcache.allow_failover = 1
memcache.max_failover_attempts=20
memcache.chunk_size =8192
memcache.default_port = 11211
d)This is important, memcached works with a EXTERNAL service. This service must be downloaded and installed prior to use the memcache. I use: http://jehiah.cz/projects/memcached-win32/
e)Remember to install the service and to start the service memcached.exe -d install for install and run services.msc for start the memcached service (or restart the system).
f) check the firewall ports.
Finally restart the apache/iis and runs some test. At least in phpinfo must show some info about the memcache.
Final notes :The "awe" about memcache is not only can help for speed some process (or reduce the cpu use), also can be used like a global session for store a whole object also this "global session" is shared among all the users, like APPLICATION used in ASP. So (for example) it's possible to do a user counter without needing of database or writing a file.

试试态度加上看了一下,看到了熟悉的东西了:

view plaincopy to clipboardprint?

$mem=newMemcache;  

$mem->connect('127.0.0.1',11211);  

 

$mem->set('key','Thisisatest!',0,60);  

 

$val=$mem->get('key');  

echo$val;

$mem = new Memcache;
$mem->connect('127.0.0.1', 11211);
$mem->set('key', 'This is a test!', 0, 60);
$val = $mem->get('key');
echo $val;

在浏览器输出了

This is a test!

另外作为服务器开发者,自然很关注的一个问题就是这个Memcached的原理问题,在老农如是想,如是说,如是为博客中提到:

引用

Memcached 本身的启动过程,在 memcached.c 的 main 函数中顺序如下:

1 、调用 settings_init() 设定初始化参数

2 、从启动命令中读取参数来设置 setting 值

3 、设定 LIMIT 参数

4 、开始网络 socket 监听(如果非 socketpath 存在)( 1.2 之后支持 UDP 方式)

5 、检查用户身份( Memcached 不允许 root 身份启动)

6 、如果有 socketpath 存在,开启 UNIX 本地连接(Sock 管道)

7 、如果以 -d 方式启动,创建守护进程(如上调用 daemon 函数)

8 、初始化 item 、 event 、状态信息、 hash 、连接、 slab

9 、如设置中 managed 生效,创建 bucket 数组

10 、检查是否需要锁定内存页

11 、初始化信号、连接、删除队列

 

12 、如果 daemon 方式,处理进程 ID

13 、event 开始,启动过程结束, main 函数进入循环。

转载请注明:苏demo的别样人生 » Win 下面配置 memcache

]]>
https://www.libaocai.com/1335.html/feed 0
php的memcache配置 https://www.libaocai.com/1333.html https://www.libaocai.com/1333.html#respond Sun, 04 Jan 2015 06:43:07 +0000 http://www.libaocai.com/?p=1333 linux下的Memcache安装:

1. 下载 memcache的linux版本,注意 memcached 用 libevent 来作事件驱动,所以要先安装有 libevent。
2. 安装 pecl::memcache。

用 pecl 命令行工具安装:
pecl install memcache

或直接从源码安装:
phpize
./configure
make
make install

Windows下的Memcache安装:

1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:/memcached
2. 在终端(也即cmd命令界面)下输入 ‘c:/memcached/memcached.exe -d install’ 安装
3. 再输入: ‘c:/memcached/memcached.exe -d start’ 启动。NOTE: 以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。
4.下载php_memcache.dll,请自己查找对应的php版本的文件
5. 在C:/winnt/php.ini 加入一行 ‘extension=php_memcache.dll’
6.重新启动Apache,然后查看一下phpinfo,如果有memcache,那么就说明安装成功!

memcached的基本设置:

-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25-n 最小分配空间,key+value+flags默认是48
-h 显示帮助

php.ini中的配置:

[Memcache]

; 一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,
; 它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。

; 是否在遇到错误时透明地向其他服务器进行故障转移。
memcache.allow_failover = On

; 接受和发送数据时最多尝试多少个服务器,只在打开memcache.allow_failover时有效。memcache.max_failover_attempts = 20

; 数据将按照此值设定的块大小进行转移。此值越小所需的额外网络传输越多。
; 如果发现无法解释的速度降低,可以尝试将此值增加到32768。
memcache.chunk_size = 8192

; 连接到memcached服务器时使用的默认TCP端口。
memcache.default_port = 11211

; 控制将key映射到server的策略。默认值”standard”表示使用先前版本的老hash策略。
; 设为”consistent”可以允许在连接池中添加/删除服务器时不必重新计算key与server之间的映射关系。
;memcache.hash_strategy = “standard”; 控制将key映射到server的散列函数。默认值”crc32″使用CRC32算法,而”fnv”则表示使用FNV-1a算法。
; FNV-1a比CRC32速度稍低,但是散列效果更好。
;memcache.hash_function = “crc32”

;memcache也可以作为session的存储模块,具体参看:memcache PHP 的 session.save_handler.

memcache的测试代码:

  1. $memcache = new Memcache;
  2. $memcache->connect(‘localhost’, 11211) or die (“Could not connect”);
  3. $version = $memcache->getVersion();
  4. echo “Server’s version: “.$version.”<br />/n”;
  5. $tmp_object = new stdClass;
  6. $tmp_object->str_attr = ‘test’;
  7. $tmp_object->int_attr = 123;
  8. $memcache->set(‘key’, $tmp_object, false, 10) or die (“Failed to save data at the server”);
  9. echo “Store data in the cache (data will expire in 10 seconds)<br />/n”;
  10. $get_result = $memcache->get(‘key’);
  11. echo “Data from the cache:<br />/n”;
  12. var_dump($get_result);

转载请注明:苏demo的别样人生 » php的memcache配置

]]>
https://www.libaocai.com/1333.html/feed 0
使用Memcache缓存MySQL查询 https://www.libaocai.com/1256.html https://www.libaocai.com/1256.html#respond Sun, 04 Jan 2015 06:21:48 +0000 http://www.libaocai.com/?p=1256 实际应用中,尤其是在大规模访问的Web项目中,Memcache作为一种优秀的缓存解决方案,被广泛使用。其灵活性和可扩展性受到了广大开发者的青睐。为了提高响应速度,使用Memcache来减少数据查询运算是一种不错的选择。

     关于Memcache的安装和配置,网上有很多资料,这里就不再赘述。本文以笔者在实际项目中的应用为例,来说明如何使用Memcache缓存MySQL数据,从而达到高效数据响应的目的。

     需求背景:网站有一个图片放映功能,页面加载时需从数据库中查询管理员所提交放映的图片信息,图片放映区共有9张图片,对应在数据库表中有9条记录。每次用户访问网站时,需查询数据库,并显示图片。当承受大规模用户同时访问时,数据库的压力可想而之。

      为了降低数据库压力,提高查询速度,我采用了Memcache来将图片记录信息缓存到Memcache服务器上。这样就将数据库的查询压力分散到了各缓存服务器,提高了网站访问的整体效率。

    一、Memcache 服务器配置(内部集群模拟)

 $MEMCACHE_SERVERS[] = ‘192.168.1.78:11211’;
$MEMCACHE_SERVERS[] = ‘192.168.1.78:11212’;
$MEMCACHE_SERVERS[] = ‘192.168.1.252:11211’;
$MEMCACHE_SERVERS[] = ‘192.168.1.252:11212’;
 //如果要添加Memcache节点,就在此处添加即可。
 
 $MEMCACHE_LIFETIME = 6000;   // 缓存数据生命周期

   我采用了两台机器作为Memcache Server,每台开启两个端口提供Memcache服务,每个服务开启服务内存为1G。这样,缓存空间总的有4G。

  二、php连接Memcache服务器

  php中和Memcache的交互有两种:1、采用php下的memcache扩展;2、采用libmemcache组件。两者的优劣在此先不作对比。

$_SGLOBAL[‘memcache’] = new memcache;
global $MEMCACHE_SERVERS;
$result = array();
foreach ($MEMCACHE_SERVERS as $server){
       $serverstr = explode(‘:’,$server);
$host = $serverstr[0];
$port = $serverstr[1];
     
       $_SGLOBAL[‘memcache’]->addServer($host,$port);
}

这段程序从刚刚的memcache服务器配置文件中读取所有的server列表,并创立连接。

  三、php实现memcache和mysql的缓存查询

  实现原理其实很简单,在我的数据库操作类中新增一个函数query_memcache($sql,$type),以$sql的MD5值作为Key,从 Memcache服务器上查询是否存在该key的值,如果存在,则直接返回;如果不存在,则从MySQL数据库中查询,并将结果写入Memcache。

 function query_memcache($sql,$type=”){
 $key = md5($sql);
 
 if(!($value = $_SGLOBAL[‘memcache’]->get($key))){ //Cache中没有,则从My SQL中查询
  $query = $this->query($sql,$type);
  while($item = $this->fetch_array($query)){
   $result[] = $item;
  }
  $value = $result;
  
  //将Key和Value写入MemCache
  $_SGLOBAL[‘memcache’]->set($key,$result,0,$MEMCACHE_LIFETIME);
 }
 return $value;
}

在页面加载的过程中,将图片检索的数据库查询调用改成用query_memcache()函数即可。

转载请注明:苏demo的别样人生 » 使用Memcache缓存MySQL查询

]]>
https://www.libaocai.com/1256.html/feed 0
用memcache.php监测memcache的状况 https://www.libaocai.com/1209.html https://www.libaocai.com/1209.html#respond Sun, 04 Jan 2015 06:08:27 +0000 http://www.libaocai.com/?p=1209 最新的memcache pecl中,新增了一个memcache.php,这个php文件可以用来方便的查看memcache的状况,界面上与apc自带的apc.php风格一致。

应该算是最方便的监测memcache的办法了。

memcache

在浏览器地址栏里访问,

默认账户

memcache

密码 password

转载请注明:苏demo的别样人生 » 用memcache.php监测memcache的状况

]]>
https://www.libaocai.com/1209.html/feed 0
memcache安装及测试 on windows https://www.libaocai.com/1199.html https://www.libaocai.com/1199.html#respond Sun, 04 Jan 2015 06:06:10 +0000 http://www.libaocai.com/?p=1199 版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。http://blog.csdn.net/mayongzhan – 马永占,myz,mayongzhan

Memcache是danga.com的一个项目,下面是具体的项目内容
http://www.danga.com/memcached/
——————————————————-
服务端安装
memcache windows版本服务端下载:
http://jehiah.cz/projects/memcached-win32/
服务端解压到某位置
然后cmd -> 执行 memecache.exe -d install
memcache.exe -d start
其他命令见下面
服务端去查看一下windows的服务,看有没有memcache
——————————————————-
客户端安装
php memcache dll下载地址:
http://pecl4win.php.net/list.php
将php_memcache.dll放到php文件夹的ext中
php.ini中添加extension那部分 extension=php_memcache.dll
php查看是否有memcache
<?php
phpinfo();
?>
——————————————————-
测试
注释的东西反复折腾几次熟悉一下memcache~,其他memcache方法见下面
<?php
header(“Content-type: text/html; charset=utf-8”);
$mem = new Memcache();
$mem->connect(‘127.0.0.1’);
//$mem->set(‘key’, ‘This is a test!’, 0, 60);
$val = $mem->get(‘key’);
//$mem->flush();
echo $val;
?>
——————————————————-
memecache服务端命令
-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务 (在windows下同时加入自启动中)
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助
——————————————————-
php中memcache操作方法
Memcache::add — Add an item to the server
Memcache::addServer — Add a memcached server to connection pool
Memcache::close — Close memcached server connection
Memcache::connect — Open memcached server connection
memcache_debug — Turn debug output on/off
Memcache::decrement — Decrement item’s value
Memcache::delete — Delete item from the server
Memcache::flush — Flush all existing items at the server
Memcache::get — Retrieve item from the server
Memcache::getExtendedStats — Get statistics from all servers in pool
Memcache::getServerStatus — Returns server status
Memcache::getStats — Get statistics of the server
Memcache::getVersion — Return version of the server
Memcache::increment — Increment item’s value
Memcache::pconnect — Open memcached server persistent connection
Memcache::replace — Replace value of the existing item
Memcache::set — Store data at the server
Memcache::setCompressThreshold — Enable automatic compression of large values
Memcache::setServerParams — Changes server parameters and status at runtime

转载请注明:苏demo的别样人生 » memcache安装及测试 on windows

]]>
https://www.libaocai.com/1199.html/feed 0
windows下安装 memcache + php https://www.libaocai.com/1193.html https://www.libaocai.com/1193.html#respond Sun, 04 Jan 2015 06:03:48 +0000 http://www.libaocai.com/?p=1193 1.memcache是什么

Free & open source, 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 is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

Memcached is simple yet powerful . Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.

和其他的Cache一样(我用过 Java的OSCache PHP的Smaty Cache和Symfony Cache),思路都是把动态网站经常被访问的资源,或者数据量很大的资源保存到内存中去,从而达到服务器负荷降低,响应加快的目的。不过 Memcache似乎更加通用,他不是莫一种BS架构的插件,而是以Windows 服务或者是Linux 守护进程那样,以平行的方式提供API给服务器的其他应用程序。Cache对象以key-value这种Map结构保存在内存,memocache有一套 特有的内存对象管理方式。详细运作原理可以参考Sina的Memcached深度分析(原创)

 

memcached的基本设置:

-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助

2.Memcache主要用来做什么

用的最多的还是数据检索结果的缓存。因为现在服务器出现负荷过高的时候,多半和数据库瓶颈有关。对于那些80/20中的20%的特殊检索(特殊包 括:查询特别频繁,检索特别复杂,返回结果特别大),如果如何怎么tuning,怎么优化,在走投无路升级硬件之前,可以考虑考虑memcache这一类 的cache工具了。当然咯, 信息的刷新间隔和效率优化时反比的。对于那些一定要实时刷新的数据,使用Cache只会是搬起石头砸自己的脚。

另外,还有一个DB服务器侧的技术,就是SQL的Cache,比如Mysql Query Cache。这种技术对于那种频繁访问,且发行SQL是一样的query,是个很不错的选择。可以综合起来使用。

3.Windows下如何安装

PHP的官方网站有详细的说明,一个 pecl install memcache就可以一键搞定。可惜的是,Windows下不行,总会报

ERROR: Did not understand the completion status returned from msdev.exe.

这个错。

估计原因主要发生在编译系统上。Linux上的gcc是默认的,windows下没有相应的编译器所以出错。

还是有好人的,辛辛苦苦地把memcache编译成exe贡献给大家。

安装流程

3.1 下载windows版的memcache

http://code.jellycan.com/memcached/ 直接下载windows版本

上面已经是最新的稳定版啦,如果想要自己编译的,也可以看看他是怎么一步一步做的。

3.2 安装服务

  • 下载的zip解压缩到一个目录(G:\lib\memcached-1.2.6)
  • 如果是win7,请把memcached.exe的执行权限设为管理员
  • 安装服务  G:\lib\memcached-1.2.6>memcached.exe -d install
  • 启动服务 G:\lib\memcached-1.2.6>memcached.exe -d start

这个时候可以看看系统的服务,应该增加了一个memcached的服务

3.3 设定PHP的extention

[PHP_MEMCACHE]
extension=php_memcache.dll

  • 重启apache

3.4 测试程序

01 <?php
02 $memcache = new Memcache;
03 $memcache ->connect( "localhost" ,11211); #根据情况要把"localhost" 改为 "127.0.0.1"
04
05 echo "Server's version: " . $memcache ->getVersion() . "<br />\n" ;
06
07 $tmp_object = new stdClass;
08 $tmp_object ->str_attr = "test" ;
09 $tmp_object ->int_attr = 123;
10
11 $memcache ->set( "key" , $tmp_object ,false,10);
12 echo "Store data in the cache (data will expire in 10 seconds)<br />\n" ;
13
14 echo "Data from the cache:<br />\n" ;
15 var_dump( $memcache ->get( "key" ));
16 ?>

不出错,就说明大功告成了。

原文: http://www.infony.co.cc/?p=322

转载请注明:苏demo的别样人生 » windows下安装 memcache + php

]]>
https://www.libaocai.com/1193.html/feed 0