add caching to simple structure

This commit is contained in:
John Bintz 2010-01-14 20:47:12 -05:00
parent d1193abfb9
commit 7ba5fc8ba9
2 changed files with 73 additions and 22 deletions

View File

@ -255,6 +255,12 @@ class ComicPressStoryline {
* Get a simple structure. * Get a simple structure.
*/ */
function get_category_simple_structure($parent = null) { function get_category_simple_structure($parent = null) {
$cache_key = $this->generate_cache_key('storyline-structure', $parent);
$result = wp_cache_get($cache_key, 'comicpress');
if (is_array($result)) {
return $result;
}
$structure = array(); $structure = array();
$dbi = $this->get_comicpress_dbi(); $dbi = $this->get_comicpress_dbi();
@ -277,9 +283,18 @@ class ComicPressStoryline {
} }
} }
wp_cache_set($cache_key, $structure, 'comicpress');
return $structure; return $structure;
} }
function generate_cache_key($key_name, $param) {
if (!empty($param)) {
$key_name = "${key_name}-${param}";
}
return $key_name;
}
/** /**
* Get a flattened category node list. * Get a flattened category node list.
*/ */

View File

@ -7,6 +7,7 @@ require_once('ComicPressStoryline.inc');
class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
function setUp() { function setUp() {
_reset_wp(); _reset_wp();
wp_cache_flush();
$this->css = new ComicPressStoryline(); $this->css = new ComicPressStoryline();
} }
@ -282,7 +283,44 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected, $this->css->_merge_simple_storyline($original)); $this->assertEquals($expected, $this->css->_merge_simple_storyline($original));
} }
function testGetCategorySimpleStructure() { function providerTestGetCategorySimpleStructure() {
return array(
array(
1,
array(
'0' => array(
'1' => array(
'2' => array(
'3' => true,
'4' => true
)
)
)
),
'storyline-structure-1'
),
array(
null,
array(
'0' => array(
'1' => array(
'2' => array(
'3' => true,
'4' => true
)
),
'5' => true
)
),
'storyline-structure'
)
);
}
/**
* @dataProvider providerTestGetCategorySimpleStructure
*/
function testGetCategorySimpleStructure($parent, $expected_result, $expected_cache_key) {
$css = $this->getMock('ComicPressStoryline', array('get_comicpress_dbi')); $css = $this->getMock('ComicPressStoryline', array('get_comicpress_dbi'));
$dbi = $this->getMock('ComicPressDBInterface', array('get_parent_child_category_ids')); $dbi = $this->getMock('ComicPressDBInterface', array('get_parent_child_category_ids'));
@ -293,28 +331,26 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
$css->expects($this->any())->method('get_comicpress_dbi')->will($this->returnValue($dbi)); $css->expects($this->any())->method('get_comicpress_dbi')->will($this->returnValue($dbi));
$this->assertEquals(array( $this->assertTrue(wp_cache_get($expected_cache_key, 'comicpress') === false);
'0' => array(
'1' => array(
'2' => array(
'3' => true,
'4' => true
)
)
)
), $css->get_category_simple_structure(1));
$this->assertEquals(array( $this->assertEquals($expected_result, $css->get_category_simple_structure($parent));
'0' => array(
'1' => array( $this->assertEquals($expected_result, wp_cache_get($expected_cache_key, 'comicpress'));
'2' => array( }
'3' => true,
'4' => true function providerTestGenerateCacheKey() {
) return array(
), array(null, 'test-key'),
'5' => true array(1, 'test-key-1'),
) array('1', 'test-key-1')
), $css->get_category_simple_structure()); );
}
/**
* @dataProvider providerTestGenerateCacheKey
*/
function testGenerateCacheKey($param, $expected_key) {
$this->assertEquals($expected_key, $this->css->generate_cache_key('test-key', $param));
} }
function providerTestNormalizeFlattenedStoryline() { function providerTestNormalizeFlattenedStoryline() {