当前位置: 首页 > 文档资料 > DoitPHP 帮助文档 >

6.8 Memcached使用说明

优质
小牛编辑
125浏览
2023-12-01

DoitPHP扩展类CacheMemcached,用于memcached的数据操作。

类方法使用说明:

1、set($key, $data, $expire = null)

写入缓存。

参数说明:
$key : 缓存Key
$data : 缓存内容
$expire : 缓存时间(秒),默认:900秒。

2、get($key)

读取缓存,失败或缓存撒失效时返回false。

参数说明:
$key : 所要读取数据的key

3、delete($key)

删除指定的缓存。

参数说明:
$key : 所要删除数据的Key

4、increment($key, $value = 1)

数据自增。

参数说明:
$key : 数据key
$value : 自增数据值

5、decrement($key, $value = 1)

数据自减。

参数说明:
$key : 数据key
$value : 自减数据值

6、clear()

清除所有的缓存数据。

参数说明:
参数为空

7、getConnection()

返回memcached实例化对象。

参数说明:
参数为空

8、getInstance($params = null)

单例模式。 用于本类的单例模式(singleton)实例化。

参数说明:
$params : 数据库连接参数

举例说明:

例一、

首先在项目的主配置文件(config/application.php)中设置memcached连接参数,根据实际情部添加代码如下:

$config['memcached'] = array(
'servers'=> array(
array(
'host'=>'127.0.0.1',
'port'=>11211,
'persistent'=>true,
'weight'=>1,
'timeout'=>60
),),
'compressed'=>true,
'expire' => 3600,
'persistent' => true,
);

Controller文件代码内容如下:

public function indexAction() {

$mcdObj = $this->instance('CacheMemcached');

$mcdObj->set('name', 'doitphp');

$params = $mcdObj->get('name');

$this->dump($params);
}