yiisoft/cache-db
Composer 安装命令:
composer require yiisoft/cache-db
包简介
Yii Caching Library - DB Handler
关键字:
README 文档
README
Yii Cache Library - DB Handler
This package implements database-based PSR-16 cache.
Supported databases
| Packages | PHP | Versions | CI-Actions |
|---|---|---|---|
| [db-mssql] | 8.1 - 8.5 | 2017 - 2025 | |
| [db-mysql] (MySQL) | 8.1 - 8.5 | 5.7 - 9.5 | |
| [db-mysql] (MariaDB) | 8.1 - 8.5 | 10.4 - 12.10 | |
| [db-oracle] | 8.1 - 8.5 | 11C - 21C | |
| [db-pgsql] | 8.1 - 8.5 | 9.0 - 18.0 | |
| [db-sqlite] | 8.1 - 8.5 | 3:latest |
Requirements
- PHP 8.1 - 8.5.
PDOPHP extension.
Installation
The package could be installed with Composer:
composer require yiisoft/cache-db
Create database connection
For more information see yiisoft/db.
Database Preparing
Package provides two way for preparing database:
-
Raw SQL. You can use it with the migration package used in your application.
-
Ensure tables:
-
Ensure no tables:
-
-
DbSchemaManagerforensureTable(),ensureNoTable()methods for cache table (by default{{%yii_cache}}).
// Create db schema manager $dbSchemaManager = new DbSchemaManager($db); // Ensure table with default name $dbSchemaManager->ensureTable(); // Ensure table with custom name $dbSchemaManager->ensureTable('{{%custom_cache_table}}'); // Ensure no table with default name $dbSchemaManager->ensureNoTable(); // Ensure no table with custom name $dbSchemaManager->ensureNoTable('{{%custom_cache_table}}');
Configuration
When creating an instance of \Yiisoft\Cache\Db\DbCache, you must pass an instance of the database connection.
$cache = new \Yiisoft\Cache\Db\DbCache($db, $table, $gcProbability);
$db (\Yiisoft\Db\Connection\ConnectionInterface)- The database connection instance.$table (string)- The name of the database table to store the cache data. Defaults to "cache".$gcProbability (int)- The probability (parts per million) that garbage collection (GC) should be performed when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance. This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
General usage
The package does not contain any additional functionality for interacting with the cache, except those defined in the PSR-16 interface.
$cache = new \Yiisoft\Cache\Db\DbCache($db); $parameters = ['user_id' => 42]; $key = 'demo'; // try retrieving $data from cache $data = $cache->get($key); if ($data === null) { // $data is not found in cache, calculate it from scratch $data = calculateData($parameters); // store $data in cache for an hour so that it can be retrieved next time $cache->set($key, $data, 3600); } // $data is available here
In order to delete value you can use:
$cache->delete($key); // Or all cache $cache->clear();
To work with values in a more efficient manner, batch operations should be used:
getMultiple()setMultiple()deleteMultiple()
This package can be used as a cache handler for the Yii Caching Library.
Additional logging
In order to log details about failures you may set a logger instance. It should be Psr\Log\LoggerInterface::class. For example, you can use yiisoft\Log:
$cache = new \Yiisoft\Cache\Db\DbCache($db, $table, $gcProbability); $cache->setLogger(new \Yiisoft\Log\Logger());
This allows you to log cache operations, when ocurring errors, etc.
Documentation
If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
License
The Yii Cache Library - DB Handler is free software. It is released under the terms of the BSD License.
Please see LICENSE for more information.
Maintained by Yii Software.
Support the project
Follow updates
yiisoft/cache-db 适用场景与选型建议
yiisoft/cache-db 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.76k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2021 年 10 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「cache」 「db」 「yii」 「psr-16」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yiisoft/cache-db 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yiisoft/cache-db 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yiisoft/cache-db 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
repository php library
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
Laravel 5 - Repositories to the database layer
Priveate for SkeekS CMS
C3JS Chart for Yii Framework 2.0
统计信息
- 总下载量: 7.76k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2021-10-07