lscache_wp icon indicating copy to clipboard operation
lscache_wp copied to clipboard

Better control over the cache location

Open galbaras opened this issue 2 years ago • 6 comments

Re: https://wordpress.org/support/topic/change-the-location-where-files-are-cached/ https://wordpress.org/support/topic/move-litespeed-folder-location/

On all of my sites, I want the Litespeed cache directory to be wp-content/cache/litespeed, and it seems I'm not the only one. The advice to use wp-config.php constants based on WP_CONTENT_DIR doesn't work, because that constant is only defined later in the code.

Better option 1

Provide filters on all of the constants in litespeed-cache.php, but particularly on LITESPEED_STATIC_DIR and LITESPEED_STATIC_URL, because those will be processed later, when WP_CONTENT_DIR is already defined, and other site-specific definitions can be used.

Using filters will also allow overriding cache locations in a generic way via a plugin, which can then be installed on multiple sites with the same code. Being a developer feature prevents non-technical users from being scared and/or making mistakes.

Better option 2

Provide plugin settings for the cache location directory. One setting to override the use of WP_CONTENT_DIR should do the trick, because it can then be used for both constants.

Better option 3

Use public global variables, which can be changed with code, instead of constants.

galbaras avatar Feb 28 '23 23:02 galbaras

3 weeks and no response?

galbaras avatar Mar 22 '23 04:03 galbaras

Still nothing? 😞

galbaras avatar May 06 '23 06:05 galbaras

PLEASE, can I at least get a response?

galbaras avatar Aug 14 '23 23:08 galbaras

It's been a while, but no change, although this really seems like an easy thing to implement.

galbaras avatar Feb 19 '24 05:02 galbaras

This issue celebrated its first birthday last month...

galbaras avatar Apr 01 '24 04:04 galbaras

image

And still, nothing 😞

galbaras avatar May 05 '24 02:05 galbaras

Hi @galbaras Sorry for late response. Thanks for following up.

The advice to use wp-config.php constants based on WP_CONTENT_DIR doesn't work, because that constant is only defined later in the code. Can you tell more about why can't use WP_CONTENT_DIR? Its default value is defined in wp-settings.php which is loaded after wp-config.php definitions and before LSCWP init. So if you define it in wp-config.php, it will affect LSCWP.

The about question is just out of curiosity. I believe to move /litespeed to /cache/litespeed, changing WP's content constant isn't a good practice.

Another question is, is there any reason you can't define these consts to redirect the folder? image

hi-hai avatar May 06 '24 20:05 hi-hai

I agree. That's why I suggested a filter, which will be applied in litespeed-cache.php, when all the constants and all the site options can be used. It's both easy to implement and standard for WordPress plugins, so why not just do it?

Just change lines 60 & 61 to this:

!defined( 'LITESPEED_STATIC_URL' ) && define( 'LITESPEED_STATIC_URL', apply_filters( 'litespeed_static_url', $WP_CONTENT_URL . '/litespeed' ) ); // Full static cache folder URL '//example.com/wp-content/litespeed'
!defined('LITESPEED_STATIC_DIR') && define('LITESPEED_STATIC_DIR', apply_filters( 'litespeed_static_dir', LSCWP_CONTENT_DIR . '/litespeed' ) ); // Full static cache folder path '/var/www/html/***/wp-content/litespeed'

galbaras avatar May 07 '24 03:05 galbaras

It seems not the answer to my question. Why not using constants define but filters to make it? Filters have lower efficiency IMO?

hi-hai avatar May 07 '24 13:05 hi-hai

The problem with constants is that they are defined in wp-config.php, and things like home_url() aren't defined by then.

Defining constants in functions.php is too late, because the plugin has been loaded by then. Now that I think about it, it also means that filters defined in function.php won't work either to override the plugin's constants.

Why not add this as an "autoload" plugin setting? Autoloaded options may extend loading time by a small fraction of a second.

Anyway, my motivation for this was related to backups, and I've found a way around it, so I'm just going to close this issue, but I still think site owners should have control over this without programming.

galbaras avatar May 08 '24 00:05 galbaras

Adding an option to setting is an option, however its kind of advanced and less used for most users, IMO its a bit overkill.

I agree this requirement is a must in certain cases, how do you think we put the folder name to a const so you can define it in any early stage? image

hi-hai avatar May 08 '24 13:05 hi-hai

If LITESPEED_DATA_FOLDER idea can work, we can add it to next release.

hi-hai avatar May 08 '24 13:05 hi-hai

It's easier to define one constant than it is to define two, and using that in both of the other constants is a good idea for sure, especially since it's relative to the WP content folder.

That's a very clever way of doing it so that it doesn't depend on the root path/URL. Well done.

galbaras avatar May 09 '24 00:05 galbaras

It is added into https://github.com/litespeedtech/lscache_wp/commit/270381e5e102a16436e264be4904420c8b11dfff Please confirm if its as expected or not. TIA.

hi-hai avatar May 09 '24 17:05 hi-hai

I've copied only the constant definition changes to the plugin on one of my sites and added this to wp-config.php:

define( 'LITESPEED_DATA_FOLDER', 'cache/litespeed' );

After clearing all caches and reloading the home page, resources are being stored in the right place and retrieved from that place correctly. This works. Thank you!

galbaras avatar May 10 '24 00:05 galbaras

Great to hear that! Good day!

hi-hai avatar May 14 '24 21:05 hi-hai