Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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();
digitalcubeinc
デジタルキューブは AWS Partner Network (APN) アドバンスドテクノロジーパートナーです。
https://ja.amimoto-ami.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした