From 7ba5fc8ba9fd68f99b8b3eff73445477a3b0740e Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 14 Jan 2010 20:47:12 -0500 Subject: [PATCH] add caching to simple structure --- classes/ComicPressStoryline.inc | 15 ++++++ test/ComicPressStorylineTest.php | 80 +++++++++++++++++++++++--------- 2 files changed, 73 insertions(+), 22 deletions(-) diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index e39c0c4..072fd19 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -255,6 +255,12 @@ class ComicPressStoryline { * Get a simple structure. */ 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(); $dbi = $this->get_comicpress_dbi(); @@ -277,9 +283,18 @@ class ComicPressStoryline { } } + wp_cache_set($cache_key, $structure, 'comicpress'); + 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. */ diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php index 9449c0b..17ed851 100644 --- a/test/ComicPressStorylineTest.php +++ b/test/ComicPressStorylineTest.php @@ -7,6 +7,7 @@ require_once('ComicPressStoryline.inc'); class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { function setUp() { _reset_wp(); + wp_cache_flush(); $this->css = new ComicPressStoryline(); } @@ -282,7 +283,44 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $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')); $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)); - $this->assertEquals(array( - '0' => array( - '1' => array( - '2' => array( - '3' => true, - '4' => true - ) - ) - ) - ), $css->get_category_simple_structure(1)); + $this->assertTrue(wp_cache_get($expected_cache_key, 'comicpress') === false); - $this->assertEquals(array( - '0' => array( - '1' => array( - '2' => array( - '3' => true, - '4' => true - ) - ), - '5' => true - ) - ), $css->get_category_simple_structure()); + $this->assertEquals($expected_result, $css->get_category_simple_structure($parent)); + + $this->assertEquals($expected_result, wp_cache_get($expected_cache_key, 'comicpress')); + } + + function providerTestGenerateCacheKey() { + return array( + array(null, 'test-key'), + array(1, 'test-key-1'), + array('1', 'test-key-1') + ); + } + + /** + * @dataProvider providerTestGenerateCacheKey + */ + function testGenerateCacheKey($param, $expected_key) { + $this->assertEquals($expected_key, $this->css->generate_cache_key('test-key', $param)); } function providerTestNormalizeFlattenedStoryline() {