Ingantacciyar Cache Amfani a cikin Laravel: Inganta Ayyuka

Yin amfani Cache da inganci a ciki Laravel dabara ce mai ƙarfi don haɓaka aikin aikace-aikacen ku ta hanyar rage tambayoyin bayanai da haɓaka saurin amsawa. Laravel yana ba da goyon baya mai ginawa don caching, yana sauƙaƙa aiwatarwa da sarrafawa.

Anan ga jagorar mataki-mataki kan yadda ake amfani da su Cache yadda ya kamata a Laravel:

Kanfigareshan

Tabbatar cewa Laravel an saita aikace-aikacen ku da kyau don amfani da caching. Laravel yana goyan bayan cache direbobi daban-daban kamar Fayil, Database, Mem cache d, Redis, da sauransu. Zaɓi cache direban da ya dace bisa buƙatun aikace-aikacenku da saitin sabar.

 

Caching Data

Yi amfani da Cache facade don adanawa da dawo da bayanai daga cache. Caching bayanai masu tsada ko ake samu akai-akai na iya rage buƙatar maimaita tambayoyin bayanai. Ga misalin sakamakon binciken caching:

$users = Cache::remember('cached-users', $minutes, function() {  
    return User::all(); // Expensive query that will be cached for $minutes  
});  

 

Saitin Cache Karewa

Lokacin adana bayanai, saita lokacin ƙarewar da ya dace don tabbatar da cewa an cache sabunta bayanan lokaci-lokaci. Wannan yana hana shigar da bayanan da suka lalace ga masu amfani. A cikin misalin da ke sama, $minutes shine tsawon lokacin da bayanai za su kasance cache d kafin a sabunta su.

 

Cache Tags

Laravel yana goyan bayan cache tags, yana ba ku damar haɗa cache bayanai masu alaƙa tare. Wannan yana sauƙaƙa sarrafa da soke cache bayanan d lokacin da takamaiman al'amura suka faru.

Misali:

Cache::tags(['users', 'admins'])->put('user-1', $user, $minutes);

 

Cache Share:

Share bayanan cache lokacin da ya cancanta don ci gaba da sabunta bayanai.

Misali, bayan sabunta ko share bayanai daga ma'ajin bayanai, zaku iya cire cache bayanan d masu dacewa don gujewa ba da bayanan da suka gabata.

Cache::forget('cached-users'); // Remove cached users data

 

Cache a Route mataki

Don takamaiman route s waɗanda suke da tsadar lissafi ko kuma ba safai ake canzawa ba, zaku iya cache amsa duka. Laravel 's route middleware yana ba da hanya mai sauƙi don cache route amsawa..

Route::get('/expensive-route', function() {  
    // Cache response for 60 minutes  
})->middleware('cacheResponse:60');

 

Ta amfani Cache da inganci a cikin Laravel, zaku iya rage kaya akan bayananku, haɓaka lokutan amsawa, kuma a ƙarshe ƙirƙirar aikace-aikacen da ya fi dacewa da masu amfani. Ka tuna don zaɓar dabarar ɓoye da ta dace dangane da takamaiman buƙatun aikace-aikacenku.