-
Notifications
You must be signed in to change notification settings - Fork 25
5.数据缓存
mi pro edited this page Apr 28, 2018
·
5 revisions
缓存类的定义请参考 缓存类
/**
* 模型
**/
class UserModel extends \Itxiao6\Database\Eloquent\Model {
/**
* 构造方法
* */
public function __construct(array $attributes = [])
{
// 实例化数据库容器
$capsule = new DB;
// 添加连接到容器内
$capsule - >addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'username',
'password' => 'pwd',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
$capsule -> bootEloquent();
# 设置缓存驱动
$this -> set_cache_driver(new CacheClass());
}
/**
* 定义表名
* */
protected $table = 'users';
}# 查询id=50的用户,缓存时间为10秒。缓存时间内不会请求数据库。而是使用缓存驱动,获得数据。
UserModel::remember(10) -> where(['id'=>50]) -> first();