array_rand/shuffle result is being cached
-
Hello there,
I have the same exact issue reported in this other thread.
I confirm I have LITESPEED_SERVER_TYPE = LITESPEED_SERVER_ENT variable set, and that I need to return a value and not directly print via echo, as suggested in your ESI guide.
Since the other thread has been closed with no solution, I’m wondering how to solve my problem.
Basically I have an object with multiple array containing images and their caption, on each refresh the page should show a different cover splash image and sentence, unfortunately has been cached. On my local machine (where no Litespeed is installed) it works perfectly, while in the Production env the strong Litespeed cache prevent this logic to work.
Do you have any suggestion? or can you make public the resolution applied in the other thread?
Thanks in advance
The page I need help with: [log in to see the link]
-
please share what was the code you have implement via ESI ?
Hello, thanks for your reply.
Here is the code:
shuffle($covers);
$cover = reset($covers);So I have one variable as argument and one variable to return.
I tried with ESI, but it’s not returning the variable.
Here the code I tried instead:echo apply_filters('litespeed_esi_url', 'my_esi_block', 'Custom ESI block', $covers);
Together with what I put in my
functions.php
using a slightly different approach (array_rand insted of shuffle, still the same result expected):add_action('litespeed_esi_load-my_esi_block', 'my_esi_block_esi_load');
function my_esi_block_esi_load($params) {
do_action('litespeed_control_set_nocache');
$param_index = array_rand($params);
return $params[$param_index];
}- This reply was modified 3 months, 3 weeks ago by Pyetro.
em , have you tried the demo code with that rand() ? did that work ?
@subfighter3 Looking at the code I see a thing you can test.
Changeecho apply_filters('litespeed_esi_url', 'my_esi_block', 'Custom ESI block', $covers);
toecho apply_filters('litespeed_esi_url', 'my_esi_block', 'Custom ESI block',?array($covers));
Also add some var_dump() and see what parameters you have passed.
Also can look as example for: https://docs.litespeedtech.com/lscache/lscwp/api/#generate-esi-block-url -> example: Random Number Example Plugin
Your code seems almost good. If you want further help create a ticket by sending email to:support at litespeedtech.com
and ask for a developer to look over.@qtwrk I tried exactly what the first example the guide suggests, and what I have in result is:
<include src="/?lsesi=my_esi_block&_control=private%2Cno-vary&_hash=9a2f73c5b904f7d204deefa0ac1f0999" cache-control="private,no-vary"></include>
No way to output the rendered content.
@litetim I even tried the other example passing parameters, and a var_dump before gives me the correct data (of course), but a second var_dump after the
apply_filters
results in a similar code as the above: a string 2XXX characters long.string(2493) "<!-- lscwp Custom ESI block --><esi:include src='/?lsesi=my_esi_block&_control=private%2Cno-vary&esi=W1t7ImltYWdlIjoyMzYyLCJjb250ZW50IjoiQXJjaGl0ZWN0dXJlIGFuZCBpbnRlcmlvciBkZXNpZ24gcHJvamVjdHMgPGJyIFwvPlxyXG50YWtlbiBvbiB3aXRoIHRoZSBzYW1lIGF0dGVudGlvbiB0byBkZXRhaWwuICIsIm92ZXJsYXlfb3BhY2l0eSI6IjEiLCJsaW5rIjp7InRpdGxlIjoiUHJvZHVjdCIsInVybCI6Imh0dHBzOlwvXC9kZXYuc3BhZ251bG9hbmRwYXJ0bmVycy5pdFwvaW50ZXJpb3ItZGVzaWduLXByb2plY3RcL3ZpbGxhLWx1eHVyeS1yZXNpZGVuY2Utc3QtbW9yaXR6XC8iLCJ0YXJnZXQiOiIifX0seyJpbWFnZSI6MjM1OCwiY29udGVudCI6IlNwYWdudWxvICYgUGFydG5lcnMgcGFpZCBpbiB0aGUgbGFzdCB5ZWFycyBhIHNwZWNpYWwgYXR0ZW50aW9uIDxiciBcLz5cclxudG8gaW50ZXJpb3IgZGVzaWduIHByb2plY3RzLCBtb3N0bHkgZm9yIGx1eHVyeSByZXNpZGVudGlhbCBhbmQgaG90ZWxzLCA8YnIgXC8%2BXHJcbmJvdGggaW4gSXRhbHkgYW5kIGFicm9hZC4iLCJvdmVybGF5X29wYWNpdHkiOiIxIiwibGluayI6eyJ0aXRsZSI6IkJhZ2xpb25pIEhvdGVsIExvbmRvbiIsInVybCI6Imh0dHBzOlwvXC9kZXYuc3BhZ251bG9hbmRwYXJ0bmVycy5pdFwvaW50ZXJpb3ItZGVzaWduLXByb2plY3RcL2hvdGVsLWJhZ2xpb25pLWxvbmRvblwvIiwidGFyZ2V0IjoiIn19LHsiaW1hZ2UiOjIzNjYsImNvbnRlbnQiOiJFdmVyeSBwcm9qZWN0IGlzIGEgbmV3IGFkdmVudHVyZ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dGF1cmFudCBHaWFubmlubyBNaWxhbiIsInVybCI6Imh0dHBzOlwvXC9kZXYuc3BhZ251bG9hbmRwYXJ0bmVycy5pdFwvaW50ZXJpb3ItZGVzaWduLXByb2plY3RcL3Jlc3RhdXJhbnQtZ2lhbm5pbm8tbWlsYW5cLyIsInRhcmdldCI6IiJ9fV1d&_hash=b80bd7fa01d76834a49345ef7111276a' cache-control='private,no-vary' /><!-- lscwp Custom ESI block esi end -->"
I double checked on cPanel and the server is enabled, otherwise neither the <include> block wouldn’t be render like that.
Maybe some wrong ESI setting? “Enable ESI” in Cache > ESI was the only thing I did, other than making sure it’s a Litespeed Enterprise server, which I can manage via cPanel.
Any idea about why the content of the filter does not render properly?
if you see that
<esi include src="/?lsesi
, that means ESI is not supportedplease try this standalone PHP script for ESI example , see if it works https://docs.litespeedtech.com/lscache/devguide/advanced/#esi-example
@qtwrk thanks for the help!
Unluckily still does not work.
Accordingly to the linked guide, I tried both the method of setting the header (via htaccess and PHP), regardless of the header setting I also see
x-litespeed-cache: hit
in the page headers, so I guess Litespeed and ESI are properly set, still I see the following HTML piece of code:<include src="/?lsesi=my_esi_block&_control=private%2Cno-vary&_hash=9a2f73c5b904f7d204deefa0ac1f0999" cache-control="private,no-vary"></include>
I also remember that in one of my first attempts, the Random code worked, then suddenly printing the above piece of HTML.
I really can’t understand the pattern…
now it starts to make me think your server doesn’t support ESI , or ESI is not enabled
please try this
create a sub directory on your website document root, name it as
esi-test
create file
./esi-test/.htaccess
with content:<IfModule LiteSpeed>
RewriteEngine on
CacheLookup on
RewriteRule .* - [E=esi_on:1]
RewriteRule .* - [E=cache-control:max-age=120]
</IfModule>create file
./esi-test/index.php
with content:<?php
echo 'line 1 - cache friendly';
echo '<br>';
echo '<esi:include src="/esi-test/esi.php" cache-control="no-cache"/>';
echo '<br>';
echo 'line 3 - cache friendly';
?>create file
./esi-test/esi.php
with content:<?php
echo "line 2 - cache unfriendly, generate a random number: " . rand(1,999);
?>then open
https://domain.com/esi-test/index.php
, see what it shows.this code I tested on my cPanel and confirmed working
meanwhile, please provide the report number , you can get it in toolbox -> report -> click “send to LiteSpeed”
It works!
Looks like it’s not working in the WP installation (even if once worked)…I am trying to send the report, but as soon as I press “Send to Litespeed” I got the following error message on reload:
Failed to request via WordPress: No longer supported. please upgrade your plugin. [server] https://api.quic.cloud [service] wp/report
And I have the latest LS Cache version: 6.3.0.1
Edit: retrying sendind the report I also get this:
Cloud Error: Please try after 3m for service wp/report
- This reply was modified 3 months, 3 weeks ago by Pyetro.
Ok it worked now, here is the report number: ZSCHJOWN
Hello there, it’s getting more weird.
Yesterday night I tried again and the function perfectly worked, as you can see from the screenshot:
then after a while, even when I was still working on refining the code, suddenly stopped to work, again, giving me the same weird <include> tag. The weird part is that I didn’t modify any of the ESI settings, nor the code of the filter… Of course neither the basic example with rand() works anymore:
And above all, the most worrying thing is that having ESI enabled and loading any page with that apply_filters code, takes ages to load, like forever, any other page without it just work as expected, but ONLY if logged in! Logged out users can load the page in a decent time, still with the <include> tag instead of the rendered code…
In the meanwhile, the /esi-test/ out-of-wp perfectly works! so the server is properly accepting and making work ESI… the problem only happen within WP environment, so it’s probably a faulty Plugin feature.
Looks like the ESI feature is quite buggy and unresponsive, even plenty of other post I read here about, underline how broken is this thing.
How is supposed to work? Does exist any positive proof that this feature actually work?
@subfighter3 let’s look at the problem this way:
ESI are cached blocks that will load data(like nonce) on a page.
For each ESI request WP init is run => slows down the server
In your case, best results with ESI is to create the block and disable cache on the whole page ->but this is not a good solution(I believe is your homepage).
One ideea is to create the banner/slider html(leave html cached) and change it from JS: add the slides and texts as JS variable that will get a random slider and replace current- This reply was modified 3 months, 3 weeks ago by litetim.
Yes it’s the Homepage and it’s unlikely the optimal solution to disable the cache in favor of the slider.
I thought about replacing the PHP in JS but the problem is that the main array comes from a group of Custom Fields, where I only get the image ID, then the wp_get_attachment_image is crucial to render the proper <img> tag with all the proper srcset/sizes which are different from image to image.
PHP renders all these information way before the JS can do anything, that’s why it’s important to find a way to exclude Litespeed from caching that random value.
I want to avoid to load all the slides, then pick one with JS, it’s still a waste of resources, and in case the client adds too many slides, it’s going to affect loading time in any case…
Why ESI it’s simply not working as expected?
On top of all of it, why the heck sometimes works and other times is not, the lack of a clear pattern is driving me crazy!!function lscwp_log_out_content( $content ) {
error_log($content, 3, '/path/to/a/log');
return $content;
}
add_filter( 'litespeed_buffer_after', 'lscwp_log_out_content', 0);please try this , to log out the HTML source code
it should not see as
<include ...
, but only<esi:include
is correct format.Error log successfully created, but I still see only <include… in the HTML source code:
- You must be logged in to reply to this topic.