If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.

PHP Asynchronous Processing
Web applications में speed और performance को improve करने के लिए caching का use किया जाता है। Caching का concept यह है कि frequently accessed data को temporary storage में रखा जाये ताकि next time जब यह data access किया जाये तो उससे directly cache से fetch किया जा सके, न कि database या original source से।
इस blog में हम देखेंगे कैसे PHP application में Redis cache implement किया जाता है।
Caching एक technique है जिसमे data या resources को एक temporary storage location में store किया जाता है ताकि future requests के लिए उस data को quickly access किया जा सके। यह technique application कि performance को significantly improve करती है, क्योंकि cache से data access करना original source से access करने से काफी fast होता है।
●●●
Redis एक in-memory data structure store है जो different types of data structures जैसे strings, lists, sets, hashes, और more को support करता है। यह एक fast, open-source, in-memory key-value store है, जो caching के लिए widely use किया जाता है।
Redis का primary use-case high-performance caching है।
PHP में Redis use करने के लिए आपको पहले Redis server और PHP Redis extension install करना होगा।
sudo apt-get update sudo apt-get install redis-server
Redis के official website से Redis for Windows download करें।
Redis server को install और start करें।
इसके अलावा php/ext directory में php_redis.dll file check कर लें , अगर ये file नहीं है तो https://pecl.php.net/package/redis से download करके php/ext directory में रखें फिर php.ini file में enable कर दे।
●●●
अब जब Redis server और PHP extension install हो चुके हैं, तो आप PHP में Redis use कर सकते हैं।
सबसे पहले, आपको Redis client create करना होगा जो Redis server से connect करेगा।
$redis = new Redis();
$redis->connect('127.0.0.1', 6379); // Default Redis port is 6379आप Redis में simple key-value pairs store कर सकते हैं -
$redis->set("user:1000", "Rahul");
echo "Stored in Redis: " . $redis->get("user:1000");यहां , "user:1000" एक key है और "Rahul" एक value. आप get() method का use करके stored value को Redis से retrieve कर सकते हैं।
Redis में आप keys के लिए expiry time भी set कर सकते हैं , यह useful होता है जब आपको cache को automatically किसी specific time के बाद invalidate करना हो।
$redis->setex("session:12345", 3600, "user data"); // Expires in 1 hourRedis strings के अलावा complex data structures भी store कर सकता है जैसे lists, sets, और hashes.
$redis->lpush("tasks", "Task 1");
$redis->lpush("tasks", "Task 2");
$tasks = $redis->lrange("tasks", 0, -1);
print_r($tasks); // Output: Array ( [0] => Task 2 [1] => Task 1 )●●●
Redis को आप cache के रूप में भी use कर सकते हैं , जैसे अगर आपको किसी expensive database query का result cache करना हो, तो आप यह कर सकते हैं।
// Check if data exists in cache
$cacheKey = "users:all";
$users = $redis->get($cacheKey);
if (!$users) {
// Fetch from database (simulated with an array here)
$users = json_encode([
['id' => 1, 'name' => 'John Doe'],
['id' => 2, 'name' => 'Jane Doe'],
]);
// Store in Redis cache
$redis->set($cacheKey, $users);
$redis->expire($cacheKey, 3600); // Cache for 1 hour
echo "Fetched from database\n";
} else {
echo "Fetched from Redis cache\n";
}
// Use the data
$users = json_decode($users, true);
print_r($users);यहां , अगर users:all key Redis cache में available नहीं है तो data database से fetch किया जाता है और Redis में store किया जाता है ताकि next request में directly cache से fetch किया जा सके।
अगर आपको Redis cache clear करना हो, तो आप flushall या flushdb commands का use कर सकते हैं।
$redis->flushall(); // Clear all keys from all databases●●●
Redis एक powerful caching tool है जो PHP applications में performance boost करने के लिए use किया जा सकता है। Redis के साथ, आप easily frequently accessed data को cache कर सकते हैं और अपनी application कि speed को significantly improve कर सकते हैं।
Caching, especially Redis cache, large-scale web applications के लिए एक must-have optimization है।
Loading ...