From c23d6287a376c12744bd8a433c3c1d556d5c4800 Mon Sep 17 00:00:00 2001 From: felixmaier1989 Date: Wed, 4 Oct 2017 12:52:21 +0700 Subject: [PATCH] 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]);