问题
I recently deployed my Magento site and started using Varnish on it.
However, we do have some PHP logic that place an open and close button on each of the stores (they are open on working times), and after it`s closed it should received any orders. Also, we have logics on the buttons 'Add to Cart', to inform the customer when the store is opened.
The issue here is that if I serve the pages with VArnish, the loading time indeed is marvelous, however when the store gets opened (or closed), the page is not refreshed (of course).
Which would be a best practice in this case?
- Write an AJAX logic to obtain these dynamic information from the backend
- Run Varnish Purge for all pages that have this logic, by the time the store is opened
- Have a low TTL set (this will reduce Cache Hit).
Thank you very much for the help.
Best Regards, Mauricio.
回答1:
What about storing the open and close times of each store in the html and use javascript to show the current status to the user? That way you can use the cache and still have the dynamic parts that you mentioned.
Example (will not work, mostly pseudo code):
<p id="open" data-open-start="8:00 UTC" data-open-end="16:00 UTC">We are open between 8:00 UTC and 16:00 UTC</p>
<script>
// assuming JQuery
var open_start_time = [get data-open-start-time and parse it]
var open_end_time = [get data-open-end-time and parse it]
// some time zone conversions...
if (currentTime >= open_start_time && currentTime <= open_end_time) {
$('p#open').text('we are open!');
} else {
$('p#open').text('sorry, we are closed!');
}
</script>
回答2:
If you want to still cache the full page, the only solution is to update your Magento to set a special HTTP header "Expires" for each page served. This header allow Varnish to expire cache when the date is over.
So you will need to set the value of the next opening if shop is currently closed and the other way around.
来源:https://stackoverflow.com/questions/13308080/magento-varnish-logic-for-stores-that-open-and-close-at-specific-times