| 1 |
<?php |
| 2 |
namespace WPIDE\App\Services\Cache; |
| 3 |
|
| 4 |
use WPIDE\App\Services\Service; |
| 5 |
|
| 6 |
class Cache implements Service |
| 7 |
{ |
| 8 |
|
| 9 |
protected $group; |
| 10 |
|
| 11 |
public function init(array $config = []) |
| 12 |
{ |
| 13 |
|
| 14 |
$this->group = !empty($config['group']) ? $config['group'] : ''; |
| 15 |
} |
| 16 |
|
| 17 |
public function set($key, $val, $expire = 0) { |
| 18 |
|
| 19 |
return wp_cache_set($key, $val, $this->group, $expire); |
| 20 |
} |
| 21 |
|
| 22 |
public function get($key) { |
| 23 |
|
| 24 |
return wp_cache_get( $key, $this->group ); |
| 25 |
} |
| 26 |
|
| 27 |
public function delete($key) { |
| 28 |
|
| 29 |
return wp_cache_delete( $key, $this->group ); |
| 30 |
} |
| 31 |
|
| 32 |
public function exists($key) { |
| 33 |
|
| 34 |
return $this->get( $key ) !== false; |
| 35 |
} |
| 36 |
|
| 37 |
public function flush() { |
| 38 |
|
| 39 |
return wp_cache_flush(); |
| 40 |
} |
| 41 |
|
| 42 |
public function result($key, callable $callback, $expiration = 0) { |
| 43 |
|
| 44 |
$cached = $this->get($key); |
| 45 |
|
| 46 |
if($cached === false) { |
| 47 |
|
| 48 |
$cached = $callback(); |
| 49 |
|
| 50 |
$this->set($key, $cached, $expiration); |
| 51 |
} |
| 52 |
|
| 53 |
return $cached; |
| 54 |
} |
| 55 |
|
| 56 |
} |