collation: Refactor getFirstLetterData() cache handling

* Factor out fetchFirstLetterData() as a separate method.
* Move 'version' into the key instead of checking afterwards.
* Use getWithSetCallback() for the cache handling.
  (Depends on version being in the key).

Change-Id: I15bddf5d1dabcdcef47a938447ba59436bd8a294
This commit is contained in:
Timo Tijhof 2016-04-19 22:27:22 +01:00 committed by Krinkle
parent f4070a5526
commit 2a86b5a17a

View file

@ -234,32 +234,33 @@ class IcuCollation extends Collation {
/** /**
* @since 1.16.3 * @since 1.16.3
* @return array
*/ */
public function getFirstLetterData() { public function getFirstLetterData() {
if ( $this->firstLetterData !== null ) { if ( $this->firstLetterData === null ) {
return $this->firstLetterData;
}
$cache = ObjectCache::getLocalServerInstance( CACHE_ANYTHING ); $cache = ObjectCache::getLocalServerInstance( CACHE_ANYTHING );
$cacheKey = $cache->makeKey( $cacheKey = $cache->makeKey(
'first-letters', 'first-letters',
$this->locale, $this->locale,
$this->digitTransformLanguage->getCode(), $this->digitTransformLanguage->getCode(),
self::getICUVersion() self::getICUVersion(),
self::FIRST_LETTER_VERSION
); );
$cacheEntry = $cache->get( $cacheKey ); $this->firstLetterData = $cache->getWithSetCallback( $cacheKey, $cache::TTL_WEEK, function () {
return $this->fetchFirstLetterData();
if ( $cacheEntry && isset( $cacheEntry['version'] ) } );
&& $cacheEntry['version'] == self::FIRST_LETTER_VERSION }
) {
$this->firstLetterData = $cacheEntry;
return $this->firstLetterData; return $this->firstLetterData;
} }
/**
* @return array
* @throws MWException
*/
private function fetchFirstLetterData() {
// Generate data from serialized data file // Generate data from serialized data file
if ( isset( self::$tailoringFirstLetters[$this->locale] ) ) { if ( isset( self::$tailoringFirstLetters[$this->locale] ) ) {
$letters = wfGetPrecompiledData( "first-letters-root.ser" ); $letters = wfGetPrecompiledData( 'first-letters-root.ser' );
// Append additional characters // Append additional characters
$letters = array_merge( $letters, self::$tailoringFirstLetters[$this->locale] ); $letters = array_merge( $letters, self::$tailoringFirstLetters[$this->locale] );
// Remove unnecessary ones, if any // Remove unnecessary ones, if any
@ -374,15 +375,11 @@ class IcuCollation extends Collation {
$data = [ $data = [
'chars' => array_values( $letterMap ), 'chars' => array_values( $letterMap ),
'keys' => array_keys( $letterMap ), 'keys' => array_keys( $letterMap ),
'version' => self::FIRST_LETTER_VERSION,
]; ];
// Reduce memory usage before caching // Reduce memory usage before caching
unset( $letterMap ); unset( $letterMap );
// Save to cache
$this->firstLetterData = $data;
$cache->set( $cacheKey, $data, $cache::TTL_WEEK );
return $data; return $data;
} }
@ -390,30 +387,21 @@ class IcuCollation extends Collation {
* @since 1.16.3 * @since 1.16.3
*/ */
public function getLetterByIndex( $index ) { public function getLetterByIndex( $index ) {
if ( $this->firstLetterData === null ) { return $this->getFirstLetterData()['chars'][$index];
$this->getFirstLetterData();
}
return $this->firstLetterData['chars'][$index];
} }
/** /**
* @since 1.16.3 * @since 1.16.3
*/ */
public function getSortKeyByLetterIndex( $index ) { public function getSortKeyByLetterIndex( $index ) {
if ( $this->firstLetterData === null ) { return $this->getFirstLetterData()['keys'][$index];
$this->getFirstLetterData();
}
return $this->firstLetterData['keys'][$index];
} }
/** /**
* @since 1.16.3 * @since 1.16.3
*/ */
public function getFirstLetterCount() { public function getFirstLetterCount() {
if ( $this->firstLetterData === null ) { return count( $this->getFirstLetterData()['chars'] );
$this->getFirstLetterData();
}
return count( $this->firstLetterData['chars'] );
} }
/** /**