adding caching

This commit is contained in:
John Bintz 2009-11-06 07:33:06 -05:00
parent b2198c25c2
commit 651db07359
5 changed files with 105 additions and 68 deletions

View File

@ -203,6 +203,13 @@ class ComicPressComicPost {
update_post_meta($this->post->ID, 'comic_ordering', $new_order);
}
function get_parent_categories() {
$parents = wp_get_post_categories($this->post->ID);
do {
$parents
} while ($parents_found);
}
}
?>

View File

@ -4,7 +4,7 @@ require_once('ComicPress.inc');
require_once('ComicPressDBInterface.inc');
class ComicPressStoryline {
var $_structure, $root_category;
var $_structure;
var $_category_search;
function read_from_options() {
@ -52,64 +52,76 @@ class ComicPressStoryline {
* @return boolean True if the structure was valid.
*/
function create_structure($structure) {
$new_structure = array();
$parent = null;
$all_leaves = array();
$this->root_category = false;
$adjacents_by_parent = array();
if (is_string($structure)) {
$structure = explode(',', $structure);
}
if (is_array($structure)) {
$is_valid = true;
foreach ($structure as $branch) {
if (is_string($branch)) {
$parts = explode('/', $branch);
$valid = false;
if (count($parts) > 1) {
if ($parts[0] == '0') { $valid = true; }
}
if (!$valid) {
$is_valid = false; break;
} else {
$data = array();
$leaf = end($parts);
$all_leaves[] = $leaf;
$data['level'] = count($parts) - 1;
if (count($parts) > 2) {
$parent = $parts[count($parts) - 2];
if (!isset($adjacents_by_parent[$parent])) {
$adjacents_by_parent[$parent] = array();
}
$adjacents_by_parent[$parent][] = $leaf;
$data['parent'] = $parent;
} else {
$this->root_category = $leaf;
}
$new_structure[$leaf] = $data;
}
} else {
$is_valid = false; break;
}
$key = null;
if (is_string($structure)) {
$key = $structure;
$structure = explode(',', $structure);
} else {
if (is_array($structure)) {
$key = implode(',', $structure);
}
if ($is_valid) {
for ($i = 0; $i < count($all_leaves); ++$i) {
foreach (array('previous' => -1, 'next' => 1) as $type => $dir) {
if (isset($all_leaves[$i + $dir])) {
$new_structure[$all_leaves[$i]][$type] = $all_leaves[$i + $dir];
}
if (!is_null($key)) {
$key = "storyline-structure-${key}";
if (($result = wp_cache_get($key, 'comicpress')) !== false) {
$this->_structure = $result;
} else {
$new_structure = array();
$parent = null;
$all_leaves = array();
$adjacents_by_parent = array();
if (is_array($structure)) {
$is_valid = true;
foreach ($structure as $branch) {
if (is_string($branch)) {
$parts = explode('/', $branch);
$valid = false;
if (count($parts) > 1) {
if ($parts[0] == '0') { $valid = true; }
}
if (!$valid) {
$is_valid = false; break;
} else {
$data = array();
$leaf = end($parts);
$all_leaves[] = $leaf;
$data['level'] = count($parts) - 1;
if (count($parts) > 2) {
$parent = $parts[count($parts) - 2];
if (!isset($adjacents_by_parent[$parent])) {
$adjacents_by_parent[$parent] = array();
}
$adjacents_by_parent[$parent][] = $leaf;
$data['parent'] = $parent;
}
$new_structure[$leaf] = $data;
}
} else {
$is_valid = false; break;
}
}
}
if ($is_valid) {
for ($i = 0; $i < count($all_leaves); ++$i) {
foreach (array('previous' => -1, 'next' => 1) as $type => $dir) {
if (isset($all_leaves[$i + $dir])) {
$new_structure[$all_leaves[$i]][$type] = $all_leaves[$i + $dir];
}
}
}
$this->_structure = $new_structure;
$this->_structure = $new_structure;
}
}
wp_cache_set($key, $this->_structure, 'comicpress');
}
}
return is_array($this->_structure);

View File

@ -161,6 +161,10 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected_result, get_post_meta(1, 'comic_ordering', true));
}
function testFindParents() {
}
}
?>

View File

@ -17,7 +17,6 @@ class ComicPressNavigationTest extends PHPUnit_Framework_TestCase {
$dbi = $this->getMock('ComicPressDBInterface', array('get_previous_comic', 'get_next_comic', 'get_first_comic', 'get_last_comic'));
$storyline = new ComicPressStoryline();
$storyline->root_category = 1;
$storyline->_structure = array(
'1' => array('next' => 2),
'2' => array('previous' => 1, 'next' => 3),

View File

@ -35,13 +35,11 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
),
array(
array('0/1'),
array('1' => array('level' => 1)),
1
array('1' => array('level' => 1))
),
array(
array('0/1', '0/1/2'),
array('1' => array('next' => 2, 'level' => 1), '2' => array('parent' => 1, 'previous' => 1, 'level' => 2)),
1
array('1' => array('next' => 2, 'level' => 1), '2' => array('parent' => 1, 'previous' => 1, 'level' => 2))
),
array(
array('0/1', '0/1/2', '0/1/3'),
@ -49,8 +47,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
'1' => array('next' => 2, 'level' => 1),
'2' => array('parent' => 1, 'previous' => 1, 'next' => 3, 'level' => 2),
'3' => array('parent' => 1, 'previous' => 2, 'level' => 2),
),
1
)
),
array(
array('0/1', '0/1/2', '0/1/2/3', '0/1/2/4', '0/1/5'),
@ -60,8 +57,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
'3' => array('parent' => 2, 'next' => 4, 'previous' => 2, 'level' => 3),
'4' => array('parent' => 2, 'next' => 5, 'previous' => 3, 'level' => 3),
'5' => array('parent' => 1, 'previous' => 4, 'level' => 2),
),
1
)
),
array(
array('0/1', '0/1/2', '0/1/2/3', '0/1/2/4', '0/1/5', '0/1/5/6', '0/1/5/7', '0/1/5/8', '0/1/9'),
@ -75,19 +71,38 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
'7' => array('parent' => 5, 'next' => 8, 'previous' => 6, 'level' => 3),
'8' => array('parent' => 5, 'next' => 9, 'previous' => 7, 'level' => 3),
'9' => array('parent' => 1, 'previous' => 8, 'level' => 2),
),
1
)
),
);
}
/**
* @dataProvider providerTestCreateStorylineStructure
* @group nocache
*/
function testCreateStorylineStructure($input, $expected_structure, $expected_root_category) {
function testCreateStorylineStructure($input, $expected_structure) {
global $wp_object_cache;
$this->assertTrue(empty($wp_object_cache->cache['comicpress']));
$this->assertEquals(is_array($expected_structure), $this->css->create_structure($input));
$this->assertEquals($expected_structure, $this->css->_structure);
if ($expected_structure !== false) {
$this->assertTrue(!empty($wp_object_cache->cache['comicpress']));
}
}
/**
* @dataProvider providerTestCreateStorylineStructure
* @group cache
*/
function testCreateStorylineStructureFromCache($input, $expected_structure) {
if (is_string($input)) {
wp_cache_set("storyline-structure-${input}", $expected_structure, 'comicpress');
}
$this->assertEquals(is_array($expected_structure), $this->css->create_structure($input));
$this->assertEquals($expected_structure, $this->css->_structure);
$this->assertEquals($expected_root_category, $this->css->root_category);
}
function providerTestGetFields() {