fengdangxing/hyperf-model-cache

hyperf-db模型缓存、查询sql后自动缓存

v1.0.1 2024-03-27 06:48 UTC

This package is auto-updated.

Last update: 2024-12-27 08:18:38 UTC


README

hyperf-db模型缓存、查询sql后自动缓存 #配置数据库app/databases.php

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
return [
    'default' => [
        'driver' => env('DB_DRIVER', 'mysql'),
        'host' => env('DB_HOST', 'localhost'),
        'database' => env('DB_DATABASE', 'test'),
        'port' => env('DB_PORT', 3306),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', 'root'),
        'charset' => env('DB_CHARSET', 'utf8'),
        'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
        'prefix' => env('DB_PREFIX', ''),
        'pool' => [
            'min_connections' => 1,
            'max_connections' => 10,
            'connect_timeout' => 10.0,
            'wait_timeout' => 3.0,
            'heartbeat' => -1,
            'max_idle_time' => (float) env('DB_MAX_IDLE_TIME', 60),
        ],
        'commands' => [
            'gen:model' => [
                'path' => 'app/Model',
                'force_casts' => true,
                'inheritance' => 'Model',
            ],
        ],
    ],
];

#配置redis app/redis.php

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
return [
    'default' => [
        'host' => env('REDIS_HOST', 'localhost'),
        'auth' => env('REDIS_AUTH', null),
        'port' => (int) env('REDIS_PORT', 6379),
        'db' => (int) env('REDIS_DB', 0),
        'pool' => [
            'min_connections' => 1,
            'max_connections' => 50,
            'connect_timeout' => 10.0,
            'wait_timeout' => 3.0,
            'heartbeat' => -1,
            'max_idle_time' => (float) env('REDIS_MAX_IDLE_TIME', 60),
        ],
    ]
];

#使用

<?php
namespace App\Model;

use Fengdangxing\ModelCache\ModelCache;

class UserModel extends ModelCache
{
    protected $table = 'user';

    public static $redisList = 'user:list';
    public static $redisInfo = 'user:info';
    
    /**
      配置回调关系表
    */
    public static function hasDelRedis()
    {
        //UserInfo::delRedis(); 关系表删除缓存
    }

    /**
     * @Notes: 获取列表
     * @param array $param
     * @param string[] $showFields
     * @return array
     */
    public static function getPublishedList(array $param = [], array $showFields = ['*'], $isDeleted = false)
    {
        return self::getRow($param, $showFields, $page, $limit);
    }
}