keycdn / cache-enabler

A lightweight caching plugin for WordPress that makes your website faster by generating static HTML files.
https://wordpress.org/plugins/cache-enabler/
123 stars 46 forks source link

cache search results #83

Closed centminmod closed 3 years ago

centminmod commented 4 years ago

I've managed to make Cache Enabler cach search results when setup via advanced caching with Nginx server as outlined at https://github.com/centminmod/pretty-search-url. But would be much easier if you can update Cache Enabler to support a configurable admin option as to whether search is cacheable or not. This would allow folks to not have to modify cache_enabler.class.php

with https://github.com/centminmod/pretty-search-url plugin and nginx changes

    if ($args ~* s=(.*)) {
      set $cache_uri $request_uri;
      set $check_surl $cache_uri;
      set $cache_uri /search/$1;
    }
    add_header Check-Uri "$check_surl";
    add_header Set-Uri "$cache_uri";

    # default html file
    set $cache_enabler_uri '${custom_subdir}/wp-content/cache/cache-enabler/${http_host}${cache_uri}index.html';

with wordpress search cached

load test with my forked wrk

wrk-cmm -t4 -c50 -d20s --latency --breakout http://cache-enabler.domain.com/search/worldpress+cache/
Running 20s test @ http://cache-enabler.domain.com/search/worldpress+cache/
  4 threads and 50 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.01ms    4.16ms  99.65ms   97.01%
    Connect   159.21us   71.17us 288.00us   58.33%
    TTFB        1.00ms    4.16ms  99.64ms   97.01%
    TTLB       10.21us   32.01us  11.02ms   99.97%
    Req/Sec    23.94k     9.82k   45.89k    64.12%
  Latency Distribution
     50%  415.00us
     75%  603.00us
     90%    1.19ms
     99%   10.78ms
  1907359 requests in 20.04s, 35.79GB read
Requests/sec:  95189.83
Transfer/sec:      1.79GB

direct query search cached

wrk-cmm -t4 -c50 -d20s --latency --breakout http://cache-enabler.domain.com/?s=wordpress+cache             
Running 20s test @ http://cache-enabler.domain.com/?s=wordpress+cache
  4 threads and 50 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   738.15us    2.44ms 108.60ms   95.81%
    Connect   144.73us   69.16us 280.00us   60.42%
    TTFB      724.86us    2.44ms 108.58ms   95.81%
    TTLB       13.46us   58.63us  14.02ms   99.96%
    Req/Sec    26.26k     6.27k   39.64k    70.00%
  Latency Distribution
     50%  351.00us
     75%  546.00us
     90%    1.21ms
     99%    7.54ms
  2092164 requests in 20.03s, 50.16GB read
Requests/sec: 104448.19
Transfer/sec:      2.50GB

versus with wordpress search non-cached

compared to default without wordpress search cache

cached search at 95,189 OR 104,448 requests/sec versus non-cached search at 332 requests/sec

wrk-cmm -t4 -c50 -d20s --latency --breakout http://cache-enabler.domain.com/?s=wordpress+cache            
Running 20s test @ http://cache-enabler.domain.com/?s=wordpress+cache
  4 threads and 50 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   143.66ms   16.60ms 238.90ms   68.44%
    Connect   165.79us   83.37us 334.00us   56.25%
    TTFB      143.60ms   16.60ms 238.79ms   68.47%
    TTLB       63.16us   25.84us   1.19ms   85.77%
    Req/Sec    83.38     14.48   121.00     68.62%
  Latency Distribution
     50%  144.40ms
     75%  154.74ms
     90%  163.80ms
     99%  185.14ms
  6663 requests in 20.02s, 181.04MB read
Requests/sec:    332.85
Transfer/sec:      9.04MB
coreykn commented 4 years ago

Allowing search results to be cached is a feature that I think would be a good idea to add. In my opinion adding an option in the Cache Behavior setting would work best.

The default search format (e.g. ?s=query) should be able to be cached, which would still allow the rewritten search results (e.g. /search/query/) to be cached. The cached file and the directory names in wp-content/cache/cache-enabler/example.com/ should be the same regardless of the search format used.

I'll start working on this for version 1.3.7.

centminmod commented 4 years ago

Great news ! Not sure if it's something you can resolve too but with my search cache method it doesn't work with WPML language plugin as that generates urls like domain.com/en/?s=query or domain.com/de/?s=query which caching doesn't pick up for separate languages. Maybe something to keep in mind for 1.3.7+ ๐Ÿ˜ƒ

coreykn commented 4 years ago

I'll make sure to check that out. It won't change the release date, however, this will be available in version 1.4.0 instead as that will be the next version released.

coreykn commented 4 years ago

The way that I want to implement allowing search results to be cached has been more complicated than I had previously anticipated. Due to this I've pushed this feature to be released in version 1.5.0 instead. I apologize for the delay.

centminmod commented 4 years ago

What about for now just giving us a admin side toggle option to exclude search ? It would aid in your planned method ? and it shouldn't interfere too much with future work/plans ?

Would allow folks like myself to just roll out Cache Enabler search cache via something like https://github.com/centminmod/pretty-search-url at Nginx level totally bypassing PHP processing.

diff -u cache_enabler.class.php-orig cache_enabler.class.php-removesearch 
--- cache_enabler.class.php-orig        2020-07-17 05:33:33.933006105 +0000
+++ cache_enabler.class.php-removesearch        2020-07-17 05:34:17.572342714 +0000
@@ -1407,7 +1407,7 @@
         }

         // conditional tags
-        if ( self::_is_index() OR is_search() OR is_404() OR is_feed() OR is_trackback() OR is_robots() OR is_preview() OR post_password_required() ) {
+        if ( self::_is_index() OR is_404() OR is_feed() OR is_trackback() OR is_robots() OR is_preview() OR post_password_required() ) {
             return true;
         }
coreykn commented 4 years ago

@centminmod We decided we're going to add a filter hook to allow this instead of what was previously planned. I unfortunately didn't quite get to adding and updating our hooks in version 1.5.0, but this is on the top of my list todos so you shouldn't have to continue to modify the plugin much longer to cache search results. ๐Ÿ™‚

centminmod commented 4 years ago

Cheers @coreykn thanks for the update ๐Ÿ‘