Or
$this->cache_key = apply_filters('wp_rest_cache/cache_key', md5($this->request_uri), $this->request_uri);
example result after apply_filters: 0fc1532dc07ad188604b9c332c9b820a-fr
Plugin Author
Acato
(@acato)
Hi @redcastor
Thank you for using our plugin!
Unfortunately the suggested filter will not work. The function build_request_uri()
is called from within a must use plugin, so before any plugin or theme is loaded and therefore before any add_filter(..)
is run.
We do however want to implement a solution for this problem. Can you tell me what exactly the language headers are that wpml adds?
As a quick-fix (until we have a better solution) you could maybe add the language as a parameter to the REST call. Each parameter will cause a new cache record to be created, so this would solve it for now.
Hello,
WPML is not compatible with stateless url but i have create in my angularjs plugin the language switcher based on custom url header.
Use parameter in url is not a solution for me .
Suggestion cache-enabler use a json file contains the config.
And the file is updated on save options in admin.
https://github.com/keycdn/cache-enabler/blob/master/inc/cache_enabler_disk.class.php
Regards.
Or more simple add db option to list wich header keys is used for create cache key. Simple input text comma separeted.
Then concat md5 request url with headers values from option headers keys.
Regards,
Thanks
Like this:
In my wp-config i define
define('WP_REST_CACHE_KEY_HEADERS', ['X_WP_LANG']);
In class Endpoint_Api
/**
* Get the requested URI and create the cache key.
*
* @return string The request URI.
*/
public function build_request_uri() {
$home_url = get_home_url(null, '', 'relative');
$request_uri = str_replace($home_url, '', filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL ));
$uri_parts = wp_parse_url( $request_uri );
$request_path = rtrim( $uri_parts['path'], '/' );
$cache_key_headers = '';
if ( isset( $uri_parts['query'] ) && ! empty( $uri_parts['query'] ) ) {
parse_str( $uri_parts['query'], $params );
ksort( $params );
$request_path .= '?' . http_build_query( $params );
}
if (defined('WP_REST_CACHE_KEY_HEADERS') && is_array(WP_REST_CACHE_KEY_HEADERS)) {
foreach (WP_REST_CACHE_KEY_HEADERS as $key) {
if (!isset($_SERVER["HTTP_$key"])) {
continue;
}
$val = $_SERVER["HTTP_$key"];
$cache_key_headers = "$cache_key_headers_$val";
}
}
$this->request_uri = $request_path;
$this->cache_key = md5( $this->request_uri ) . (empty($cache_key_headers) ? '' : '_') . $cache_key_headers;
return $request_path;
}
-
This reply was modified 5 years, 9 months ago by
redcastor.
And for regenerate expired cache
Class Caching line 1027
// Regenerate.
$url = get_home_url() . $result['request_uri'];
$headers = array_slice(explode('_', $result['cache_key']), 1);
if (!empty($headers) && defined('WP_REST_CACHE_KEY_HEADERS') && is_array(WP_REST_CACHE_KEY_HEADERS)) {
$cache_headers = WP_REST_CACHE_KEY_HEADERS;
foreach ($headers as $index => $value) {
if (isset($cache_headers[$index])) {
$headers[$cache_headers[$index]] = $value;
}
unset($headers[$index]);
}
}
$return = wp_remote_get(
$url,
[
'timeout' => 10,
'sslverify' => false,
'headers' => $headers,
]
);
Hello,
You can find the implementation here: https://github.com/RedCastor/wp-rest-cache
Plugin Author
Acato
(@acato)
Hi @redcastor
Wow, you have been busy these past few days, thank you for that. This message is to let you know that I will have a look at it, but since I have just been to WordCamp Europe I am a little busy right now. But I will get back to you.
Plugin Author
Acato
(@acato)
Hi @redcastor
We have decided to do it slightly different than your implementation, but you are now able to specify request headers for caching. See the new FAQ entry for more details.
Hello,
Super great.
I did tests and it works well.
Many thanks for your time.
Auban