RequestCacheService.php
3 years ago
RequestService.php
3 years ago
RequestServiceProvider.php
3 years ago
RequestCacheService.php
173 lines
| 1 | <?php |
| 2 | namespace SureCart\Request; |
| 3 | |
| 4 | /** |
| 5 | * Request caching service. |
| 6 | */ |
| 7 | class RequestCacheService { |
| 8 | /** |
| 9 | * Has this been cached yet for the request? |
| 10 | * |
| 11 | * @var boolean |
| 12 | */ |
| 13 | protected static $cached = false; |
| 14 | |
| 15 | /** |
| 16 | * The endpoint for the request |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $endpoint = ''; |
| 21 | |
| 22 | /** |
| 23 | * The args for the request |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | protected $args = []; |
| 28 | |
| 29 | /** |
| 30 | * The cache key on the account. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | protected $account_cache_key = ''; |
| 35 | |
| 36 | /** |
| 37 | * Needs an endpoint and args |
| 38 | * |
| 39 | * @param string $endpoint The endpoint for the request. |
| 40 | * @param array $args The args for the request. |
| 41 | * @param string $account_cache_key The key on the account model to check for cache. |
| 42 | */ |
| 43 | public function __construct( $endpoint, $args, $account_cache_key ) { |
| 44 | $this->endpoint = $endpoint; |
| 45 | $this->args = $args; |
| 46 | $this->account_cache_key = $account_cache_key; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Can we use transient caching. |
| 51 | * |
| 52 | * @return boolean |
| 53 | */ |
| 54 | public function canUseTransient() { |
| 55 | $expand = $this->args['query']['expand'] ?? []; |
| 56 | // if any expand has nested expansion, do not cache. |
| 57 | foreach ( $expand as $item ) { |
| 58 | if ( strpos( '.', $item ) !== false ) { |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | return true; |
| 63 | |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get the object cache key. |
| 68 | * |
| 69 | * @return string |
| 70 | */ |
| 71 | public function getObjectCacheKey() { |
| 72 | return $this->endpoint . wp_json_encode( $this->args ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get the transient cache key. |
| 77 | * |
| 78 | * @return string |
| 79 | */ |
| 80 | public function getTransientCacheKey() { |
| 81 | $timestamp = \SureCart::account()->{$this->account_cache_key} ?? 0; |
| 82 | if ( ! $timestamp ) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | return $this->endpoint . wp_json_encode( $this->args ) . $timestamp; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Set the cache. |
| 91 | * |
| 92 | * @param mixed $data Data to set. |
| 93 | * @param string $type The type of cache to set. |
| 94 | * |
| 95 | * @return boolean |
| 96 | */ |
| 97 | public function setCache( $data, $type ) { |
| 98 | switch ( $type ) { |
| 99 | case 'object': |
| 100 | return $this->setObjectCache( $data ); |
| 101 | case 'transient': |
| 102 | return $this->setTransientCache( $data ); |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Set the object cache. |
| 109 | * |
| 110 | * @param mixed $data Data to set. |
| 111 | * |
| 112 | * @return boolean |
| 113 | */ |
| 114 | public function setObjectCache( $data ) { |
| 115 | $object_cache_key = $this->getObjectCacheKey(); |
| 116 | // set in object cache. |
| 117 | if ( $object_cache_key ) { |
| 118 | return wp_cache_set( $object_cache_key, $data ); |
| 119 | } |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get request from object cache. |
| 125 | * |
| 126 | * @return mixed |
| 127 | */ |
| 128 | public function getObjectCache() { |
| 129 | // we cache this so we can request it several times. |
| 130 | $object_cache_key = $this->getObjectCacheKey(); |
| 131 | |
| 132 | // flush the cache on the first request to clear any redis caching. |
| 133 | if ( ! self::$cached ) { |
| 134 | wp_cache_flush(); |
| 135 | self::$cached = true; |
| 136 | } |
| 137 | |
| 138 | // already made the request this cycle. |
| 139 | return wp_cache_get( $object_cache_key ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Set the transient cache. |
| 144 | * |
| 145 | * @param mixed $data Data to set. |
| 146 | * |
| 147 | * @return boolean |
| 148 | */ |
| 149 | public function setTransientCache( $data ) { |
| 150 | if ( ! $this->canUseTransient() ) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | $transient_cache_key = $this->getTransientCacheKey(); |
| 155 | if ( $transient_cache_key ) { |
| 156 | return set_transient( $transient_cache_key, $data, 60 * MINUTE_IN_SECONDS ); |
| 157 | } |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get the transient cache. |
| 163 | * |
| 164 | * @return mixed |
| 165 | */ |
| 166 | public function getTransientCache() { |
| 167 | if ( ! $this->canUseTransient() ) { |
| 168 | return false; |
| 169 | } |
| 170 | return get_transient( $this->getTransientCacheKey() ); |
| 171 | } |
| 172 | } |
| 173 |