nanvaie/database-repository
Composer 安装命令:
composer require nanvaie/database-repository
包简介
Repository Pattern
README 文档
README
Installation
Use the following command to add this package to the composer development requirement.
composer require eghamat24/database-repository --dev
Setup Laravel Repository
Then run the following command in the console to publish the necessary assets in your project directory.
php artisan vendor:publish --tag=database-repository-config
Setup Lumen Repository
Navigate to app.php in bootstrap folder and add the following line after service providers registrations:
// snip if ($app->environment('local', 'testing')) { $app->register(Eghamat24\DatabaseRepository\DatabaseRepositoryServiceProvider::class); } // snip
Copy repository.php to the project config folder located at the project root.
Note: Make sure to run composer dump-autoload after these changes.
Usage
To use this package easily, you can run the following command. It will create all the required components such as Entity, IRepository, Repository, MySqlRepository, RedisRepository, Resource, and Factory for the users table.
php artisan repository:make-all --table_names=users --strategy_name=QueryCacheStrategy
List of artisan commands:
| Command | Inputs | Options | Description |
|---|---|---|---|
repository:make-entity |
table_name | -f, -d, -k, -g | Create new Entity |
repository:make-enum |
table_name | -f, -d, -g | Create new Enum |
repository:make-factory |
table_name | -f, -d, -g | Create new Factory |
repository:make-resource |
table_name | -f, -d, -k, -g | Create new Resource |
repository:make-interface-repository |
table_name | -f, -d, -k, -g | Create new Repository Interface |
repository:make-repository |
table_name, selected_db(optional) | -f, -d, -k, -g | Create new Base Repository |
repository:make-mysql-repository |
table_name | -f, -d, -k, -g | Create new MySql Repository class |
repository:make-redis-repository |
table_name | -f, -d, -k, -g | Create new Redis Repository class |
repository:make-all |
--table_names=table_names(optional) --selected_db=database(optional) |
-a, -f, -d, -k, -g | Run all of the above commands |
Options Explanation
-f|--force: Force commands to override existing files.-d|--delete: Delete already created files.-k|--foreign-keys: Try to detect foreign keys of a table.-g|--add-to-git: Add created files to the git repository.-a|--all-tables: Use all existing tables.--table_names=: Add table names, separate names with comma.--selected_db=: Use betweenMysql,Redis, If it does not send, the value will return fromconfig/repository.php--strategy_name=: add a trait to your Redis repository based on the strategy you choose
Example 1. Create a new Entity for a table named 'users'.
php artisan repository:make-entity users
Example 2. Create all necessary classes for two tables named 'users' and 'customers' with an enabled foreign key option.
php artisan repository:make-all --table_names=users,customers -k
Example 3. Create all necessary classes for all tables with an enabled foreign key option(this may be used for new projects).
php artisan repository:make-all -a -k
Cache Strategy
We created some strategies for caching data, based on the number of records and change frequency
SingleKeyCacheStrategy
SingleKeyCacheStrategy is a good choice for tables with very few rows, such as less than 50 records. This strategy creates one cache key and stores all the data on it. Then, when the app queries data, it loops through the data and returns the result.
This strategy has one cache key and clears all the cached data whenever there is a change. This ensures that the data is always updated and valid.
QueryCacheStrategy
QueryCacheStrategy is suitable for tables that have low change frequency and not too many records. For example, if the table has less than 10,000 rows or if it has more than that but the queries are not very unique and there are similar queries, this strategy works well.
This strategy assigns a tag to each cache of the repository and clears all the cached data whenever there is a change. This ensures that the data is always updated and valid.
TemporaryCacheStrategy
TemporaryCacheStrategy is useful when we have a large or diverse amount of data and the data user can tolerate some delay in the updates. We cache every request and delay the changes that are not critical to be reflected in real time. For example, if we have a post and we edit its content, we can cache the old version until the cache expires. We set the cache expiration time for each data item based on the maximum delay the user can accept. This way, we use this strategy to cache data temporarily.
ClearableTemporaryCacheStrategy
Sometimes, we need to clear the data from a temporary cache when some critical changes occur. ClearableTemporaryCacheStrategy is suitable for these situations.
php artisan repository:make-all --table_names=users --strategy_name=QueryCacheStrategy
nanvaie/database-repository 适用场景与选型建议
nanvaie/database-repository 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 910 次下载、GitHub Stars 达 19, 最近一次更新时间为 2022 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nanvaie/database-repository 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nanvaie/database-repository 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 910
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 19
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-02