| 1 |
<?php |
| 2 |
|
| 3 |
namespace Leadpages; |
| 4 |
|
| 5 |
defined('ABSPATH') || die('No script kiddies please!'); // Avoid direct file request |
| 6 |
|
| 7 |
|
| 8 |
/** |
| 9 |
* A class for managing a cache for Leadpages asset serving backed by the WP Transients API. |
| 10 |
* |
| 11 |
* WordPress Transients API: https://developer.wordpress.org/apis/transients/ |
| 12 |
* Transient expiration times are a maximum time. There is no minimum age. Transients |
| 13 |
* might disappear one second after you set them, or 24 hours, but they will never be |
| 14 |
* around after the expiration time. |
| 15 |
* |
| 16 |
* Pages are cached by the slug that they are published under in WordPress so that they |
| 17 |
* can be quickly referenced when serving. The entire response when fetching the page |
| 18 |
* is cached, not just the page HTML content. |
| 19 |
*/ |
| 20 |
class Cache { |
| 21 |
// The maximum amount of time a value should be cached for |
| 22 |
private static $max_time = 60 * 60 * 24; // 1 day |
| 23 |
|
| 24 |
// The prefix to cache pages with |
| 25 |
private static $page_key_prefix = LEADPAGES_OPT_PREFIX . '_page_'; |
| 26 |
|
| 27 |
/* |
| 28 |
* Build the cache key given the slug of the page. |
| 29 |
* |
| 30 |
* @param string $slug |
| 31 |
* @return string |
| 32 |
*/ |
| 33 |
public static function page_key( $slug ) { |
| 34 |
return self::$page_key_prefix . $slug; |
| 35 |
} |
| 36 |
|
| 37 |
/* |
| 38 |
* Set a value in the cache. |
| 39 |
* |
| 40 |
* Example: |
| 41 |
* Cache::set(Cache::page_key($slug), $page) |
| 42 |
* |
| 43 |
* @param string $name |
| 44 |
* @param mixed $value |
| 45 |
* @return boolean |
| 46 |
*/ |
| 47 |
public static function set( $name, $value ) { |
| 48 |
return set_transient($name, $value, self::$max_time); |
| 49 |
} |
| 50 |
|
| 51 |
/* |
| 52 |
* Retrieve a value from the cache. |
| 53 |
* |
| 54 |
* @param string $name |
| 55 |
* @return mixed |
| 56 |
*/ |
| 57 |
public static function get( $name ) { |
| 58 |
return get_transient($name); |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
* Remove a value from the cache. |
| 63 |
* |
| 64 |
* @param string $name |
| 65 |
* @return boolean |
| 66 |
*/ |
| 67 |
public static function delete( $name ) { |
| 68 |
return delete_transient($name); |
| 69 |
} |
| 70 |
} |
| 71 |
|