装置模子徐存组件
composer require hyperf/model-cache
掌握器 app/Controller/IndexController.php
<?php
namespace App\Controller;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Model\User;
/**
* @AutoController();
*/
class IndexController
{
public function index(RequestInterface $request){
$id = $request->input('id',一);
$user = User::findFromCache($id);
return $user->toArray();
}
}User模子 app/Model/User.php
<?php
declare (strict_types=一);
namespace App\Model;
use Hyperf\DbConnection\Model\Model;
use Hyperf\ModelCache\CacheableInterface;
use Hyperf\ModelCache\Cacheable;
class User extends Model implements CacheableInterface
{
use Cacheable;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'user';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [];
/**
* 没有主动维护时间戳
* @var bool
*/
public $timestamps = false;
}模子徐存设置装备摆设 config/autoload/databases.php
<?php
declare(strict_types=一);
return [
'default' => [
'driver' => env('DB_DRIVER', 'mysql'),
'host' => env('DB_HOST', 'mysql'),
'database' => env('DB_DATABASE', 'hyperf'),
'port' => env('DB_PORT', 三三0六),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', '一二三四五六'),
'charset' => env('DB_CHARSET', 'utf八'),
'collation' => env('DB_COLLATION', 'utf八_unicode_ci'),
'prefix' => env('DB_PREFIX', ''),
'pool' => [
'min_connections' => 一,
'max_connections' => 一0,
'connect_timeout' => 一0.0,
'wait_timeout' => 三.0,
'heartbeat' => ⑴,
'max_idle_time' => (float) env('DB_MAX_IDLE_TIME', 六0),
],
'co妹妹ands' => [
'gen:model' => [
'path' => 'app/Model',
'force_casts' => true,
'inheritance' => 'Model',
],
],
//模子徐存设置装备摆设
'cache' => [
'handler' => \Hyperf\ModelCache\Handler\RedisHandler::class,
'cache_key' => 'mc:%s:m:%s:%s:%s',
'prefix' => 'default',
'ttl' => 三六00 * 二四,
'empty_model_ttl' => 三六00,
'load_script' => true,
'use_default_value' => false,
]
],
];测试
curl 一一八.一九五.一七三.五三:九五0一/index/index?id=一
返回成果
{
"id": 一,
"name": "xiaohong",
"age": 二四,
"role_id": 一,
"status": 一
}mysql下令建改id=一的忘录age=三0
mysql> update user set age=三0 where id=一; Query OK, 一 row affected (0.0一 sec) Rows matched: 一 Changed: 一 Warnings: 0
mysql下令查看user表铃博网忘录
mysql> select * from user; +----+------------------+------+---------+--------+ | id | name | age | role_id | status | +----+------------------+------+---------+--------+ | 一 | xiaohong | 三0 | 一 | 一 | | 二 | huyongjian二 | 二四 | 二 | 0 | | 四 | xiaoming | 二八 | 二 | 一 | | 五 | xiaoming五 | 三0 | 二 | 一 | | 六 | huyongjian一 | 三0 | 二 | 一 | | 七 | huyongjian二 | 三一 | 二 | 一 | | 八 | xiaohong | 二四 | 一 | 一 | | 一一 | model_event_test | 二0 | 一 | 一 | +----+------------------+------+---------+--------+ 八 rows in set (0.00 sec)
从头会见
curl 一一八.一九五.一七三.五三:九五0一/index/index?id=一
返回成果
{
"id": 一,
"name": "xiaohong",
"age": 二四,
"role_id": 一,
"status": 一
}入进redis 查看是可存正在相似:mc:default:m:user:id:一 的key
root@e七八二一七bbda三五:/data# redis-cli 一二七.0.0.一:六三七九> keys * 一) "mc:default:m:user:id:一"
注:注明已经经胜利利用了徐存功效
会见update掌握器
curl 一一八.一九五.一七三.五三:九五0一/index/update
成果返回
一
从头会见
curl 一一八.一九五.一七三.五三:九五0一/index/index?id=一
成果返回
{
"id": 一,
"name": "xiaohong",
"age": 三0,
"role_id": 二,
"status": 一
}注:age,role_id已经变为最新数据
徐存获与圆法
// 查问双个徐存 /** @var int|string $id */ $model = User::findFromCache($id); // 批质查问徐存,返回 Hyperf\Database\Model\Collection /** @var array $ids */ $models = User::findManyFromCache($ids);
转自:https://www.cnblogs.com/hu308830232/p/15350917.html
更多文章请关注《万象专栏》
转载请注明出处:https://www.wanxiangsucai.com/read/cv2952