我用的环境是win7 64位的操作系统,wampserver也是64位,版本是2.5的,其中带的php版本是5.5的。
配置memcache的步骤:
第一步,先下载memcahed.exe和php_memcache.dll扩展。下载地址:http://pan.baidu.com/s/1jG8OREu
第二步,安装memcache和配置memcache扩展。把memcache.exe的压缩包解压到wampserver的bin目录下,和php的目录同级。
然后在dos中执行如图中的命令;注:下图中的路径换成你的memcached.exe所在的路径。
接下来配置php的mecahce扩展,将php_memcache.dll放到php的ext目录下,我的目录是D:\wamp\bin\php\php5.5.12\ext;
然后在php.ini中添加一行extension=php_memcache.dll,注意前面不要加分号。然后重启wampserver的服务。
注:因为在配置memcache的时候重启wampserver不报什么错误,我在phpinfo里面也没有检测到memcache。所以只能直接上代码进行测试,索性我运气不错,配置成功了。
下面是我进行测试的代码:
<span> 1</span> <?<span>php </span><span> 2</span> <span>/*</span><span>* </span><span> 3</span> <span> * 这里介绍PHP的memcache操作 </span><span> 4</span> <span> * </span><span> 5</span> <span> * @author lidaming<lidaming307@163.com> </span><span> 6</span> <span> * @copyright Copyright ? 2014-2015 Dajiangtai.com Inc. </span><span> 7</span> <span>*/</span> <span> 8</span> <span>//</span><span> 连接</span> <span> 9</span> <span>$mem</span> = <span>new</span><span> Memcache (); </span><span>10</span> <span>$mem</span>->connect ( "localhost", 11211 ); <span>//</span><span> 默认端口为11211 </span><span>11</span> <span> // 保存数据</span> <span>12</span> <span>$mem</span>->set ( 'course', 'php', 0, 60<span> ); </span><span>13</span> <span>$course</span> = <span>$mem</span>->get ( 'course'<span> ); </span><span>14</span> <span>echo</span> "First course is: " . <span>$course</span> . "<br>"<span>; </span><span>15</span> <span>//</span><span> 替换数据</span> <span>16</span> <span>$mem</span>->replace ( 'course', 'haddop', 0, 60<span> ); </span><span>17</span> <span>$course</span> = <span>$mem</span>->get ( 'course'<span> ); </span><span>18</span> <span>echo</span> "Second course is: " . <span>$course</span> . "<br>"<span>; </span><span>19</span> <span>//</span><span> 保存数组</span> <span>20</span> <span>$arr</span> = <span>array</span><span> ( </span><span>21</span> 'zhangsan', <span>22</span> 'lisi', <span>23</span> 'wangwu' <span>24</span> <span> ); </span><span>25</span> <span>$mem</span>->set ( 'user', <span>$arr</span>, 0, 60<span> ); </span><span>26</span> <span>$user</span> = <span>$mem</span>->get ( 'user'<span> ); </span><span>27</span> <span>echo</span> "user’s name is : "<span>; </span><span>28</span> <span>print_r</span> ( <span>$user</span><span> ); </span><span>29</span> <span>echo</span> "<br>"<span>; </span><span>30</span> <span>//</span><span> 删除数据</span> <span>31</span> <span>$mem</span>->delete ( 'course'<span> ); </span><span>32</span> <span>$course</span> = <span>$mem</span>->get ( 'course'<span> ); </span><span>33</span> <span>echo</span> "Now course is : " . <span>$course</span> . "<br>"<span>; </span><span>34</span> <span>//</span><span> 清除所有数据</span> <span>35</span> <span>$mem</span>-><span>flush</span><span> (); </span><span>36</span> <span>$user</span> = <span>$mem</span>->get ( 'user'<span> ); </span><span>37</span> <span>echo</span> "Now user’s name is: "<span>; </span><span>38</span> <span>print_r</span> ( <span>$user</span><span> ); </span><span>39</span> <span>echo</span> "<br>"<span>; </span><span>40</span> <span>//</span><span> 关闭连接</span> <span>41</span> <span>$mem</span>-><span>close (); </span><span>42</span> ?>
输出:
到此配置成功!若有错误的地方,请看到的朋友指正。
在此再附上部分php操作memcache常用的函数:
Memcache::add 添加一个值,如果已经存在,则返回false
Memcache::addServer 添加一个可供使用的服务器地址
Memcache::close 关闭一个Memcache对象
Memcache::connect 创建一个Memcache对象
memcache_debug 控制调试功能
Memcache::decrement 对保存的某个key中的值进行减法操作
Memcache::delete 删除一个key值
Memcache::flush 清除所有缓存的数据
Memcache::get 获取一个key值
Memcache::getExtendedStats 获取进程池中所有进程的运行系统统计
Memcache::getServerStatus 获取运行服务器的参数
Memcache::getStats 返回服务器的一些运行统计信息
Memcache::getVersion 返回运行的Memcache的版本信息
Memcache::increment 对保存的某个key中的值进行加法操作
Memcache::pconnect 创建一个Memcache的持久连接对象
Memcache::replace 对一个已有的key进行覆写操作
Memcache::set 添加一个值,如果已经存在,则覆写
Memcache::setCompressThreshold 对大于某一大小的数据进行压缩
Memcache::setServerParams 在运行时修改服务器的参数