I’m trying to get my caching working correctly so that I don’t need to create a new instance of memcache every time I want to cache a query in a model function:
class contentModel {
function getData() {
$core = Connect::getInstance();
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$data = $memcache->get("query".$sql);
if($!data)
// get data
return $data;
}
}
If in my root index.php I add:
global $memcache;
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
Then would I just be able to call $memcache->get($key) throughout all classes without have to re instance it?
Or is there another way I should go about simplifying this?
You could make
$memcacheda static member of your model base class, for example: