Caching Queries & Result without Rerun Database

Control Over User Login Procedure - Cookies Authentication
Control Over User Login Procedure - Cookies Authentication

आज इस आर्टिकल में हम आपको Caching Queries & Result without Rerun Database के बारे में बताएंगे। 

Problem:- Caching Queries & Result without Rerun Database

Solution:-

Download Cache Lite PEAR Package

<?php 

require 'Cache/Lite.php';

$opts = array(
		'cacheDir'=> 'Cache',
		'automaticSerialization'=>true,
		'lifeTime'=>600

);
$cache = new Cache_lite($opts);

$db = new PDO("mysql:host=localhost; dbname=testdb", 'root', '');

$sql = "SELECT * FROM data WHERE name = ?";
$param = array($_GET['name']);
$key = cache_key($sql, $param);
$result = $cache->get($key);

if ($result=== false) {
	$stmt = $db->prepare($sql);
	$stmt->execute($param);
	$result = $stmt->fetchAll();
	$cache->save($result);
}

foreach ($result as $value) {
	print $value['id'] . ' : '. $value['name']. ' => ' . $value['mobile'].'<br>'; 
}

function cache_key($sql, $param){
	return md5($sql,
		implode(" | ", array_keys($param)) . implode(' | ', $param) 

	);
}

 ?>

Final Words

  • आज इस आर्टिकल में हमने आपको Caching Queries & Result without Rerun Database के बारे में बताया।
  • अगर आपको इससे जुडी कोई अन्य जानकारी चाहए है तो निचे कमेन्ट बॉक्स में कमेन्ट कर के पूछ सकते है।

इसे भी पढ़े – Blogger पर नई Post कैसे लिखे?

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *