2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[WordPress] Redis Object Cache プラグインとか導入するとキャッシュがきつすぎてオプションが保存されない時あるよね

Posted at

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() とかした時にキャッシュをいい感じで消してあげてます。

redis-cache-fix.php
<?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();
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?