Build high-performance Larravel applications through cache
to configure
Drive preparatory knowledge
Schema::create('cache', function($table) { $table->string('key')->unique(); $table->text('value'); $table->integer('expiration'); });
Note: You can also use Artisan commands
php artisan cache:table Generate migration through corresponding schema.
'memcached' => [ [ 'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100 ], ],
'memcached' => [ [ 'host' => '/var/run/memcached/memcached.sock', 'port' => 0, 'weight' => 100 ], ],
Cache Usage
Get cache instance
<? php namespace App\Http\Controllers; use Illuminate\Support\Facades\Cache; class UserController extends Controller { /** *Displays a list of all users of the application * * @return Response */ public function index() { $value = Cache::get('key'); // } }
$value = Cache::store('file')->get('foo'); Cache::store('redis')->put('bar', 'baz', 10);
Get data from cache
$value = Cache::get('key'); $value = Cache::get('key', 'default');
$value = Cache::get('key', function() { return DB::table(...)->get(); });
if (Cache::has('key')) { // }
Cache::increment('key'); Cache::increment('key', $amount); Cache::decrement('key'); Cache::decrement('key', $amount);
$value = Cache::remember('users', $minutes, function() { return DB::table('users')->get(); });
$value = Cache::rememberForever('users', function() { return DB::table('users')->get(); });
$value = Cache::pull('key');
Store data in cache
Cache::put('key', 'value', $minutes);
$expiresAt = Carbon::now()->addMinutes(10); Cache::put('key', 'value', $expiresAt);
Cache::add('key', 'value', $minutes);
Cache::forever('key', 'value');
Note: If you use the Memcached driver, the permanently stored data will be removed when the cache data reaches the upper limit.
Remove data from cache
Cache::forget('key');
Cache::flush();
Note: Clearing the cache does not matter what the cache key prefix is, but removes all data from the cache system, so you need to pay special attention when using this method if other applications share the cache with this application.
Cache Helper
$value = cache('key');
cache(['key' => 'value'], $minutes); cache(['key' => 'value'], Carbon::now()->addSeconds(10));
Test call
cache Function, you can Test facade Same use
Cache::shouldReceive method.
Cache Label
Note: Cache tags are not currently supported
file or
database Cache driven. In addition, when the cache using multiple tags is set to permanent storage
memcached The drive cache has the best performance, because Memcached will automatically clear stale records.
Store tagged cache items
Cache::tags(['people', 'artists'])->put('John', $john, $minutes); Cache::tags(['people', 'authors'])->put('Anne', $anne, $minutes);
Access tagged cache entries
$john = Cache::tags(['people', 'artists'])->get('John'); $anne = Cache::tags(['people', 'authors'])->get('Anne');
Remove tagged data items
Cache::tags(['people', 'authors'])->flush();
Cache::tags('authors')->flush();
Add a custom cache driver
Write Driver
<? php namespace App\Extensions; use Illuminate\Contracts\Cache\Store; class MongoStore implements Store { public function get($key) {} public function many(array $keys); public function put($key, $value, $minutes) {} public function putMany(array $values, $minutes); public function increment($key, $value = 1) {} public function decrement($key, $value = 1) {} public function forever($key, $value) {} public function forget($key) {} public function flush() {} public function getPrefix() {} }
Cache::extend('mongo', function($app) { return Cache::repository(new MongoStore); });
Note: If you are worried about where to put the custom cache driver code, you can
app Create a
Extensions Namespace. However, remember that Larravel does not have a strict application directory structure. You can freely organize the directory structure based on your needs.
Registration Driver
<? php namespace App\Providers; use App\Extensions\MongoStore; use Illuminate\Support\Facades\Cache; use Illuminate\Support\ServiceProvider; class CacheServiceProvider extends ServiceProvider { /** * Perform post-registration booting of services. * * @return void * @translator laravelacademy.org */ public function boot() { Cache::extend('mongo', function($app) { return Cache::repository(new MongoStore); }); } /** * Register bindings in the container. * * @return void */ public function register() { // } }
Cache Events
/** * The event listener mappings for the application. * * @var array */ protected $listen = [ 'Illuminate\Cache\Events\CacheHit' => [ 'App\Listeners\LogCacheHit', ], 'Illuminate\Cache\Events\CacheMissed' => [ 'App\Listeners\LogCacheMissed', ], 'Illuminate\Cache\Events\KeyForgotten' => [ 'App\Listeners\LogKeyForgotten', ], 'Illuminate\Cache\Events\KeyWritten' => [ 'App\Listeners\LogKeyWritten', ], ];