Redis a cikin Laravel: Gudanar da Ayyukan Bayanai da Inganta Ayyuka

Redis babban katafaren adana bayanai ne mai ƙarfi kuma sanannen wurin adana bayanai da ake amfani da shi don adanawa da sarrafa bayanan wucin gadi a cikin aikace-aikacen yanar gizo. A cikin Laravel, ɗaya daga cikin shahararrun tsarin tsarin PHP, zaka iya amfani da shi cikin sauƙi Redis don gudanar da ayyukan bayanai yadda ya kamata.

A ƙasa akwai wasu ayyukan gama gari na bayanai tare da Redis a cikin Laravel:

Ajiye bayanai a ciki Redis

Kuna iya amfani da set aikin don adana maɓalli-daraja biyu a cikin Redis:

use Illuminate\Support\Facades\Redis;  
  
Redis::set('name', 'John Doe');

Ana dawo da bayanai daga Redis

Kuna iya amfani da get aikin don dawo da ƙima daga Redis maɓalli:

use Illuminate\Support\Facades\Redis;  
  
$name = Redis::get('name'); // Result: "John Doe"

Share bayanai daga Redis

Kuna iya amfani da del aikin don share maɓalli da madaidaicin ƙimarsa daga Redis:

use Illuminate\Support\Facades\Redis;  
  
Redis::del('name');

Duban wanzuwar Data a ciki Redis

Kuna iya amfani da exists aikin don bincika idan maɓalli yana cikin Redis:

use Illuminate\Support\Facades\Redis;  
  
if(Redis::exists('name')) {  
    // Key exists in Redis  
} else {  
    // Key does not exist in Redis  
}  

Ajiye bayanai tare da Lokaci-To-Rayuwa(TTL)

Kuna iya amfani da setex aikin don adana maɓalli-darajar biyu tare da lokaci-zuwa-rayuwa(TTL) a cikin Redis:

use Illuminate\Support\Facades\Redis;  
  
Redis::setex('token', 3600, 'abc123'); // Store the key 'token' with value 'abc123' for 1 hour

Ajiye Bayanai azaman Jerin

Redis yana goyan bayan adana bayanai azaman jeri. Kuna iya amfani da ayyuka kamar lpush, rpush, lpop, rpop don ƙarawa da cire abubuwa daga lissafin:

use Illuminate\Support\Facades\Redis;  
  
Redis::lpush('tasks', 'task1'); // Add 'task1' to the beginning of the list 'tasks'
Redis::rpush('tasks', 'task2'); // Add 'task2' to the end of the list 'tasks'  
  
$task1 = Redis::lpop('tasks'); // Get the first element of the list 'tasks'  
$task2 = Redis::rpop('tasks'); // Get the last element of the list 'tasks'

Adana Bayanai azaman Saiti

Redis Hakanan yana goyan bayan adana bayanai azaman saiti. Kuna iya amfani da ayyuka kamar sadd, srem, smembers don ƙarawa, cirewa, da dawo da abubuwa daga saitin:

use Illuminate\Support\Facades\Redis;  
  
Redis::sadd('users', 'user1'); // Add 'user1' to the set 'users'
Redis::sadd('users', 'user2'); // Add 'user2' to the set 'users'  
  
Redis::srem('users', 'user2'); // Remove 'user2' from the set 'users'  
  
$members = Redis::smembers('users'); // Get all elements from the set 'users'

Ajiye Bayanai azaman Hash

Redis yana goyan bayan adana bayanai azaman hash, inda kowane maɓalli ke da alaƙa da saitin filaye da ƙima. Kuna iya amfani da ayyuka kamar hset, hget, hdel, hgetall don ƙarawa, dawo da, da cire filaye a cikin hash:

use Illuminate\Support\Facades\Redis;  
  
Redis::hset('user:1', 'name', 'John Doe'); // Add the field 'name' with value 'John Doe' to the hash 'user:1'
Redis::hset('user:1', 'email', '[email protected]'); // Add the field 'email' with value '[email protected]' to the hash 'user:1'  
  
$name = Redis::hget('user:1', 'name'); // Get the value of the field 'name' in the hash 'user:1'  
  
Redis::hdel('user:1', 'email'); // Remove the field 'email' from the hash 'user:1'  
  
$fields = Redis::hgetall('user:1'); // Get all fields and values in the hash 'user:1'

Gudanar da Ayyuka Bisa ga Transaction

Redis yana tallafawa ma'amaloli don gudanar da ayyukan bayanai cikin aminci kuma akai-akai. Kuna iya amfani da multi ayyuka exec don farawa da ƙarewa transaction:

use Illuminate\Support\Facades\Redis;  
  
Redis::multi(); // Begin the transaction  
  
Redis::set('name', 'John Doe');
Redis::set('email', '[email protected]');  
  
Redis::exec(); // End the transaction, operations will be executed atomically

 

Ƙarshe Amfani Redis a cikin Laravel yana ba ku damar sarrafa ayyukan bayanai da kyau da haɓaka aikin aikace-aikacen ku. Ta amfani da mahimman ayyukan bayanai da abubuwan ci-gaba na Redis, zaku iya adanawa da sarrafa bayanai yadda ya kamata, haɓaka aikin aikace-aikacen, da haɓaka ƙwarewar mai amfani.