Cakephp 3 - 限制缓存查询

时间:2017-07-03 12:40:58

标签: mysql caching cakephp-3.0

我有一个主题列表,默认情况下为15,还有一个加载更多按钮,可以使用ajax请求加载15个主题。初始查询需要花费太多时间才能获得前15个主题,因此我缓存了查询,如下所示:

$query = "SELECT *, views + views_rom + views_eng + views_rus as views
    FROM topics Topic
    LEFT JOIN topics_practices  TopicsPractice on (TopicsPractice.topic_id = Topic.id  )
    LEFT JOIN practices  Practice on (TopicsPractice.practice_id = Practice.id)
    LEFT JOIN topic_comments  TopicComment on (TopicComment.topic_id = Topic.id )
    LEFT JOIN slugs  Slug on (Slug.table = 'topics' AND Slug.item_id = Topic.id)
    WHERE Topic.id IN 
        (SELECT id FROM (
            SELECT  t.id
            FROM topics t 
            LEFT JOIN topic_comments tc on (tc.topic_id = t.id ) 
            LEFT JOIN topics_practices tp on (tp.topic_id = t.id ) 
            LEFT JOIN topics_tags  TopicsTag on (TopicsTag.topic_id = t.id)  
            WHERE $conditions
            GROUP BY t.id 
            ORDER BY $orderSubsidiary
            LIMIT $limit
        ) temp) 
    ORDER BY $order    
   ";

$topics = Cache::remember(str_replace(' ','_',$conditions).'_setTopics_cache', function() use ($query) { $topics = $this->query($query); }, '5min');

我遇到的问题是,每次我发出ajax请求时,我都会回到前15个主题。我的查询限制不会改变。 有没有办法在我的缓存查询中更改限制值?

1 个答案:

答案 0 :(得分:1)

更改缓存密钥以包含某种形式的限制,以便每个查询都有一个唯一的密钥。当您的查询发生更改时,密钥也需要更改,以便您可以获取并缓存这些结果,而不是使用先前查询的先前缓存集。