diff --git a/src/SoftCacheTrait.php b/src/SoftCacheTrait.php index 22108ab..d6170f6 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 md5(serialize($args)); + } } diff --git a/tests/SoftCacheTraitTest.php b/tests/SoftCacheTraitTest.php index 0cf6474..5618d57 100644 --- a/tests/SoftCacheTraitTest.php +++ b/tests/SoftCacheTraitTest.php @@ -11,6 +11,82 @@ 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'] .= '