定制 craftcms/yii2-cache-cascade 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

craftcms/yii2-cache-cascade

Composer 安装命令:

composer require craftcms/yii2-cache-cascade

包简介

A Yii2 cache component that cascades through multiple cache drivers on failure

README 文档

README

A Yii2 cache component that cascades through multiple cache drivers on failure, preventing app downtime due to cache outages.

Note: This is not a multi-store/write-through cache. Only one cache component is used at a time. This makes the most sense when using a fast, in-memory cache like ArrayCache as a fallback, to prevent your application from crashing while the primary cache (e.g., Redis) is unavailable or restabilizing.

Installation

composer require craftcms/yii2-cache-cascade

Usage

Configure the cache component in your Yii2 application config:

use craft\cachecascade\CascadeCache;
use craft\cachecascade\CacheFailedEvent;

'components' => [
    'cache' => [
        'class' => CascadeCache::class,
        'caches' => [
            'redisCache',
            [
                'class' => \yii\caching\ArrayCache::class,
            ],
        ],
        'on cacheFailed' => function (CacheFailedEvent $event) {
            // Custom logging
            Yii::error(
                "Cache failover: {$event->operation} failed on " . get_class($event->cache) . ': ' . $event->exception->getMessage(),
                'cache'
            );

            // Or send to external monitoring
            // MyMonitoring::trackCacheFailure(get_class($event->cache), $event->exception);

            // Optionally prevent cascading (will re-throw the exception)
            // $event->shouldCascade = false;
        },
    ],
    'redisCache' => [
        'class' => \yii\redis\Cache::class,
        'redis' => [
            'hostname' => 'localhost',
            'port' => 6379,
            'connectionTimeout' => 1,
            'dataTimeout' => 1,
            'retries' => 1,
            'retryInterval' => 0,
        ],
    ],
],

Configuration

caches

An array of cache components in priority order. Each element can be:

  • String: A component ID (e.g., 'redis', 'cache')
  • Array: A Yii2 component configuration
  • Object: A CacheInterface instance

Events

The component triggers a CascadeCache::EVENT_CACHE_FAILED event when a cache operation fails (shown in the usage example above). Use this for custom logging, monitoring, or to control cascade behavior.

CacheFailedEvent Properties

Property Type Description
$cache CacheInterface The cache that failed
$operation string The operation that failed ('get', 'set', etc.)
$exception \Throwable The exception that was thrown
$shouldCascade bool Whether to cascade to the next cache (default: true)

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固