{"id":14966326,"url":"https://github.com/yiisoft/cache-db","last_synced_at":"2025-07-22T13:06:49.463Z","repository":{"id":37275838,"uuid":"184578374","full_name":"yiisoft/cache-db","owner":"yiisoft","description":"Yiisoft database adapter for cache","archived":false,"fork":false,"pushed_at":"2024-12-13T11:07:16.000Z","size":197,"stargazers_count":12,"open_issues_count":2,"forks_count":7,"subscribers_count":17,"default_branch":"master","last_synced_at":"2025-07-02T11:53:24.481Z","etag":null,"topics":["cache","database","hacktoberfest","yii3"],"latest_commit_sha":null,"homepage":"https://www.yiiframework.com/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yiisoft.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"open_collective":"yiisoft","github":["yiisoft"]}},"created_at":"2019-05-02T12:38:20.000Z","updated_at":"2024-12-13T11:07:20.000Z","dependencies_parsed_at":"2024-04-11T04:04:34.431Z","dependency_job_id":"5b98ef11-ee92-46d6-b22a-a9f47a678e36","html_url":"https://github.com/yiisoft/cache-db","commit_stats":{"total_commits":76,"total_committers":11,"mean_commits":6.909090909090909,"dds":0.6710526315789473,"last_synced_commit":"ca870fcd9fb1fbc7bad5380611c362a0f27d14ec"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/yiisoft/cache-db","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fcache-db","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fcache-db/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fcache-db/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fcache-db/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yiisoft","download_url":"https://codeload.github.com/yiisoft/cache-db/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fcache-db/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264396534,"owners_count":23601534,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","database","hacktoberfest","yii3"],"created_at":"2024-09-24T13:36:13.284Z","updated_at":"2025-07-22T13:06:49.439Z","avatar_url":"https://github.com/yiisoft.png","language":"PHP","funding_links":["https://opencollective.com/yiisoft","https://github.com/sponsors/yiisoft"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/yiisoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://yiisoft.github.io/docs/images/yii_logo.svg\" height=\"100px\" alt=\"Yii\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003eYii Cache Library - DB Handler\u003c/h1\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\n[![Latest Stable Version](https://poser.pugx.org/yiisoft/cache-db/v/stable.png)](https://packagist.org/packages/yiisoft/cache-db)\n[![Total Downloads](https://poser.pugx.org/yiisoft/cache-db/downloads.png)](https://packagist.org/packages/yiisoft/cache-db)\n[![codecov](https://codecov.io/gh/yiisoft/cache-db/branch/master/graph/badge.svg?token=H7YALMK1YN)](https://codecov.io/gh/yiisoft/cache-db)\n[![Mutation testing badge](https://img.shields.io/endpoint?style=flat\u0026url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fcache-db%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/cache-db/master)\n[![static analysis](https://github.com/yiisoft/cache-db/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/cache-db/actions?query=workflow%3A%22static+analysis%22)\n[![type-coverage](https://shepherd.dev/github/yiisoft/cache-db/coverage.svg)](https://shepherd.dev/github/yiisoft/cache-db)\n\nThis package implements database-based [PSR-16](https://www.php-fig.org/psr/psr-16/) cache.\n\n## Supported databases\n\n| Packages | PHP | Versions | CI-Actions |\n|----------|-----|----------|------------|\n| [[db-mssql]](https://github.com/yiisoft/db-mssql) | **8.0 - 8.2** | **2017 - 2022** | [![mssql](https://github.com/yiisoft/cache-db/actions/workflows/mssql.yml/badge.svg)](https://github.com/yiisoft/cache-db/actions/workflows/mssql.yml) | |\n| [[db-mysql/mariadb]](https://github.com/yiisoft/db-mysql)  | **8.0 - 8.2** |  **5.7-8.0**/**10.4-10.10** | [![mysql](https://github.com/yiisoft/cache-db/actions/workflows/mysql.yml/badge.svg)](https://github.com/yiisoft/cache-db/actions/workflows/mysql.yml)   |\n| [[db-oracle]](https://github.com/yiisoft/db-oracle) | **8.0 - 8.2** |  **11C - 21C** | [![oracle](https://github.com/yiisoft/cache-db/actions/workflows/oracle.yml/badge.svg)](https://github.com/yiisoft/cache-db/actions/workflows/oracle.yml) |\n| [[db-pgsql]](https://github.com/yiisoft/db-pgsql)  | **8.0 - 8.2** | **9.0 - 15.0** | [![pgsql](https://github.com/yiisoft/cache-db/actions/workflows/pgsql.yml/badge.svg)](https://github.com/yiisoft/cache-db/actions/workflows/pgsql.yml) |\n| [[db-sqlite]](https://github.com/yiisoft/db-sqlite) | **8.0 - 8.2** |  **3:latest** | [![sqlite](https://github.com/yiisoft/cache-db/actions/workflows/sqlite.yml/badge.svg)](https://github.com/yiisoft/cache-db/actions/workflows/sqlite.yml) |\n\n## Requirements\n\n- PHP 8.0 or higher.\n- `PDO` PHP extension.\n\n## Installation\n\nThe package could be installed with [Composer](https://getcomposer.org):\n\n```shell\ncomposer require yiisoft/cache-db\n```\n\n## Create database connection\n\nFor more information see [yiisoft/db](https://github.com/yiisoft/db/tree/master/docs/guide/en#create-connection).\n\n## Database Preparing\n\nPackage provides two way for preparing database:\n\n1. Raw SQL. You can use it with the migration package used in your application.\n\n    - Ensure tables:\n        - [MSSQL](sql/sqlsrv-up.sql),\n        - [MySQL / MariaDB](sql/mysql-up.sql),\n        - [Oracle](sql/oci-up.sql),\n        - [PostgreSQL](sql/pgsql-up.sql)\n        - [SQLite](sql/sqlite-up.sql)\n\n    - Ensure no tables:\n        - [MSSQL](sql/sqlsrv-down.sql),\n        - [MySQL / MariaDB](sql/mysql-down.sql),\n        - [Oracle](sql/oci-down.sql),\n        - [PostgreSQL](sql/pgsql-down.sql)\n        - [SQLite](sql/sqlite-down.sql)\n\n2. `DbSchemaManager` for `ensureTable()`, `ensureNoTable()` methods for cache table (by default `{{%yii_cache}}`).\n\n```php\n// Create db schema manager\n$dbSchemaManager = new DbSchemaManager($db);\n\n// Ensure table with default name\n$dbSchemaManager-\u003eensureTable();\n\n// Ensure table with custom name\n$dbSchemaManager-\u003eensureTable('{{%custom_cache_table}}');\n\n// Ensure no table with default name\n$dbSchemaManager-\u003eensureNoTable();\n\n// Ensure no table with custom name\n$dbSchemaManager-\u003eensureNoTable('{{%custom_cache_table}}');\n```\n\n## Configuration\n\nWhen creating an instance of `\\Yiisoft\\Cache\\Db\\DbCache`, you must pass an instance of the database connection.\n\n```php\n$cache = new \\Yiisoft\\Cache\\Db\\DbCache($db, $table, $gcProbability);\n```\n\n- `$db (\\Yiisoft\\Db\\Connection\\ConnectionInterface)` - The database connection instance.\n- `$table (string)` - The name of the database table to store the cache data. Defaults to \"cache\".\n- `$gcProbability (int)` - The probability (parts per million) that garbage collection (GC) should\n  be performed when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.\n  This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.\n\n## General usage\n\nThe package does not contain any additional functionality for interacting with the cache,\nexcept those defined in the [PSR-16](https://www.php-fig.org/psr/psr-16/) interface.\n\n```php\n$cache = new \\Yiisoft\\Cache\\Db\\DbCache($db);\n$parameters = ['user_id' =\u003e 42];\n$key = 'demo';\n\n// try retrieving $data from cache\n$data = $cache-\u003eget($key);\n\nif ($data === null) {\n    // $data is not found in cache, calculate it from scratch\n    $data = calculateData($parameters);\n    \n    // store $data in cache for an hour so that it can be retrieved next time\n    $cache-\u003eset($key, $data, 3600);\n}\n\n// $data is available here\n```\n\nIn order to delete value you can use:\n\n```php\n$cache-\u003edelete($key);\n// Or all cache\n$cache-\u003eclear();\n```\n\nTo work with values in a more efficient manner, batch operations should be used:\n\n- `getMultiple()`\n- `setMultiple()`\n- `deleteMultiple()`\n\nThis package can be used as a cache handler for the [Yii Caching Library](https://github.com/yiisoft/cache).\n\n## Additional logging\n\nIn 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](https://github.com/yiisoft/log):\n\n```php\n$cache = new \\Yiisoft\\Cache\\Db\\DbCache($db, $table, $gcProbability);\n$cache-\u003esetLogger(new \\Yiisoft\\Log\\Logger());\n```\n\nThis allows you to log cache operations, when ocurring errors, etc.\n\n## Documentation\n\n- [Internals](docs/internals.md)\n\nIf you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that.\nYou may also check out other [Yii Community Resources](https://www.yiiframework.com/community).\n\n## License\n\nThe Yii Cache Library - DB Handler is free software. It is released under the terms of the BSD License.\nPlease see [`LICENSE`](./LICENSE.md) for more information.\n\nMaintained by [Yii Software](https://www.yiiframework.com/).\n\n## Support the project\n\n[![Open Collective](https://img.shields.io/badge/Open%20Collective-sponsor-7eadf1?logo=open%20collective\u0026logoColor=7eadf1\u0026labelColor=555555)](https://opencollective.com/yiisoft)\n\n## Follow updates\n\n[![Official website](https://img.shields.io/badge/Powered_by-Yii_Framework-green.svg?style=flat)](https://www.yiiframework.com/)\n[![Twitter](https://img.shields.io/badge/twitter-follow-1DA1F2?logo=twitter\u0026logoColor=1DA1F2\u0026labelColor=555555?style=flat)](https://twitter.com/yiiframework)\n[![Telegram](https://img.shields.io/badge/telegram-join-1DA1F2?style=flat\u0026logo=telegram)](https://t.me/yii3en)\n[![Facebook](https://img.shields.io/badge/facebook-join-1DA1F2?style=flat\u0026logo=facebook\u0026logoColor=ffffff)](https://www.facebook.com/groups/yiitalk)\n[![Slack](https://img.shields.io/badge/slack-join-1DA1F2?style=flat\u0026logo=slack)](https://yiiframework.com/go/slack)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiisoft%2Fcache-db","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyiisoft%2Fcache-db","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiisoft%2Fcache-db/lists"}