0
因此,我將Redis添加到已開發的項目中,並且想知道在哪裏放置這些緩存調用。 有現有的模式,我想知道如果我可以注入的Redis到模型,然後用包緩存代碼每個查詢,像這樣:高速緩存調用在MVC中的位置
$cacheKey = "table/{$id}";
// If table entity is not in cache
if (!$predis->exists($cacheKey)) {
// Pre-existing database code
$this->db->query('SELECT * FROM table WHERE table.id = "'.$id.'" ');
$query = $this->db->get();
$result = $query->result_array();
// Set entity in redis cache
$predis->set($cacheKey, json_encode($result[0]));
return $result[0];
}
// Return cached entity from redis
return json_decode($predis->get($cacheKey), true);
但我只是想知道,如果這是一個骯髒的黑客,或者實際上是做事的最佳方式,並且它是放置緩存代碼的最合適的地方? 我從以前的項目中瞭解到,最好是以正確的方式做事,第一次!