public function testInvalidate() { SlugCache::set('User', array(1 => 2)); SlugCache::set('Route', array(3 => 4)); SlugCache::invalidate('User'); $this->assertFalse(Cache::read('User', 'SluggerTest')); $expected = array(3 => 4); $result = Cache::read('Route', 'SluggerTest'); $this->assertEquals($expected, $result); $expected = array('SluggerTest' => array('Route' => array(3 => 4))); $result = SlugCache::$_varCache; $this->assertEquals($expected, $result); }
/** * Gets slugs, checks cache first * * @param string $modelName The name of the model * @return array Array of slugs */ public function getSlugs($modelName) { $slugs = SlugCache::get($modelName); if (empty($slugs)) { $Model = ClassRegistry::init($modelName, true); if ($Model === false) { return false; } $field = $Model->displayField; if (!empty($this->models[$modelName]['slugField'])) { $field = $this->models[$modelName]['slugField']; } $slugs = $Model->find('all', array('fields' => array($Model->name . '.' . $Model->primaryKey, $Model->name . '.' . $field), 'recursive' => -1)); $counts = $Model->find('all', array('fields' => array('LOWER(TRIM(' . $Model->name . '.' . $field . ')) AS ' . $field, 'COUNT(*) AS count'), 'group' => array($field))); $counts = Set::combine($counts, '{n}.0.' . $field, '{n}.0.count'); $listedSlugs = array(); foreach ($slugs as $pk => $fields) { $values = array('_field' => $fields[$Model->name][$field], '_count' => $counts[strtolower(trim($fields[$Model->name][$field]))], '_pk' => $fields[$Model->name][$Model->primaryKey]); $listedSlugs[$fields[$Model->name][$Model->primaryKey]] = $this->slug($values); } SlugCache::set($modelName, $listedSlugs); } return SlugCache::get($modelName); }