<?php
/**
- * Object caching using PHP arrays.
+ * Per-process memory cache for storing items.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
use Wikimedia\Assert\Assert;
/**
- * This is a test of the interface, mainly. It stores things in an associative
- * array, which is not going to persist between program runs.
+ * Simple store for keeping values in an associative array for the current process.
+ *
+ * Data will not persist and is not shared with other processes.
*
* @ingroup Cache
*/
}
public function set( $key, $value, $exptime = 0, $flags = 0 ) {
+ // Refresh key position for maxCacheKeys eviction
+ unset( $this->bag[$key] );
$this->bag[$key] = array(
self::KEY_VAL => $value,
self::KEY_EXP => $this->convertExpiry( $exptime )
$this->assertEquals( false, $cache->get( "key" . $i - 10 ) );
}
}
+
+ public function testExpire() {
+ $cache = new HashBagOStuff();
+ $cacheInternal = TestingAccessWrapper::newFromObject( $cache );
+ $cache->set( 'foo', 1 );
+ $cache->set( 'bar', 1, 10 );
+ $cache->set( 'baz', 1, -10 );
+
+ $this->assertEquals( 0, $cacheInternal->bag['foo'][$cache::KEY_EXP], 'Indefinite' );
+ // 2 seconds tolerance
+ $this->assertEquals( time() + 10, $cacheInternal->bag['bar'][$cache::KEY_EXP], 'Future', 2 );
+ $this->assertEquals( time() - 10, $cacheInternal->bag['baz'][$cache::KEY_EXP], 'Past', 2 );
+
+ $this->assertEquals( 1, $cache->get( 'bar' ), 'Key not expired' );
+ $this->assertEquals( false, $cache->get( 'baz' ), 'Key expired' );
+ }
+
+ public function testKeyOrder() {
+ $cache = new HashBagOStuff( array( 'maxKeys' => 3 ) );
+
+ foreach ( array( 'foo', 'bar', 'baz' ) as $key ) {
+ $cache->set( $key, 1 );
+ }
+
+ // Set existing key
+ $cache->set( 'foo', 1 );
+
+ // Add a 4th key (beyond the allowed maximum)
+ $cache->set( 'quux', 1 );
+
+ // Foo's life should have been extended over Bar
+ foreach ( array( 'foo', 'baz', 'quux' ) as $key ) {
+ $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
+ }
+ $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
+ }
}