giver/cacheable
Composer 安装命令:
composer require giver/cacheable
包简介
Query caching for Laravel 5
README 文档
README
Cacheable is an Eloquent trait for Laravel 5.0+ that brings back the cache() query functions from Laravel 4.2. This makes it super easy to cache your query results for an adjustable amount of time.
// Get a the first user's posts and cache them for a day.
User::first()->cache(1440)->posts()->get();
It works by simply cacheing the SQL query that was used and storing the result. If the same query is attempted while the cache is persisted it will be retrieved from the store instead of hitting your database again.
Installation
Install using Composer, just as you would anything else.
composer require giver/cacheable
The easiest way to get started with Eloquent is to create an abstract App\Model which you can extend your application models from. In this base model you can import the cacheable trait which will extend the same caching functionality to any queries you build off your model.
<?php
namespace App;
use Giver\Cacheable\Cacheable;
use Illuminate\Database\Eloquent\Model as Eloquent;
abstract class Model extends Eloquent
{
use Cacheable;
}
Now, just ensure that your application models from this new App\Model instead of Eloquent.
<?php
namespace App;
class Post extends Model
{
//
}
Alternatively, you can simply apply the trait to each and every model you wish to use cache() on.
Usage
Using the cache method is super simple. Just pass the number of minutes you want to store the result of that query in the cache for, and whenever the same query is called within that time frame the result will be pulled from the cache, rather than from the database again.
// Cache the number of users for an hour.
$users = User::cache(60)->count();
Cache tags
If you want to tag certain queries you can add cacheTags('tag_name') to your query. Please notice that cache tags are not supported by all cache drivers.
// Cache the number of users for an hour and tag it with 'user_queries'
User::cache(60)->cacheTags('user_queries')->count();
Cache prefix
If you want a unique prefix added to the cache key for each of your queries (say, if your cache doesn't support tagging), you can add prefix('prefix') to your query.
// Cache the number of users for an hour and prefix the key with 'users'
User::cache(60)->prefix('users')->count();
Alternatively, you can add the ``$cacheCachePrefix` property to your model to always use that cache prefix.
Model wide cache tag
You can set a cache tag for all queries of a model by setting the $cacheCacheTag property with an unique string that should be used to tag the queries.
Relationships
Validating works by caching queries on a query-by-query basis. This means that when you perform eager-loading those additional queries will not be cached as well unless explicitly specified. You can do that by using a callback with your eager-loads.
$users = User::where("id", ">", "1")
->with(['posts' => function ($q) { $q->cache(10); }])
->cache(10)
->take(5)
->get();
Always enable
You can opt-in to cache all queries of a model by setting the $cacheFor property with the number of minutes you want to cache results for. Use this feature with caution as it could lead to unexpected behaviour and stale data in your app if you're not familiar with how it works.
Cache flushing
Based on the architecture of the package it's not possible to delete the cache for a single query. But if you tagged any queries using cache tags, you are able to flush the cache for the tag:
User::flushCache('user_queries');
If you used the $cacheCacheTag property you can use the method without a parameter and the caches for the tag set by $cacheCacheTag are flushed:
User::flushCache();
giver/cacheable 适用场景与选型建议
giver/cacheable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 293 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 10 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cache」 「caching」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 giver/cacheable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 giver/cacheable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 giver/cacheable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Query caching for Laravel 5
Laravel 5 - Repositories to the database layer
Simple PHP caching system that uses a tmp folder in a Linux environment.
Provides support for PSR-6 compatible cache for Yii1 application
Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain.
统计信息
- 总下载量: 293
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-10-04