Skip to content

Commit

Permalink
Merge pull request #2 from felixmaier1989/shorter-keys
Browse files Browse the repository at this point in the history
Shorter keys
  • Loading branch information
felixmaier1989 authored Oct 4, 2017
2 parents 8234b1e + c23d628 commit bf1b548
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/SoftCacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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];
}
Expand All @@ -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;
}
Expand All @@ -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));
}
}
76 changes: 76 additions & 0 deletions tests/SoftCacheTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,82 @@ 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]);
$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);
Expand Down

0 comments on commit bf1b548

Please sign in to comment.