|
| 1 | +<?php |
| 2 | + |
| 3 | +defined( 'ABSPATH' ) || exit; // block direct access. |
| 4 | + |
| 5 | +/** |
| 6 | + * Class WC_Stripe_Database_Cache |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * A class for caching data as an option in the database. |
| 11 | + * |
| 12 | + * Based on the WooCommerce Payments Database_Cache class implementation. |
| 13 | + * |
| 14 | + * @see https://github.com/Automattic/woocommerce-payments/blob/4b084af108cac9c6bd2467e52e5cdc3bc974a951/includes/class-database-cache.php |
| 15 | + */ |
| 16 | +class WC_Stripe_Database_Cache { |
| 17 | + |
| 18 | + /** |
| 19 | + * In-memory cache for the duration of a single request. |
| 20 | + * |
| 21 | + * This is used to avoid multiple database reads for the same data and as a backstop in case the database write fails. |
| 22 | + * |
| 23 | + * @var array |
| 24 | + */ |
| 25 | + private static $in_memory_cache = []; |
| 26 | + |
| 27 | + /** |
| 28 | + * Class constructor. |
| 29 | + */ |
| 30 | + private function __construct() { |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Stores a value in the cache. |
| 35 | + * |
| 36 | + * @param string $key The key to store the value under. |
| 37 | + * @param mixed $data The value to store. |
| 38 | + * @param int $ttl The TTL of the cache. Dafault 1 hour. |
| 39 | + * |
| 40 | + * @return void |
| 41 | + */ |
| 42 | + public static function set( $key, $data, $ttl = HOUR_IN_SECONDS ) { |
| 43 | + self::write_to_cache( $key, $data, $ttl ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Gets a value from the cache. |
| 48 | + * |
| 49 | + * @param string $key The key to look for. |
| 50 | + * |
| 51 | + * @return mixed|null The cache contents. NULL if the cache value is expired or missing. |
| 52 | + */ |
| 53 | + public static function get( $key ) { |
| 54 | + $cache_contents = self::get_from_cache( $key ); |
| 55 | + if ( is_array( $cache_contents ) && array_key_exists( 'data', $cache_contents ) ) { |
| 56 | + if ( self::is_expired( $key, $cache_contents ) ) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + return $cache_contents['data']; |
| 61 | + } |
| 62 | + |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Deletes a value from the cache. |
| 68 | + * |
| 69 | + * @param string $key The key to delete. |
| 70 | + * |
| 71 | + * @return void |
| 72 | + */ |
| 73 | + public static function delete( $key ) { |
| 74 | + // Remove from the in-memory cache. |
| 75 | + unset( self::$in_memory_cache[ $key ] ); |
| 76 | + |
| 77 | + // Remove from the DB cache. |
| 78 | + if ( delete_option( $key ) ) { |
| 79 | + // Clear the WP object cache to ensure the new data is fetched by other processes. |
| 80 | + wp_cache_delete( $key, 'options' ); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Wraps the data in the cache metadata and stores it. |
| 86 | + * |
| 87 | + * @param string $key The key to store the data under. |
| 88 | + * @param mixed $data The data to store. |
| 89 | + * @param int $ttl The TTL of the cache. |
| 90 | + * |
| 91 | + * @return void |
| 92 | + */ |
| 93 | + private static function write_to_cache( $key, $data, $ttl ) { |
| 94 | + // Add the data and expiry time to the array we're caching. |
| 95 | + $cache_contents = [ |
| 96 | + 'data' => $data, |
| 97 | + 'ttl' => $ttl, |
| 98 | + 'updated' => time(), |
| 99 | + ]; |
| 100 | + |
| 101 | + // Write the in-memory cache. |
| 102 | + self::$in_memory_cache[ $key ] = $cache_contents; |
| 103 | + |
| 104 | + // Create or update the DB option cache. |
| 105 | + // Note: Since we are adding the current time to the option value, WP will ALWAYS write the option because |
| 106 | + // the cache contents value is different from the current one, even if the data is the same. |
| 107 | + // A `false` result ONLY means that the DB write failed. |
| 108 | + // Yes, there is the possibility that we attempt to write the same data multiple times within the SAME second, |
| 109 | + // and we will mistakenly think that the DB write failed. We are OK with this false positive, |
| 110 | + // since the actual data is the same. |
| 111 | + // |
| 112 | + // Note 2: Autoloading too many options can lead to performance problems, and we are implementing this as a |
| 113 | + // general cache for the plugin, so we set the autoload to false. |
| 114 | + $result = update_option( $key, $cache_contents, false ); |
| 115 | + if ( false !== $result ) { |
| 116 | + // If the DB cache write succeeded, clear the WP object cache to ensure the new data is fetched by other processes. |
| 117 | + wp_cache_delete( $key, 'options' ); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Get the cache contents for a certain key. |
| 123 | + * |
| 124 | + * @param string $key The cache key. |
| 125 | + * |
| 126 | + * @return array|false The cache contents (array with `data`, `ttl`, and `updated` entries). |
| 127 | + * False if there is no cached data. |
| 128 | + */ |
| 129 | + private static function get_from_cache( $key ) { |
| 130 | + // Check the in-memory cache first. |
| 131 | + if ( isset( self::$in_memory_cache[ $key ] ) ) { |
| 132 | + return self::$in_memory_cache[ $key ]; |
| 133 | + } |
| 134 | + |
| 135 | + // Read from the DB cache. |
| 136 | + $data = get_option( $key ); |
| 137 | + |
| 138 | + // Store the data in the in-memory cache, including the case when there is no data cached (`false`). |
| 139 | + self::$in_memory_cache[ $key ] = $data; |
| 140 | + |
| 141 | + return $data; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Checks if the cache value is expired. |
| 146 | + * |
| 147 | + * @param string $key The cache key. |
| 148 | + * @param array $cache_contents The cache contents. |
| 149 | + * |
| 150 | + * @return boolean True if the contents are expired. False otherwise. |
| 151 | + */ |
| 152 | + private static function is_expired( $key, $cache_contents ) { |
| 153 | + if ( ! is_array( $cache_contents ) || ! isset( $cache_contents['updated'] ) || ! isset( $cache_contents['ttl'] ) ) { |
| 154 | + // Treat bad/invalid cache contents as expired |
| 155 | + return true; |
| 156 | + } |
| 157 | + |
| 158 | + // Double-check that we have integers for `updated` and `ttl`. |
| 159 | + if ( ! is_int( $cache_contents['updated'] ) || ! is_int( $cache_contents['ttl'] ) ) { |
| 160 | + return true; |
| 161 | + } |
| 162 | + |
| 163 | + $expires = $cache_contents['updated'] + $cache_contents['ttl']; |
| 164 | + $now = time(); |
| 165 | + |
| 166 | + return apply_filters( 'wcstripe_database_cache_is_expired', $expires < $now, $key, $cache_contents ); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Get all cached keys in memory. |
| 171 | + * |
| 172 | + * @return array The cached keys. |
| 173 | + */ |
| 174 | + public static function get_cached_keys() { |
| 175 | + return array_keys( self::$in_memory_cache ); |
| 176 | + } |
| 177 | +} |
0 commit comments