WordPress の MySQL へのクエリ発行数を減らすために Redis Object Cache プラグインとか導入することがあるんですよ。
そんな時 update_option()
しても変更が反映されないとかってことありませんか?
これ実は、codex にも書いてあるんだけど、すでにキャッシュされているオプションを更新する場合は wp_cache_delete ( 'alloptions', 'options' );
しろよってことらしいです。
Notes
Upon getting an option in code, this option value will be cached. If you modify any options outside of the Options API, then try to update an option that's already cached, this will fail updating and return false. You can use the following method to clear the options cache before trying to get or update the options on the same request:
<?php
wp_cache_delete ( 'alloptions', 'options' );
?>
Function Reference/update option
でも、使用している全てのプラグインとか、テーマとかがそのお作法に則ってキャッシュを消してくれてるわけでもないし、だいたいコアのソースで制御してるはずのプラグインの有効化/無効化ですら Redis Object Cache を導入すると反映されなくなっちゃう...
そんなわけで、Redis Object Cache 入れた時は以下のようなプラグインを wp-content/mu-plugins/ に入れて update_option()
とかした時にキャッシュをいい感じで消してあげてます。
<?php
/*
Plugin Name: Redis Object Cache fix
Plugin URI:
Description:
Version: 0.1
Author: DigitalCube
Author URI: https://www.digitalcube.jp/
License: GPLv2 or later
*/
class redis_cache_fix {
public function __construct() {
add_action( 'add_option', array( $this, 'option_cache_flush' ) );
add_action( 'update_option', array( $this, 'option_cache_flush' ) );
add_action( 'delete_option', array( $this, 'option_cache_flush' ) );
}
// update_option, delete_option 時に cache をフラッシュ
public function option_cache_flush($option, $old_value = '', $value = ''){
if ( !empty( $option ) ) {
wp_cache_delete( $option, 'options' );
foreach (array('alloptions','notoptions') as $options_name) {
$options = wp_cache_get( $options_name, 'options' );
if ( ! is_array($options) ) {
$options = array();
}
if ( isset($options[$option]) ) {
unset($options[$option]);
wp_cache_set( $options_name, $options, 'options' );
}
unset($options);
}
}
return;
}
};
new redis_cache_fix();