See Release Notes
Long Term Support Release
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace cachestore_redis; 18 19 /** 20 * TTL support test for Redis cache. 21 * 22 * If you wish to use these unit tests all you need to do is add the following definition to 23 * your config.php file. 24 * 25 * define('TEST_CACHESTORE_REDIS_TESTSERVERS', '127.0.0.1'); 26 * 27 * @package cachestore_redis 28 * @copyright 2021 The Open University 29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 30 * @covers \cachestore_redis 31 */ 32 class ttl_test extends \advanced_testcase { 33 /** @var \cachestore_redis|null Cache store */ 34 protected $store = null; 35 36 public function setUp(): void { 37 // Make sure cachestore_redis is available. 38 require_once (__DIR__ . '/../lib.php'); 39 if (!\cachestore_redis::are_requirements_met() || !defined('TEST_CACHESTORE_REDIS_TESTSERVERS')) { 40 $this->markTestSkipped('Could not test cachestore_redis. Requirements are not met.'); 41 } 42 43 // Set up a Redis store with a fake definition that has TTL set to 10 seconds. 44 $definition = \cache_definition::load('core/wibble', [ 45 'mode' => 1, 46 'simplekeys' => true, 47 'simpledata' => true, 48 'ttl' => 10, 49 'component' => 'core', 50 'area' => 'wibble', 51 'selectedsharingoption' => 2, 52 'userinputsharingkey' => '', 53 'sharingoptions' => 15, 54 ]); 55 $this->store = new \cachestore_redis('Test', \cachestore_redis::unit_test_configuration()); 56 $this->store->initialise($definition); 57 58 parent::setUp(); 59 } 60 61 protected function tearDown(): void { 62 parent::tearDown(); 63 64 if ($this->store instanceof \cachestore_redis) { 65 $this->store->purge(); 66 } 67 } 68 69 /** 70 * Tests expiring data. 71 */ 72 public function test_expire_ttl(): void { 73 $this->resetAfterTest(); 74 75 // Set some data at time 100. 76 \cachestore_redis::set_phpunit_time(100); 77 $this->store->set('a', 1); 78 $this->store->set('b', 2); 79 $this->store->set_many([['key' => 'c', 'value' => 3], ['key' => 'd', 'value' => 4], 80 ['key' => 'e', 'value' => 5], ['key' => 'f', 'value' => 6], 81 ['key' => 'g', 'value' => 7], ['key' => 'h', 'value' => 8]]); 82 83 // Set some other data at time 110, including some of the existing values. Whether the 84 // value changes or not, its TTL should update. 85 \cachestore_redis::set_phpunit_time(110); 86 $this->store->set('b', 2); 87 $this->store->set_many([['key' => 'c', 'value' => 99], ['key' => 'd', 'value' => 4]]); 88 89 // Check all the data is still set. 90 $this->assertEqualsCanonicalizing(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], 91 $this->store->find_all()); 92 93 // Delete some data (to check deletion doesn't confuse expiry). 94 $this->store->delete('f'); 95 $this->store->delete_many(['g', 'h']); 96 97 // Set time to 115 and expire data. 98 \cachestore_redis::set_phpunit_time(115); 99 $info = $this->store->expire_ttl(); 100 101 // We are expecting keys a and e to be deleted. 102 $this->assertEquals(2, $info['keys']); 103 $this->assertEquals(1, $info['batches']); 104 105 // Check the keys are as expected. 106 $this->assertEqualsCanonicalizing(['b', 'c', 'd'], $this->store->find_all()); 107 108 // Might as well check the values of the surviving keys. 109 $this->assertEquals(2, $this->store->get('b')); 110 $this->assertEquals(99, $this->store->get('c')); 111 $this->assertEquals(4, $this->store->get('d')); 112 } 113 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body