Skip to content

Commit

Permalink
Have shorter cache keys
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmaier1989 committed Oct 4, 2017
1 parent dd8c9dd commit c23d628
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/SoftCacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public function checkMethodCache($method, array $args) {
* @return string
*/
public function getCacheKey(array $args) {
return serialize($args);
return md5(serialize($args));
}
}
68 changes: 68 additions & 0 deletions tests/SoftCacheTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,74 @@ public function setUp() {
$this->Class = new TestClass();
}

public function test_getCacheKey_size() {
$array1 = [
'name' => 'Alpha',
'description' => '<p>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.</p>',
'author_id' => 4,
'Author' => [
'name' => 'Beta',
'description' => '<p>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.</p>',
],
'Retailer' =>[
[
'name' => 'Charlie',
'description' => '<p>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.</p>',
'Address' => [
'firstname' => 'Delta',
'lastname' => 'Echo',
'street1' => 'Foxtrot',
'street2' => '',
'zip' => '12345',
'city' => 'Golf',
'country' => 'VN',
],

],
[
'name' => 'Hotel',
'description' => '<p>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.</p>',
'Address' => [
'firstname' => 'Delta',
'lastname' => 'Echo',
'street1' => 'Foxtrot',
'street2' => '',
'zip' => '12345',
'city' => 'Golf',
'country' => 'VN',
],

],
[
'name' => 'India',
'description' => '<p>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.</p>',
'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'] .= '<br/>';
$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]);
Expand Down

0 comments on commit c23d628

Please sign in to comment.