memcache在tp3.2中的使用

2025-11-10 11:17:07

1、先看下服务是否开启来了,如何安装和开启memcached请看我前面一篇经验。

memcache在tp3.2中的使用

2、在web目录下新建phpinfo.php内容

<?php phpinfo();

然后在浏览器中输入localhost/phpinfo.php访问

查找下看是否已经安装好memcache扩展了。

memcache在tp3.2中的使用

3、在tp3.2的配置文件中config.php 添加

    'DATA_CACHE_TIME'        => 1800,        // 数据缓存有效期s

    'DATA_CACHE_TYPE' => 'Memcache',   //数据缓存类型

    'MEMCACHE_HOST'   => 'tcp://127.0.0.1:11211',

memcache在tp3.2中的使用

4、memcache的实例化,设置内容和获取内容方式一

在IndexController的index方法中加入以下内容做个测试

//连接

$mem = new \Think\Cache\Driver\Memcache;

$mem->set('key1', 'This is first value', 0, 60);

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

echo "Get key1 value: " . $val ."<br />";

$class = new \Think\Cache\Driver\Memcache();  

$class->set('key','1234');  

$data = $class->get('key');  

echo $data;  

memcache在tp3.2中的使用

5、memcache设置内容和获取内容方式二,调用S方法。

在IndexController的index方法中加入以下内容做个测试

S('test','hello memcache');

$mem_test = S('test');

echo $mem_test; //输出hello memcache表示成功

memcache在tp3.2中的使用

6、效果展示。

memcache在tp3.2中的使用

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
相关推荐
  • 阅读量:28
  • 阅读量:44
  • 阅读量:137
  • 阅读量:32
  • 阅读量:124
  • 猜你喜欢