• Resolved guzart

    (@guzart)


    Hello!

    Is there a way to configure Page Cache and Object Cache to use Redis depending on an ENV configuration?

    The problem is that I use Heroku Redis service and they change the Server Address and Password from time to time, so I need to define those settings during initialization.

    Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @guzart

    Thank you for your inquiry and I am happy to assist you with this.
    Just to confirm, you want as the server address and password change to automatically update in W3 Total Cache?
    Thanks!

    Thread Starter guzart

    (@guzart)

    Yes. Heroku gives me the Redis server connection URL in the form of a getenv('REDIS_URL'). So I don’t have a permanent Redis Server Address and Password. They expect my application to adjust the Redis configuration at Runtime.

    This was super easy to do for the configuration database because it’s possible to configure the Database connection at runtime using the W3TC_CONFIG_CACHE_* constants.

    
    $redisURL = parse_url(getenv('REDIS_URL'));
    define('W3TC_CONFIG_CACHE_ENGINE', 'redis');
    define('W3TC_CONFIG_CACHE_REDIS_SERVERS', $redisURL['host'] . ':' . $redisURL['port']);
    define('W3TC_CONFIG_CACHE_REDIS_PASSWORD', $redisURL['pass']);
    define('W3TC_CONFIG_CACHE_REDIS_PERSISTENT', true);
    define('W3TC_CONFIG_CACHE_REDIS_DBID', 0);
    

    Any suggestions on how to configure database connection for the Page Cache, Object Cache, and Database Cache either at runtime or dynamically?

    • This reply was modified 4 years, 7 months ago by guzart.
    • This reply was modified 4 years, 7 months ago by guzart.
    Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @guzart

    Thank you for the information.
    You may use the wp-cli and set config values using it (check wp-content/plugins/w3-total-cache/Cli.php).
    Or you can also modify the w3tc-config/master.php file directly.
    Thanks!

    Thread Starter guzart

    (@guzart)

    Unfortunately I don’t think modifying w3tc-config/master.php directly will work for this scenario, because AFAIU the plugin automatically prefixes the file with <?php exit; ?> so that it’s not executable by PHP. This behavior prevents me from setting runtime configuration inside that file. Any configuration in the file needs to be static.

    I think using the CLI is my best path moving forward. I should be able to run a script when the app initializes. And have the script use WP CLI to configure W3TC based on environment variables.

    Thank you for the suggestions, Marko.

    Thread Starter guzart

    (@guzart)

    Some feedback after implementing it.

    It does work but it’s slow because I have to run four WP CLI commands, and the WP CLI has to load all of WordPress each time a command is run, that is, WP is loaded and discarded four times.

    I think in the next iteration I’ll create my own WP CLI command that calls w3-total-cache CLI functions. So that all four actions can run in a single command, and this way I can also use PHP to parse out the environment variable, instead of having to parse the Redis URL using bash and pass them as WP CLI arguments.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Redis configuration from Environment’ is closed to new replies.