Last active
January 11, 2020 16:50
-
-
Save kagg-design/ad0fbf4e9708998a0b633ea9b2053e6c to your computer and use it in GitHub Desktop.
Preemptive cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = [ | |
'element_data' => | |
[ | |
7 => | |
[ | |
'translation_id' => '18', | |
'trid' => '12', | |
'lang' => 'en', | |
'source_lang' => null, | |
'type' => 'page', | |
], | |
11 => | |
[ | |
'translation_id' => '23', | |
'trid' => '12', | |
'lang' => 'ru', | |
'source_lang' => 'en', | |
'type' => 'page', | |
], | |
391 => | |
[ | |
'translation_id' => '165', | |
'trid' => '12', | |
'lang' => 'uk', | |
'source_lang' => 'en', | |
'type' => 'page', | |
], | |
], | |
'translations' => | |
[ | |
7 => | |
[ | |
'en' => '7', | |
'ru' => '11', | |
'uk' => '391', | |
], | |
11 => | |
[ | |
'en' => '7', | |
'ru' => '11', | |
'uk' => '391', | |
], | |
391 => | |
[ | |
'en' => '7', | |
'ru' => '11', | |
'uk' => '391', | |
], | |
], | |
'trid_groups' => | |
[ | |
12 => | |
[ | |
'en' => '7', | |
'ru' => '11', | |
'uk' => '391', | |
], | |
], | |
'translation_ids_element' => | |
[ | |
18 => '7', | |
23 => '11', | |
165 => '391', | |
], | |
]; | |
/** | |
* @return array | |
*/ | |
protected function get_data_keys() { | |
return [ | |
'element_data', | |
'translations', | |
'trid_groups', | |
'translation_ids_element', | |
]; | |
} | |
public function init() { | |
$this->data = get_transient( $this->get_transient_name() ); | |
delete_transient( $this->get_transient_name() ); | |
if ( $this->data ) { | |
$this->data = json_decode( $this->data, true ); | |
$keys = $this->get_data_keys(); | |
foreach ( $keys as $key ) { | |
$this->{$key} = $this->data[ $key ]; | |
} | |
} | |
} | |
public function shutdown_action() { | |
$keys = $this->get_data_keys(); | |
$data = []; | |
foreach ( $keys as $key ) { | |
$this->data[ $key ] = isset( $this->data[ $key ] ) ? $this->data[ $key ] : []; | |
$data[ $key ] = array_replace( $this->{$key}, $this->data[ $key ] ); | |
} | |
set_transient( $this->get_transient_name(), $this->maybe_reduce_transient_size( $data ) ); | |
} | |
/** | |
* @param array $data | |
* | |
* @return string | |
*/ | |
private function maybe_reduce_transient_size( $data ) { | |
$json = wp_json_encode( $data ); | |
$size = strlen( $json ); | |
while ( $size > self::MAX_TRANSIENT_SIZE ) { | |
$count = count( $data['element_data'] ); | |
$keep_count = intval( $count / ( $size / self::MAX_TRANSIENT_SIZE ) ); | |
$keep_count = $keep_count * 2; // 'element_data' takes approximately one half of the total $data size. | |
$remove_count = $count - $keep_count; | |
$i = 0; | |
foreach ( $data['element_data'] as $key => $element_datum ) { | |
if ( $i > $remove_count ) { | |
break; | |
} | |
// Remove 'element_data' and relevant items on FIFO principle. | |
$translation_id = $data['element_data'][ $key ]['translation_id']; | |
$trid = $data['element_data'][ $key ]['trid']; | |
unset( $data['element_data'][ $key ] ); | |
unset( $data['translations'][ $key ] ); | |
unset( $data['trid_groups'][ $trid ] ); | |
unset( $data['translation_ids_element'][ $translation_id ] ); | |
$i ++; | |
} | |
$json = wp_json_encode( $data ); | |
$new_size = strlen( $json ); | |
if ( $new_size === $size ) { | |
break; // Prevent infinite loop if remove_count was estimated improperly. | |
} | |
$size = $new_size; | |
} | |
return $json; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment