hyperf 缓存
Cache代理类
Cache代理类 app/Utils/Cache.php
<?php
namespace App\Utils;
use Hyperf\Utils\ApplicationContext;
use Psr\SimpleCache\CacheInterface;
class Cache
{
        public static function getInstance()
        {
                return ApplicationContext::getContainer()->get(CacheInterface::class);
        }
        public static function __callStatic($name, $arguments)
        {
                return self::getInstance()->$name(...$arguments);
        }
}控制器 app/Controller/IndexController.php
<?php
namespace App\Controller;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Utils\Cache;
/**
 * @AutoController();
 */
class IndexController
{
        public function index(){
                Cache::getInstance()->set('user:1', ['name'=>'huyongjian', 'id'=>'1']);
                $re = Cache::get('user:1');
                return $re;
        }
}cache配置文件 config/autoload/cache.php
<?php
declare(strict_types=1);
return [
    'default' => [
        'driver' => Hyperf\Cache\Driver\RedisDriver::class,
        'packer' => Hyperf\Utils\Packer\PhpSerializerPacker::class,
        'prefix' => 'c:',
    ],
];env配置文件 /.env
//redis是docker link的Redis
REDIS_HOST=redis
REDIS_AUTH=(null)
REDIS_PORT=6379
REDIS_DB=0客户端测试
curl 118.195.173.53:9501/index/index结果显示
{"name":"huyongjian","id":"1"}Model缓存
控制器 app/Controller/IndexController.php
<?php
namespace App\Controller;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Model\User;
use Hyperf\Di\Annotation\Inject;
use App\Service\UserService;
use App\Service\SystemService;
/**
 * @AutoController();
 */
class IndexController
{
         /**
         * @Inject()
         * @var UserService
          */
        private $user;
        /**
         * @Inject()
         * @var SystemService
         */
        private $systemService;
        public function getUser(){
                return $this->user->user(1);
        }
        public function updateUser(){
                /** @var User $user */
                $user = User::query()->find(1);
                $user->name = 'HuYongjian'.rand(1,10);
                $user->save();
                $this->systemService->flushUserCache(1);
        }
}UserService类 app/Service/UserService.php
<?php
namespace App\Service;
use App\Model\User;
use Hyperf\Cache\Annotation\Cacheable;
class UserService
{
        /**
         * @Cacheable(prefix="user", ttl=9000, listener="user-update")
         */
        public function user($id)
        {
                $user = User::query()->where('id',$id)->first();
                if($user){
                        return $user->toArray();
                }
                return null;
        }
}SystemService类 app/Service/SystemService.php
<?php
declare(strict_types=1);
namespace App\Service;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Cache\Listener\DeleteListenerEvent;
use Psr\EventDispatcher\EventDispatcherInterface;
class SystemService
{
    /**
     * @Inject
     * @var EventDispatcherInterface
     */
    protected $dispatcher;
    public function flushUserCache($userId)
    {
        $this->dispatcher->dispatch(new DeleteListenerEvent('user-update', [$userId]));
        return true;
    }
}Model类 app/Model/User.php
<?php
declare (strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property string $name 
 * @property int $status 
 */
class User extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * @var bool
     */
    public $timestamps = false;
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'status' => 'integer'];
}进入MySQL 查看user表记录
select * from user;
+----+-------------+--------+
| id | name        | status |
+----+-------------+--------+
|  1 | HuYongjian6 |      1 |
|  2 | huyongjian2 |      0 |
+----+-------------+--------+访问测试(多次刷新看是否是缓存数据)
curl 118.195.173.53:9501/index/getUser{"id":1,"name":"HuYongjian6","status":1}更新后重新访问getUser (看缓存数据是否改变)
curl 118.195.173.53:9501/index/updateUser 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号