| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Db_Queries; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Db_Queries\Views\View_Base; |
| 7 |
|
| 8 |
// If this file is called directly, abort. |
| 9 |
if ( ! defined( 'WPINC' ) ) { |
| 10 |
die; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* @method Query_Cache_Builder set_view( View_Base $view ) |
| 15 |
* |
| 16 |
* Class Query_Builder |
| 17 |
* @package Jet_Form_Builder\Db_Queries |
| 18 |
*/ |
| 19 |
class Query_Cache_Builder extends Query_Builder { |
| 20 |
|
| 21 |
const CACHE_GROUP = 'jfb_queries'; |
| 22 |
protected $hash; |
| 23 |
|
| 24 |
/** |
| 25 |
* @param string $prefix |
| 26 |
* |
| 27 |
* @return string |
| 28 |
*/ |
| 29 |
public function get_hash( $prefix = '' ) { |
| 30 |
if ( ! empty( $this->hash ) ) { |
| 31 |
return $this->hash; |
| 32 |
} |
| 33 |
$this->hash = $prefix . md5( $this->sql() ); |
| 34 |
|
| 35 |
return $this->hash; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @param string $prefix |
| 40 |
* |
| 41 |
* @return false|mixed |
| 42 |
*/ |
| 43 |
public function get_cached( $prefix = '' ) { |
| 44 |
return wp_cache_get( $this->get_hash( $prefix ), self::CACHE_GROUP ); |
| 45 |
} |
| 46 |
|
| 47 |
public function query_all(): array { |
| 48 |
$cached = $this->get_cached( 'query_all_' ); |
| 49 |
|
| 50 |
if ( false !== $cached ) { |
| 51 |
return $cached; |
| 52 |
} |
| 53 |
|
| 54 |
$results = parent::query_all(); |
| 55 |
|
| 56 |
wp_cache_set( $this->get_hash(), $results, self::CACHE_GROUP ); |
| 57 |
|
| 58 |
return $results; |
| 59 |
} |
| 60 |
|
| 61 |
public function query_one(): array { |
| 62 |
$cached = $this->get_cached( 'query_one_' ); |
| 63 |
|
| 64 |
if ( false !== $cached ) { |
| 65 |
return $cached; |
| 66 |
} |
| 67 |
|
| 68 |
$results = parent::query_one(); |
| 69 |
|
| 70 |
wp_cache_set( $this->get_hash(), $results, self::CACHE_GROUP ); |
| 71 |
|
| 72 |
return $results; |
| 73 |
} |
| 74 |
|
| 75 |
public function query_var( $column_offset = 0, $row_offset = 0 ) { |
| 76 |
$cached = $this->get_cached( "query_var_{$column_offset}_{$row_offset}_" ); |
| 77 |
|
| 78 |
if ( false !== $cached ) { |
| 79 |
return $cached; |
| 80 |
} |
| 81 |
|
| 82 |
$results = parent::query_var(); |
| 83 |
|
| 84 |
wp_cache_set( $this->get_hash(), $results, self::CACHE_GROUP ); |
| 85 |
|
| 86 |
return $results; |
| 87 |
} |
| 88 |
|
| 89 |
public function query_col( $index = 0 ) { |
| 90 |
$cached = $this->get_cached( "query_col_{$index}_" ); |
| 91 |
|
| 92 |
if ( false !== $cached ) { |
| 93 |
return $cached; |
| 94 |
} |
| 95 |
|
| 96 |
$results = parent::query_col( $index ); |
| 97 |
|
| 98 |
wp_cache_set( $this->get_hash(), $results, self::CACHE_GROUP ); |
| 99 |
|
| 100 |
return $results; |
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
|