/** * Lists posts for a category * @param string $slug Category slug * @return \Cake\Network\Response|null|void * @throws RecordNotFoundException */ public function view($slug = null) { //The category can be passed as query string, from a widget if ($this->request->query('q')) { return $this->redirect([$this->request->query('q')]); } $page = $this->request->query('page') ? $this->request->query('page') : 1; //Sets the cache name $cache = sprintf('category_%s_limit_%s_page_%s', md5($slug), $this->paginate['limit'], $page); //Tries to get data from the cache list($posts, $paging) = array_values(Cache::readMany([$cache, sprintf('%s_paging', $cache)], $this->PostsCategories->cache)); //If the data are not available from the cache if (empty($posts) || empty($paging)) { $query = $this->PostsCategories->Posts->find('active')->select(['id', 'title', 'subtitle', 'slug', 'text', 'created'])->contain(['Categories' => function ($q) { return $q->select(['id', 'title', 'slug']); }, 'Tags' => function ($q) { return $q->order(['tag' => 'ASC']); }, 'Users' => function ($q) { return $q->select(['first_name', 'last_name']); }])->where(['Categories.slug' => $slug])->order([sprintf('%s.created', $this->PostsCategories->Posts->alias()) => 'DESC']); if ($query->isEmpty()) { throw new RecordNotFoundException(__d('me_cms', 'Record not found')); } $posts = $this->paginate($query)->toArray(); //Writes on cache Cache::writeMany([$cache => $posts, sprintf('%s_paging', $cache) => $this->request->param('paging')], $this->PostsCategories->cache); //Else, sets the paging parameter } else { $this->request->params['paging'] = $paging; } $this->set(am(['category' => $posts[0]->category], compact('posts'))); }
/** * testReadMany method * * @return void */ public function testReadMany() { $this->_configCache(['duration' => 2]); $data = ['App.falseTest' => false, 'App.trueTest' => true, 'App.nullTest' => null, 'App.zeroTest' => 0, 'App.zeroTest2' => '0']; foreach ($data as $key => $value) { Cache::write($key, $value, 'memcached'); } $read = Cache::readMany(array_merge(array_keys($data), ['App.doesNotExist']), 'memcached'); $this->assertSame($read['App.falseTest'], false); $this->assertSame($read['App.trueTest'], true); $this->assertSame($read['App.nullTest'], null); $this->assertSame($read['App.zeroTest'], 0); $this->assertSame($read['App.zeroTest2'], '0'); $this->assertSame($read['App.doesNotExist'], false); }
/** * testDeleteMany method * * @return void */ public function testDeleteMany() { $this->_configCache(); $data = array('App.falseTest' => false, 'App.trueTest' => true, 'App.nullTest' => null, 'App.zeroTest' => 0, 'App.zeroTest2' => '0'); Cache::writeMany(array_merge($data, array('App.keepTest' => 'keepMe')), 'tests'); Cache::deleteMany(array_keys($data), 'tests'); $read = Cache::readMany(array_merge(array_keys($data), array('App.keepTest')), 'tests'); $this->assertSame($read['App.falseTest'], false); $this->assertSame($read['App.trueTest'], false); $this->assertSame($read['App.nullTest'], false); $this->assertSame($read['App.zeroTest'], false); $this->assertSame($read['App.zeroTest2'], false); $this->assertSame($read['App.keepTest'], 'keepMe'); }
/** * Searches posts * @return void * @uses MeCms\Controller\AppController::_checkLastSearch() */ public function search() { $pattern = $this->request->query('p'); if ($pattern) { //Checks if the pattern is at least 4 characters long if (strlen($pattern) >= 4) { if ($this->_checkLastSearch($pattern)) { $this->paginate['limit'] = config('default.records_for_searches'); $page = $this->request->query('page') ? $this->request->query('page') : 1; //Sets the initial cache name $cache = sprintf('search_%s', md5($pattern)); //Updates the cache name with the query limit and the number of the page $cache = sprintf('%s_limit_%s', $cache, $this->paginate['limit']); $cache = sprintf('%s_page_%s', $cache, $page); //Tries to get data from the cache list($posts, $paging) = array_values(Cache::readMany([$cache, sprintf('%s_paging', $cache)], $this->Posts->cache)); //If the data are not available from the cache if (empty($posts) || empty($paging)) { $query = $this->Posts->find('active')->select(['title', 'slug', 'text', 'created'])->where(['OR' => ['title LIKE' => sprintf('%%%s%%', $pattern), 'subtitle LIKE' => sprintf('%%%s%%', $pattern), 'text LIKE' => sprintf('%%%s%%', $pattern)]])->order([sprintf('%s.created', $this->Posts->alias()) => 'DESC']); $posts = $this->paginate($query)->toArray(); //Writes on cache Cache::writeMany([$cache => $posts, sprintf('%s_paging', $cache) => $this->request->param('paging')], $this->Posts->cache); //Else, sets the paging parameter } else { $this->request->params['paging'] = $paging; } $this->set(compact('posts')); } else { $this->Flash->alert(__d('me_cms', 'You have to wait {0} seconds to perform a new search', config('security.search_interval'))); } } else { $this->Flash->alert(__d('me_cms', 'You have to search at least a word of {0} characters', 4)); } } $this->set(compact('pattern')); }