当前位置: 首页 > article >正文

15. 缓存(Cache)

一、安装

composer require hyperf/cache

二、配置

  • 文件:config/autoload/cache.php

    <?php
    
    return [
        'default' => [
            'driver' => Hyperf\Cache\Driver\RedisDriver::class,	// 缓存驱动,默认为 Redis
            'packer' => Hyperf\Codec\Packer\PhpSerializerPacker::class,	// 打包器
            'prefix' => 'c:',	// 缓存前缀
            'skip_cache_results' => [],		// 指定的结果不被缓存
        ],
    ];
    

三、使用 注解方式

1. 获取并生成缓存(#[Cacheable])

<?php
namespace App\Service;

use App\Model\User;
use Hyperf\Cache\Annotation\Cacheable;

class CacheService
{
    #[Cacheable(prefix: 'user', value: "_#{id}", ttl: 900)]
    public function user($id)
    {
        $user = User::query()->find($id);

        if (!$user) {
            return null;
        }
        return $user->toArray();
    }
}
  • 调用
<?php
namespace App\Controller;

use App\Service\CacheService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;

#[AutoController]
class CacheController
{
    #[Inject]
    private CacheService $cacheService;

    public function index()
    {
        return $this->cacheService->user(1);
    }
}

2. 更新缓存(#[CachePut])

  • 同上

3. 删除缓存 (#[CacheEvict])

  • 同上

http://www.kler.cn/news/366002.html

相关文章:

  • AndroidStudio新建Native项目报错[CXX1429]
  • Github关于LLM热门项目(10k+)
  • vue计算属性报错:Computed property “energyTotal“ was assigned to but it has no setter.
  • 【web安全】缓慢的HTTP拒绝服务攻击详解
  • 分页列表缓存
  • 解决:如何在opencv中得到与matlab立体标定一样的矫正图?(python版opencv)
  • 24-10-21-读书笔记(二十八)-《契诃夫文集》(十二)下([俄] 契诃夫 [译] 汝龙)我们会生活下去!
  • Island Architecture 孤岛架构
  • 正则表达式基本语法(快速认知)
  • 用xshell给服务器上传jar包
  • PVE系统无损挂载ntfs格式物理磁盘
  • 中小企业设备管理:Spring Boot实现指南
  • 2024-10-22 问AI: [AI面试题] 讨论递归神经网络 (RNN) 的概念
  • 枫清科技高雪峰:大模型要成为生产力才有生命力
  • Docker 镜像下载问题及解决办法
  • OpenCV单目相机内参标定C++
  • 深度剖析:电商 API 接口如何成就卓越用户体验
  • edge下默认pdf图标的修复办法
  • 新手入门之高级maven
  • iOS 本地存储地址(位置)
  • Python爬虫教程:从入门到精通
  • Flutter不常用组件----InteractiveViewer
  • 量子机器学习:颠覆性的前沿技术
  • 2、图像的特征
  • 如何利用动态IP对市场进行产品调研分析?
  • 架构的本质之 MVC 架构