mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-09 01:27:20 +08:00
* Default cache ttl (duration) property (fixes #6809) * Updates after PR review (related #6809) * Updates after more PR reviews (related #6809)
This commit is contained in:
@ -204,6 +204,10 @@ if ($data === false) {
|
||||
}
|
||||
```
|
||||
|
||||
Since 2.0.11 you may set [[yii\caching\Cache::$ttl|ttl]] value in your cache component configuration if you prefer a custom cache duration
|
||||
over the default unlimited duration.
|
||||
This will allow you not to pass custom `duration` parameter to [[yii\caching\Cache::set()|set()]] each time.
|
||||
|
||||
|
||||
### Cache Dependencies <span id="cache-dependencies"></span>
|
||||
|
||||
|
||||
@ -37,6 +37,9 @@ Yii Framework 2 Change Log
|
||||
- Enh #12901: Added `getDefaultHelpHeader` method to the `yii\console\controllers\HelpController` class to be able to override default help header in a class heir (diezztsk)
|
||||
- Enh: Added constants for specifying `yii\validators\CompareValidator::$type` (cebe)
|
||||
- Bug #12974: Fixed incorrect order of migrations history in case `yii\console\controllers\MigrateController::$migrationNamespaces` is in use (evgen-d, klimov-paul)
|
||||
- Enh #6809: Added `\yii\caching\Cache::$ttl` property, allowing to set custom default cache duration (sdkiller)
|
||||
- Enh #6809: Added `\yii\caching\Cache::$ttl` property, allowing to set custom default cache duration (sdkiller, dynasource)
|
||||
- Bug #12974: Changed order of migrations history in `yii\console\controllers\MigrateController::getMigrationHistory()` (evgen-d)
|
||||
|
||||
|
||||
2.0.10 October 20, 2016
|
||||
|
||||
@ -71,6 +71,11 @@ abstract class Cache extends Component implements \ArrayAccess
|
||||
* implementations of the cache can not correctly save and retrieve data different from a string type.
|
||||
*/
|
||||
public $serializer;
|
||||
/**
|
||||
* @var integer default duration in seconds before the cache will expire. Default value is 0, meaning infinity.
|
||||
* @since 2.0.11
|
||||
*/
|
||||
public $ttl = 0;
|
||||
|
||||
|
||||
/**
|
||||
@ -202,14 +207,19 @@ abstract class Cache extends Component implements \ArrayAccess
|
||||
* @param mixed $key a key identifying the value to be cached. This can be a simple string or
|
||||
* a complex data structure consisting of factors representing the key.
|
||||
* @param mixed $value the value to be cached
|
||||
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
|
||||
* @param int $duration default duration in seconds before the cache will expire. If not set,
|
||||
* default [[ttl]] value is used.
|
||||
* @param Dependency $dependency dependency of the cached item. If the dependency changes,
|
||||
* the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
|
||||
* This parameter is ignored if [[serializer]] is false.
|
||||
* @return bool whether the value is successfully stored into cache
|
||||
*/
|
||||
public function set($key, $value, $duration = 0, $dependency = null)
|
||||
public function set($key, $value, $duration = null, $dependency = null)
|
||||
{
|
||||
if ($duration === null) {
|
||||
$duration = $this->ttl;
|
||||
}
|
||||
|
||||
if ($dependency !== null && $this->serializer !== false) {
|
||||
$dependency->evaluateDependency($this);
|
||||
}
|
||||
|
||||
@ -177,6 +177,13 @@ abstract class CacheTestCase extends TestCase
|
||||
$this->assertEquals(['number_test' => 42, 'non_existent_key' => null], $cache->mget(['number_test', 'non_existent_key']));
|
||||
}
|
||||
|
||||
public function testDefaultTtl()
|
||||
{
|
||||
$cache = $this->getCacheInstance();
|
||||
|
||||
$this->assertSame(0, $cache->ttl);
|
||||
}
|
||||
|
||||
public function testExpire()
|
||||
{
|
||||
$cache = $this->getCacheInstance();
|
||||
|
||||
Reference in New Issue
Block a user