From dd8c9dd58eb62314e8f232cb2b7f1be1763834c0 Mon Sep 17 00:00:00 2001 From: felixmaier1989 Date: Wed, 4 Oct 2017 12:38:55 +0700 Subject: [PATCH 1/2] Abstract generation of caching key --- src/SoftCacheTrait.php | 15 ++++++++++++--- tests/SoftCacheTraitTest.php | 8 ++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/SoftCacheTrait.php b/src/SoftCacheTrait.php index 22108ab..d4737f6 100644 --- a/src/SoftCacheTrait.php +++ b/src/SoftCacheTrait.php @@ -17,7 +17,7 @@ trait SoftCacheTrait { * @param $output mixed */ public function writeMethodCache($method, array $args, $output) { - $cache_key = serialize($args); + $cache_key = $this->getCacheKey($args); $this->cache[$method][$cache_key] = $output; } @@ -27,7 +27,7 @@ public function writeMethodCache($method, array $args, $output) { * @return mixed */ public function readMethodCache($method, array $args) { - $cache_key = serialize($args); + $cache_key = $this->getCacheKey($args); if ($this->checkMethodCache($method, $args)) { return $this->cache[$method][$cache_key]; } @@ -40,7 +40,7 @@ public function readMethodCache($method, array $args) { * @return bool */ public function checkMethodCache($method, array $args) { - $cache_key = serialize($args); + $cache_key = $this->getCacheKey($args); if (!array_key_exists($method, $this->cache)) { return false; } @@ -49,4 +49,13 @@ public function checkMethodCache($method, array $args) { } return true; } + + /** + * Generate an unique key + * @param array $args + * @return string + */ + public function getCacheKey(array $args) { + return serialize($args); + } } diff --git a/tests/SoftCacheTraitTest.php b/tests/SoftCacheTraitTest.php index 0cf6474..4d1f72f 100644 --- a/tests/SoftCacheTraitTest.php +++ b/tests/SoftCacheTraitTest.php @@ -11,6 +11,14 @@ public function setUp() { $this->Class = new TestClass(); } + public function test_getCacheKey() { + $cache_key1 = $this->Class->getCacheKey([2015]); + $cache_key2 = $this->Class->getCacheKey([2016]); + $this->assertNotEmpty($cache_key1); + $this->assertNotEmpty($cache_key2); + $this->assertNotEquals($cache_key1, $cache_key2); + } + public function test_methodWithoutCache() { $actual = $this->Class->getNextYearsWithoutCache(2016, 5); $this->assertEquals([2017, 2018, 2019, 2020, 2021], $actual); From c23d6287a376c12744bd8a433c3c1d556d5c4800 Mon Sep 17 00:00:00 2001 From: felixmaier1989 Date: Wed, 4 Oct 2017 12:52:21 +0700 Subject: [PATCH 2/2] Have shorter cache keys --- src/SoftCacheTrait.php | 2 +- tests/SoftCacheTraitTest.php | 68 ++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/SoftCacheTrait.php b/src/SoftCacheTrait.php index d4737f6..d6170f6 100644 --- a/src/SoftCacheTrait.php +++ b/src/SoftCacheTrait.php @@ -56,6 +56,6 @@ public function checkMethodCache($method, array $args) { * @return string */ public function getCacheKey(array $args) { - return serialize($args); + return md5(serialize($args)); } } diff --git a/tests/SoftCacheTraitTest.php b/tests/SoftCacheTraitTest.php index 4d1f72f..5618d57 100644 --- a/tests/SoftCacheTraitTest.php +++ b/tests/SoftCacheTraitTest.php @@ -11,6 +11,74 @@ public function setUp() { $this->Class = new TestClass(); } + public function test_getCacheKey_size() { + $array1 = [ + 'name' => 'Alpha', + 'description' => '

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

', + 'author_id' => 4, + 'Author' => [ + 'name' => 'Beta', + 'description' => '

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

', + ], + 'Retailer' =>[ + [ + 'name' => 'Charlie', + 'description' => '

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

', + 'Address' => [ + 'firstname' => 'Delta', + 'lastname' => 'Echo', + 'street1' => 'Foxtrot', + 'street2' => '', + 'zip' => '12345', + 'city' => 'Golf', + 'country' => 'VN', + ], + + ], + [ + 'name' => 'Hotel', + 'description' => '

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

', + 'Address' => [ + 'firstname' => 'Delta', + 'lastname' => 'Echo', + 'street1' => 'Foxtrot', + 'street2' => '', + 'zip' => '12345', + 'city' => 'Golf', + 'country' => 'VN', + ], + + ], + [ + 'name' => 'India', + 'description' => '

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

', + 'Address' => [ + 'firstname' => 'Delta', + 'lastname' => 'Echo', + 'street1' => 'Foxtrot', + 'street2' => '', + 'zip' => '12345', + 'city' => 'Golf', + 'country' => 'VN', + ], + ], + ], + ]; + $array3 = $array2 = $array1; + $array2['Retailer'][2]['Address']['zip'] .= '6'; + $array3['Retailer'][2]['description'] .= '
'; + $cache_key1 = $this->Class->getCacheKey([$array1]); + $cache_key2 = $this->Class->getCacheKey([$array2]); + $cache_key3 = $this->Class->getCacheKey([$array3]); + $this->assertNotEmpty($cache_key1); + $this->assertNotEmpty($cache_key2); + $this->assertNotEquals($cache_key1, $cache_key2); + $this->assertNotEquals($cache_key1, $cache_key3); + $this->assertLessThan(100, strlen($cache_key1)); + $this->assertLessThan(100, strlen($cache_key2)); + $this->assertLessThan(100, strlen($cache_key3)); + } + public function test_getCacheKey() { $cache_key1 = $this->Class->getCacheKey([2015]); $cache_key2 = $this->Class->getCacheKey([2016]);