# How to use caching with HBook?

Using a WordPress caching plugin or the built-in cache plugins from your hosting requires adjustment to work nicely with HBook. There are two aspects concerned : 

- the caching of HBook iCal calendar URLs : it is important that these URLs are not cached so it is always an up-to-date calendar that is returned when external platforms(AirBnb, Booking.com, etc.) call your HBook iCal calendar URLs.
- the calendars displayed on your website, be it the "Availability calendar" (\[hb\_availability\] shortcode or block) or the booking form date picker (\[hb\_booking\_form\] shortcode or block). You wish to always show accurate availability in your front-end HBook calendars.

Depending on your website structure, and on the caching tool that is in place on your website, we recommend implementing the following : 

- Exclude from the cache any URLs that should not be cached. You should exclude URLs of pages where you have a HBook availability calendar. This is the easiest option and is suitable if you do not have HBook calendars on many pages of your website
- use a function from your caching tool to flush the cache whenever a reservation has been received or modified and whenever a blocked date has been added. This option is more appropriate if you have many pages on your website that display HBook calendars. And it is as well the best way to proceed for the HBook iCal calendar URLs, since not all caching tools allow having query parameter in their settings to Exclude URLs from the cache.

You can use our filters to execute the function that will clear/flush cache, each time a reservation is received or updated. The filters that should be used are "hb\_reservations\_updated" and "hb\_blocked\_accom\_updated".   

> [!INFO]
> For a list of all HBook filters, please check [this page](https://maestrel.atlassian.net/wiki/spaces/HD/pages/34734482/Plugin+customization).

Please find below a couple of examples for popular caching tools :

<a id="litespeed-documentation-referencehttps-docslitespeedtechcom"></a>

### LiteSpeed [(documentation reference)](https://docs.litespeedtech.com)

- Exclude "feed" from the cache in **"Do not cache query strings"** in "Exclude" settings for the Cache. This is to avoid caching of your HBook iCal calendars.  
![litespeed-exclude-query-strings.png](./attachments/litespeed-exclude-query-strings.png)
- Use a function - Add this code  in the file functions.php of your child theme :

```
function my_custom_flush_cache() {
  do_action( 'litespeed_purge_all' );
}

add_filter( 'hb_reservations_updated', 'my_custom_flush_cache' );
add_filter( 'hb_blocked_accom_updated', 'my_custom_flush_cache' );
```

<a id="sg-optimizer-supercache"></a>

### SG Optimizer SuperCache

- You can check the section "Exclude URLs from Caching" of [this article](https://www.siteground.com/tutorials/wordpress/sg-optimizer/supercacher/) from SG Optimizer cache to exclude pages. This is to avoid caching of your HBook iCal calendars. Here is how it should be set:

![caching_speed_optimizer.png](./attachments/caching_speed_optimizer.png)

- Use a function - Add this code in the file functions.php of your child theme:

```
function my_custom_flush_cache() {
    if (function_exists('sg_cachepress_purge_cache')) {
      sg_cachepress_purge_cache();
    }
}

add_filter( 'hb_reservations_updated', 'my_custom_flush_cache' );
add_filter( 'hb_blocked_accom_updated', 'my_custom_flush_cache' );
```

<a id="wpfastest-cache-documentation-referencehttps-wwwwpfastestcachecom-tutorial-delete-the-cache-by-calling-the-function"></a>

### WPFastest cache ([documentation reference)](https://www.wpfastestcache.com/tutorial/delete-the-cache-by-calling-the-function))

- You can check [this article](https://www.wpfastestcache.com/features/exclude-page) from WPFastest cache to exclude pages.
- Use a function - Add this code in the file functions.php of your child theme :

```
function my_custom_flush_cache() {
    if ( function_exists( 'wpfc_clear_all_cache' ) ) {
        wpfc_clear_all_cache();
    }
}

add_filter( 'hb_reservations_updated', 'my_custom_flush_cache' );
add_filter( 'hb_blocked_accom_updated', 'my_custom_flush_cache' );
```

<a id="wprocket"></a>

### WPRocket

- You can check [this article](https://docs.wp-rocket.me/article/54-exclude-pages-from-the-cache) from WPRocket to exclude pages.
- Use a function - Add this code in the file functions.php of your child theme:

```
function my_custom_flush_cache() {
    if ( function_exists( 'rocket_clean_domain' ) ) {
        rocket_clean_domain();
    }
}

add_filter( 'hb_reservations_updated', 'my_custom_flush_cache' );
add_filter( 'hb_blocked_accom_updated', 'my_custom_flush_cache' );
```

<a id="wp-super-cache"></a>

### **WP Super Cache**

- You can check [this article](https://odd.blog/wp-super-cache-developers/) from WP Super Cache to exclude pages.
- Use a function - Add this code in the file functions.php of your child theme:

```
function my_custom_flush_cache() {
    if ( function_exists( 'wp_cache_clear_cache' ) ) {
        wp_cache_clear_cache();
     }
}

add_filter( 'hb_reservations_updated', 'my_custom_flush_cache' );
add_filter( 'hb_blocked_accom_updated', 'my_custom_flush_cache' );
```

<a id="wp-optimize"></a>

### **WP Optimize**

- You can check [this article](https://getwpo.com/documentation/#advanced-settings-exclude-urls) from WP Optimize to exclude pages.
- Use a function - Add this code in the file functions.php of your child theme:

```
function my_custom_flush_cache() {
    if ( function_exists( 'get_page_cache' ) ) {
        WP_Optimize()->get_page_cache()->purge();
    }
}

add_filter( 'hb_reservations_updated', 'my_custom_flush_cache' );
add_filter( 'hb_blocked_accom_updated', 'my_custom_flush_cache' );
```

<a id="w3-total-cache"></a>

### W3 Total Cache

- You can check [this article](https://www.redbridgenet.com/how-to-exclude-specific-pages-from-w3-total-cache/) from W3 Total Cache to exclude pages. Here is where you will add any URL you wish to exclude.

![w3-cache-exclude-url.png](./attachments/w3-cache-exclude-url.png)

- Use a function - Add this code in the file functions.php of your child theme:

```
function my_custom_flush_cache() {
    if ( defined( 'W3TC' ) ) {
        $w3_plugin_totalcache->flush_all();
    }
}

add_filter( 'hb_reservations_updated', 'my_custom_flush_cache' ); 
add_filter( 'hb_blocked_accom_updated', 'my_custom_flush_cache' );
```

If you don't know whether the cache plugin you wish to use have this flush function that can be called, you can browse their documentation or get in touch with their support team. Feel free to contact us if needed.