承接 tobento/app-cache 相关项目开发

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

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

tobento/app-cache

Composer 安装命令:

composer require tobento/app-cache

包简介

App cache support.

README 文档

README

Cache support for the app.

Table of Contents

Getting Started

Add the latest version of the app cache project running this command.

composer require tobento/app-cache

Requirements

  • PHP 8.4 or greater

Documentation

App

Check out the App Skeleton if you are using the skeleton.

You may also check out the App to learn more about the app in general.

Cache Boot

The cache boot does the following:

  • installs and loads cache config file
  • implements PSR-6 and PSR-16 interfaces based on cache config
use Tobento\App\AppFactory;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots:
$app->boot(\Tobento\App\Cache\Boot\Cache::class);

// Run the app:
$app->run();

You may check out the Cache Service to learn more about it.

Cache Config

The configuration for the cache is located in the app/config/cache.php file at the default App Skeleton config location where you can specify the pools and caches for your application.

Cache Usage

You can access the pools and caches in several ways:

Using the app

use Tobento\App\AppFactory;
use Tobento\Service\Cache\CacheItemPoolsInterface;
use Tobento\Service\Cache\Simple\CachesInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots:
$app->boot(\Tobento\App\Cache\Boot\Cache::class);
$app->booting();

// PSR-6 cache:

// using the default pool:
$pool = $app->get(CacheItemPoolInterface::class);

// using the pools:
$pools = $app->get(CacheItemPoolsInterface::class);


// PSR-16 simple cache:

// using the default pool:
$cache = $app->get(CacheInterface::class);

// using the caches:
$caches = $app->get(CachesInterface::class);

// Run the app:
$app->run();

Check out the Cache Item Pools Interface to learn more about it.

Check out the Caches Interface to learn more about it.

Using autowiring

You can also request the interfaces in any class resolved by the app:

use Tobento\Service\Cache\CacheItemPoolsInterface;
use Tobento\Service\Cache\Simple\CachesInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;

class SomeService
{
    public function __construct(
        protected CacheItemPoolsInterface $pools,
        protected CacheItemPoolInterface $pool,
        protected CachesInterface $caches,
        protected CacheInterface $cache,
    ) {}
}

Adding and Registering Caches

You may add and register more pools and caches by the following way instead of using the cache config file:

use Tobento\App\AppFactory;
use Tobento\Service\Cache\CacheItemPoolsInterface;
use Tobento\Service\Cache\Simple\CachesInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// PSR-6:
$app->on(CacheItemPoolsInterface::class, function(CacheItemPoolsInterface $pools) {
    // using the add method:
    $pools->add(name: 'name', pool: $pool);
    
    // using the register method:
    $pools->register(
        name: 'name',
        pool: function(string $name): CacheItemPoolInterface {
            // create the pool:
            return $pool;
        },
    );
});

// PSR-16:
$app->on(CachesInterface::class, function(CachesInterface $caches) {
    // using the add method:
    $caches->add(name: 'name', cache: $cache);
    
    // using the register method:
    $caches->register(
        name: 'name',
        cache: function(string $name): CacheInterface {
            // create the cache:
            return $cache;
        },
    );
});

// Adding boots:
$app->boot(\Tobento\App\Cache\Boot\Cache::class);

// Run the app:
$app->run();

Deleting Expired Items

Some PSR 6 cache pools or PSR 16 caches do not include an automated mechanism for pruning expired cache items.

If you have installed the App Console you may easily delete expired items running the following commands:

PSR 6

php ap cache:pool:prune

PSR 16

php ap cache:prune

If you would like to automate this process, consider installing the App Schedule bundle and using a command task:

use Tobento\Service\Schedule\Task;
use Butschster\CronExpression\Generator;

$schedule->task(
    new Task\CommandTask(
        command: 'cache:pool:prune',
    )
    // schedule task:
    ->cron(Generator::create()->weekly())
);

Clearing Cache

If you have installed the App Console you may easily clear caches running the following commands:

PSR 6

php ap cache:pool:clear

Clearing specific pools only:

php ap cache:pool:clear --pool=file --pool=another

PSR 16

php ap cache:clear

Clearing specific pools only:

php ap cache:clear --cache=file --cache=another

Credits

tobento/app-cache 适用场景与选型建议

tobento/app-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 85 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 06 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「package」 「cache」 「app」 「tobento」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 tobento/app-cache 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 85
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 18
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-30