Caching kayan aiki ne mai mahimmanci don haɓaka aikin aikace-aikacen yanar gizo. A cikin Laravel, Redis yana ɗaya daga cikin shahararrun hanyoyin caching da ake amfani da su don adana bayanan wucin gadi da rage lokacin neman bayanai.
Farawa tare da Redis shiga Laravel
Don amfani Redis azaman cache a cikin Laravel, da farko kuna buƙatar shigarwa Redis kuma tabbatar da Laravel an daidaita shi don amfani da shi. Kuna iya shigarwa Redis ta hanyar sarrafa fakitin tsarin aiki ko daga Redis gidan yanar gizon hukuma.
Bayan shigarwa, kuna buƙatar gyara .env
fayil ɗin sanyi Laravel da samar da Redis bayanan haɗin kamar haka:
CACHE_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
Basic caching tare da Redis in Laravel
A cikin Laravel, zaku iya amfani da ayyuka kamar Cache::put
, Cache::get
, Cache::remember
, da ƙari don yin hulɗa da su Redis don caching.
Ajiye bayanai a cikin Redis:
Cache::put('key', 'value', $expirationInSeconds);
Ana dawo da bayanai daga Redis:
$value = Cache::get('key');
Maido da bayanai daga Redis ko caching idan babu:
$value = Cache::remember('key', $expirationInSeconds, function() {
// Perform data retrieval from the database or other data sources
return User::all();
});
Amfanin Amfani Redis azaman Cache
Amfani Redis azaman cache a ciki Laravel yana ba da fa'idodi da yawa, gami da:
- Ingantattun Ayyuka: Rage lokacin dawo da bayanai yana haifar da saurin aiwatar da aikace-aikacen da ingantaccen aiki.
- Rage Load ɗin Database: Ana adana bayanan wucin gadi a cikin Redis, rage adadin tambayoyin bayanai da haɓaka ingantaccen tsarin.
Kết luận Redis kayan aiki ne mai ƙarfi don amfani azaman cache a cikin Laravel aikace-aikacen ku. Yin amfani da shi Redis azaman hanyar caching yana taimakawa haɓaka aiki da haɓaka ƙwarewar mai amfani a cikin aikace-aikacen yanar gizon ku. Wannan labarin ya yi niyya don ba ku kyakkyawar fahimtar amfani da Redis ciki Laravel da amfani da shi ga ayyukan ku don haɓaka aiki da haɓaka ƙwarewar mai amfani.