woocommerce-multilingual
/
addons
/
wpml-dependencies
/
vendor
/
wpml
/
wp
/
classes
Last commit date
Attachment.php
1 month ago
Cache.php
1 month ago
Gutenberg.php
1 month ago
Hooks.php
1 month ago
Http.php
1 month ago
Nonce.php
1 month ago
Option.php
1 month ago
Post.php
1 month ago
PostType.php
1 month ago
Resources.php
1 month ago
Roles.php
4 years ago
Transient.php
1 month ago
Url.php
1 month ago
User.php
1 month ago
WP.php
1 month ago
WPDB.php
1 month ago
Cache.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPML\LIB\WP; |
| 4 | |
| 5 | use WPML\Collect\Support\Traits\Macroable; |
| 6 | use WPML\FP\Fns; |
| 7 | use WPML\FP\Just; |
| 8 | use WPML\FP\Maybe; |
| 9 | use WPML\FP\Nothing; |
| 10 | use function WPML\FP\curryN; |
| 11 | |
| 12 | class Cache { |
| 13 | |
| 14 | const KEYS = 'WPML_WP_Cache__group_keys'; |
| 15 | |
| 16 | use Macroable; |
| 17 | |
| 18 | public static function init() { |
| 19 | self::macro( 'get', curryN( 2, [ self::class, 'getInternal' ] ) ); |
| 20 | |
| 21 | self::macro( 'set', curryN( 4, function ( $group, $key, $expire, $value ) { |
| 22 | $keys = self::getKeysInGroup( $group ); |
| 23 | if ( ! in_array( $key, $keys, true ) ) { |
| 24 | $keys[] = $key; |
| 25 | \wp_cache_set( $group, [ 'data' => $keys ], self::KEYS ); |
| 26 | } |
| 27 | |
| 28 | return \wp_cache_set( $key, [ 'data' => $value ], $group, $expire ); |
| 29 | } ) ); |
| 30 | |
| 31 | self::macro( 'memorizeWithCheck', curryN( 4, function ( $group, $checkingFn, $expire, $fn ) { |
| 32 | return function () use ( $fn, $group, $checkingFn, $expire ) { |
| 33 | $args = func_get_args(); |
| 34 | $key = self::_buildKeyForFunctionArguments( $args ); |
| 35 | |
| 36 | $result = Cache::get( $group, $key ); |
| 37 | if ( Fns::isNothing( $result ) || ! $checkingFn( $result->get() ) ) { |
| 38 | $result = call_user_func_array( $fn, $args ); |
| 39 | Cache::set( $group, $key, $expire, $result ); |
| 40 | |
| 41 | return $result; |
| 42 | } |
| 43 | |
| 44 | return $result->get(); |
| 45 | }; |
| 46 | } ) ); |
| 47 | |
| 48 | self::macro( 'memorize', self::memorizeWithCheck( Fns::__, Fns::always( true ), Fns::__, Fns::__ ) ); |
| 49 | |
| 50 | } |
| 51 | |
| 52 | public static function getInternal( $group, $key ) { |
| 53 | $found = false; |
| 54 | $result = wp_cache_get( $key, $group, false, $found ); |
| 55 | |
| 56 | if ( $found && is_array( $result ) && array_key_exists( 'data', $result ) ) { |
| 57 | return Maybe::just( $result['data'] ); |
| 58 | } |
| 59 | |
| 60 | return Maybe::nothing(); |
| 61 | } |
| 62 | |
| 63 | public static function flushGroup( $group ) { |
| 64 | $keys = self::getKeysInGroup( $group ); |
| 65 | |
| 66 | foreach ( $keys as $key ) { |
| 67 | wp_cache_delete( $key, $group ); |
| 68 | } |
| 69 | |
| 70 | wp_cache_delete( $group, self::KEYS ); |
| 71 | } |
| 72 | |
| 73 | public static function clearMemoizedFunction( $group, ...$functionArgs ) { |
| 74 | self::delete( $group, self::_buildKeyForFunctionArguments( $functionArgs ) ); |
| 75 | } |
| 76 | |
| 77 | public static function delete( $group, $key ) { |
| 78 | wp_cache_delete( $key, $group ); |
| 79 | |
| 80 | $keys = self::getKeysInGroup( $group ); |
| 81 | $keys = array_values( array_diff( $keys, [ $key ] ) ); |
| 82 | wp_cache_set( $group, [ 'data' => $keys ], self::KEYS ); |
| 83 | } |
| 84 | |
| 85 | public static function getKeysInGroup( $group ) { |
| 86 | return self::getInternal( self::KEYS, $group )->getOrElse( [] ); |
| 87 | } |
| 88 | |
| 89 | public static function _buildKeyForFunctionArguments( array $args ) { |
| 90 | return serialize( $args ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | Cache::init(); |
| 95 |