diff --git a/docs/guide/caching-data.md b/docs/guide/caching-data.md index e24c006024..a6815196f9 100644 --- a/docs/guide/caching-data.md +++ b/docs/guide/caching-data.md @@ -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 diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index b37d039ee8..514c07037c 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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 diff --git a/framework/caching/Cache.php b/framework/caching/Cache.php index 72ed6b808a..46768c5da0 100644 --- a/framework/caching/Cache.php +++ b/framework/caching/Cache.php @@ -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); } diff --git a/tests/framework/caching/CacheTestCase.php b/tests/framework/caching/CacheTestCase.php index 475244a714..9bd63dc691 100644 --- a/tests/framework/caching/CacheTestCase.php +++ b/tests/framework/caching/CacheTestCase.php @@ -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();