From bb9dcda6b3aacf3dde0fe60e3b838a617e91b55a Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Wed, 26 Oct 2016 23:17:11 +0100 Subject: [PATCH] cache core Wordpress queries --- src/Models/Post.php | 3 +++ src/Proxy/WordpressDatabase.php | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Models/Post.php b/src/Models/Post.php index fc3f5be..61e3838 100644 --- a/src/Models/Post.php +++ b/src/Models/Post.php @@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany; use Koselig\Exceptions\UnsatisfiedDependencyException; use Koselig\Support\Action; use Koselig\Support\Wordpress; +use Watson\Rememberable\Rememberable; use WP_Post; /** @@ -19,6 +20,8 @@ use WP_Post; */ class Post extends Model { + use Rememberable; + public $timestamps = false; protected $table = DB_PREFIX . 'posts'; protected $primaryKey = 'ID'; diff --git a/src/Proxy/WordpressDatabase.php b/src/Proxy/WordpressDatabase.php index 14ed675..f19348f 100644 --- a/src/Proxy/WordpressDatabase.php +++ b/src/Proxy/WordpressDatabase.php @@ -1,6 +1,7 @@ sql_mode); + $modes = Cache::remember('sql_modes', config('wordpress.caching'), function () { + return explode(',', DB::selectOne('SELECT @@SESSION.sql_mode as sql_mode')->sql_mode); + }); } $modes = array_change_key_case($modes, CASE_UPPER); @@ -122,10 +125,11 @@ class WordpressDatabase extends wpdb * @since 0.71 * * @param string $query Database query + * @param bool $skipCache * - * @return int|false Number of rows affected/selected or false on error + * @return false|int Number of rows affected/selected or false on error */ - public function query($query) + public function query($query, $skipCache = false) { if (!$this->ready) { $this->check_current_query = true; @@ -216,7 +220,13 @@ class WordpressDatabase extends wpdb } elseif (preg_match('/^\s*(delete|update|replace)\s/i', $query)) { $this->last_result = $this->result = DB::affectingStatement($query); } else { - $this->last_result = $this->result = DB::select($query); + if (config('wordpress.caching') && starts_with(strtolower($query), 'select')) { + $this->result = Cache::remember('q_' . $query, config('wordpress.caching'), function () use ($query) { + return DB::select($query); + }); + + $this->last_result = $this->result; + } } $this->num_queries++; -- libgit2 1.7.2