Skip to content

Commit

Permalink
#no-ticket
Browse files Browse the repository at this point in the history
* bugfix remember method
* update tests
* bugfix CacheElement update method
  • Loading branch information
dantodev committed Sep 17, 2016
1 parent b8eb77f commit f26f87d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
12 changes: 4 additions & 8 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ public function set($key, $value, $lifetime = null, $refresh = null)
if (is_null($element = $this->getOrFetchCacheElement($key))) {
$this->_cache[$key] = $this->newCacheElement($key, $value, $lifetime, $refresh);
} else {
$element->update(
$value,
$lifetime ?: $element->getLifetime(),
$refresh ?: $this->_default_refresh
);
$element->update($value, $lifetime, $refresh);
}
return $this;
}
Expand All @@ -92,17 +88,17 @@ public function forget($key)

/**
* @param $key
* @param \Closure $call
* @param callable $call
* @param int|null $lifetime
* @param bool|null $refresh
* @return $this
*/
public function remember($key, \Closure $call, $lifetime = null, $refresh = null)
public function remember($key, callable $call, $lifetime = null, $refresh = null)
{
if (!$this->has($key)) {
$this->_cache[$key] = $this->newCacheElement($key, $call(), $lifetime, $refresh);
}
return $this;
return $this->get($key);
}

/**
Expand Down
18 changes: 15 additions & 3 deletions src/CacheElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ public function __construct(Cache $cache_instance, $key, $value, $lifetime = 60,
* @param int $lifetime
* @param bool $refresh
*/
public function update($value, $lifetime = 60, $refresh = false)
public function update($value, $lifetime = null, $refresh = null)
{
$this->_value = (string)$value;
$this->_lifetime = $lifetime;
if (!is_null($lifetime)) {
$this->_lifetime = $lifetime;
}
if (!is_null($refresh)) {
$this->_refresh = $refresh;
}
$this->_start = time();
$this->_refresh = $refresh;
$this->_modified = true;
}

Expand All @@ -58,11 +62,19 @@ public function getValue()
return $this->_value;
}

/**
* @return int
*/
public function getLifeTime()
{
return $this->_lifetime;
}

public function getRefresh()
{
return $this->_refresh;
}

/**
*
*/
Expand Down
9 changes: 4 additions & 5 deletions test/FileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ public function test()
$this->assertNull($cache->get("foo"));

// test remember
$cache->remember("foo2", function () {
$this->assertEquals("bar2", $cache->remember("foo2", function () {
return "bar2";
});
$cache->remember("foo2", function () {
}));
$this->assertEquals("bar2", $cache->remember("foo2", function () {
return "this will never be set";
});
$this->assertEquals("bar2", $cache->get("foo2"));
}));

// test timeout
$cache->set("foo3", "bar3", 1);
Expand Down

0 comments on commit f26f87d

Please sign in to comment.