在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):GeneaLabs/laravel-model-caching开源软件地址(OpenSource Url):https://github.com/GeneaLabs/laravel-model-caching开源编程语言(OpenSource Language):PHP 99.9%开源软件介绍(OpenSource Introduction):Model Caching for LaravelSupporting This PackageThis is an MIT-licensed open source project with its ongoing development made possible by the support of the community. If you'd like to support this, and our other packages, please consider becoming a sponsor. We thank the following sponsors for their generosity, please take a moment to check them out: ImpetusI created this package in response to a client project that had complex, nested
forms with many Features
Cache DriversThis package is best suited for taggable cache drivers: + Redis
+ MemCached
+ APC
+ Array It will not work with non-taggable drivers: - Database
- File
- DynamoDB Requirements
Possible Package ConflictsAny packages that override The following are packages we have identified as conflicting:
Things That Don't Work CurrentlyThe following items currently do no work with this package: - caching of lazy-loaded relationships, see #127. Currently lazy-loaded belongs-to relationships are cached. Caching of other relationships is in the works.
- using select() clauses in Eloquent queries, see #238 (work-around discussed in the issue)
- using transactions. If you are using transactions, you will likely have to manually flush the cache, see [issue #305](https://github.com/GeneaLabs/laravel-model-caching/issues/305). InstallationBe sure to not require a specific version of this package when requiring it:
Gotchas If Using With LumenThe following steps need to be figured out by you and implemented in your Lumen app. Googling for ways to do this provided various approaches to this.
Upgrade Notes0.6.0The environment and config variables for disabling this package have changed.
0.5.0The following implementations have changed (see the respective sections below):
ConfigurationRecommended (Optional) Custom Cache StoreIf you would like to use a different cache store than the default one used by
your Laravel application, you may do so by setting the
UsageFor best performance a taggable cache provider is recommended (redis, memcached). While this is optional, using a non-taggable cache provider will mean that the entire cache is cleared each time a model is created, saved, updated, or deleted. For ease of maintenance, I would recommend adding a Here's an example <?php namespace App;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
abstract class BaseModel
{
use Cachable;
//
} Multiple Database ConnectionsThanks to @dtvmedia for suggestion this feature. This is actually a more robust solution than cache-prefixes. Keeping keys separate for multiple database connections is automatically handled. This is especially important for multi-tenant applications, and of course any application using multiple database connections. Optional Cache Key PrefixThanks to @lucian-dragomir for suggesting this feature! You can use cache key prefixing to keep cache entries separate for multi-tenant applications. For this it is recommended to add the Cachable trait to a base model, then set the cache key prefix config value there. Here's is an example: <?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class BaseModel extends Model
{
use Cachable;
protected $cachePrefix = "test-prefix";
} The cache prefix can also be set in the configuration to prefix all cached models across the board: 'cache-prefix' => 'test-prefix', Exception: User ModelI would not recommend caching the user model, as it is a special case, since it
extends Experimental: Cache Cool-down In Specific ModelsIn some instances, you may want to add a cache invalidation cool-down period. For example you might have a busy site where comments are submitted at a high rate, and you don't want every comment submission to invalidate the cache. While I don't necessarily recommend this, you might experiment it's effectiveness. To use it, it must be enabled in the model (or base model if you want to use it on multiple or all models): class MyModel extends Model
{
use Cachable;
protected $cacheCooldownSeconds = 300; // 5 minutes
// ...
} After that it can be implemented in queries: (new Comment)
->withCacheCooldownSeconds(30) // override default cooldown seconds in model
->get(); or: (new Comment)
->withCacheCooldownSeconds() // use default cooldown seconds in model
->get(); Disabling Caching of QueriesThere are two methods by which model-caching can be disabled:
Recommendation: use option #1 in all your seeder queries to avoid pulling in
cached information when reseeding multiple times.
You can disable a given query by using $results = $myModel->disableCache()->where('field', $value)->get(); Manual Flushing of Specific ModelYou can flush the cache of a specific model using the following artisan command: php artisan modelCache:clear --model=App\Model This comes in handy when manually making updates to the database. You could also trigger this after making updates to the database from sources outside your Laravel app. SummaryThat's all you need to do. All model queries and relationships are now cached! In testing this has optimized performance on some pages up to 900%! Most often you should see somewhere around 100% performance increase. Commitment to QualityDuring package development I try as best as possible to embrace good design and development practices, to help ensure that this package is as good as it can be. My checklist for package development includes:
ContributingPlease observe and respect all aspects of the included Code of Conduct https://github.com/GeneaLabs/laravel-model-caching/blob/master/CODE_OF_CONDUCT.md. Reporting IssuesWhen reporting issues, please fill out the included template as completely as possible. Incomplete issues may be ignored or closed if there is not enough information included to be actionable. Submitting Pull RequestsPlease review the Contribution Guidelines https://github.com/GeneaLabs/laravel-model-caching/blob/master/CONTRIBUTING.md. Only PRs that meet all criterium will be accepted.
If you |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论