承接 slydeath/laravel-nested-caching 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

slydeath/laravel-nested-caching

Composer 安装命令:

composer require slydeath/laravel-nested-caching

包简介

Nested Caching for Laravel with caches stack

README 文档

README

Latest Stable Version Total Downloads License

Minimum requirements

  • PHP 7.4
  • Laravel 8

Installation

Add package to composer.json:

composer require slydeath/laravel-nested-caching

Open config/app.php and add the service provider to the array providers:

SlyDeath\NestedCaching\NestedCachingServiceProvider::class,

To place the configuration file, run:

php artisan vendor:publish --provider="SlyDeath\NestedCaching\NestedCachingServiceProvider" --tag=config

How to use?

Caching any HTML chunk

To cache any HTML chunk, you just need to pass the caching key to the @cache directive fragment:

@cache('simple-cache')
<div>
    This is an arbitrary piece of HTML that will be cached
    using the «simple-cache» key
</div>
@endCache

Model caching

To enable model caching support, add the trait to it NestedCacheable:

use SlyDeath\NestedCaching\NestedCacheable;

class User extends Model
{
    use NestedCacheable;
}

In the template, to cache a model, you need to pass its instance to the @cache directive:

@cache($user)
<div>App\User model caching:</div>
<ul>
    <li>Name: {{ $user->name }}</li>
    <li>Email: {{ $user->email }}</li>
</ul>
@endCache

Caching the model for a specified time

To cache the model for a certain time, specify the lifetime in minutes as the second parameter:

@cache($user, 1440)
<div>...</div>
@endCache

Updating the «parent»

For update the cache of the «parent model», we need setup touches:

use SlyDeath\NestedCaching\NestedCacheable;

class CarUser extends Model
{
    use NestedCacheable;

    // Specifying the parent relations
    protected $touches = ['user']; 

    // Parent relation
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

Usage example:

resources/views/user.blade.php

@cache($user)
<section>
    <h2>User's cars {{ $user->name }}</h2>
    <ul>
        @foreach($user->cars as $car)
            @include('user-car');
        @endforeach
    </ul>
</section>
@endCache

resources/views/user-car.blade.php

@cache($car)
<li>{{ $car->brand }}</li>
@endCache

Collection caching

Example of caching a collection:

@cache($users)
@foreach ($users as $user)
    @include('user');
@endforeach
@endCache

How to remove stack cache?

Just run this code at bottom of your page:

app(SlyDeath\NestedCaching\CacheStack::class)->clearCache();

Workflow with another caches

How to collect keys?

Keys are automatically collected in SlyDeath\NestedCaching\CacheWrittenListener if another-caching is enabled, but you must save them manually (at the end of executing app) if you want to use them elsewhere:

app(\SlyDeath\NestedCaching\CacheStack::class)->getAnotherCaches();

How to clear the stack of another caches?

Just callclearAnotherCaches method:

 app(\SlyDeath\NestedCaching\CacheStack::class)->clearAnotherCaches();

How to save keys for different pages?

You can generate dynamically the cache key (for example from the page id):

app(\SlyDeath\NestedCaching\CacheStack::class)->setAnotherKey('my-cache-key-prefix:' . optional($page)->id)->getAnotherCaches();

How to clear the stack of another caches with custom cache key?

Just callclearAnotherCaches method and provide the key:

 app(\SlyDeath\NestedCaching\CacheStack::class)->setAnotherKey('my-cache-key-prefix:' . optional($page)->id)->clearAnotherCaches();

Enable PhpStorm support

Go to the Settings → PHP → Blade, then uncheck Use default settings. Go to Directives tab and press «+» to add another one custom directive:

  • Name: cache
  • Checkbox Has parameterstrue
  • Prefix: <?php if ( ! app('SlyDeath\NestedCaching\BladeDirectives')->cache(
  • Suffix: )) { ?>

And add close directive without parameters:

  • Name: endcache or endCache, whatever you use

slydeath/laravel-nested-caching 适用场景与选型建议

slydeath/laravel-nested-caching 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.87k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 07 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 slydeath/laravel-nested-caching 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 slydeath/laravel-nested-caching 我们能提供哪些服务?
定制开发 / 二次开发

基于 slydeath/laravel-nested-caching 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 11.87k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-15