PHP code example of duckdev / wp-cache-helper

1. Go to this page and download the library: Download duckdev/wp-cache-helper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

duckdev / wp-cache-helper example snippets


$cache = \FoxeLabs\Cache\Cache::get_instance( 'my_plugin' );

$cache = new \FoxeLabs\Cache\Cache( 'my_plugin' );

add_filter( 'my_plugin_can_cache', '__return_false' );

$cache = \FoxeLabs\Cache\Cache::get_instance( 'my_plugin' );

function get_latest_posts() {
    global $cache;

    return $cache->remember( 'latest_posts', function () {
        return new WP_Query( array(
            'posts_per_page' => 5,
            'orderby'        => 'post_date',
            'order'          => 'desc',
        ) );
    }, 'queries', HOUR_IN_SECONDS );
}

$error_message = $cache->forget( 'form_errors', 'flash', false );

if ( $error_message ) {
    echo 'An error occurred: ' . esc_html( $error_message );
}

$cache->persist( 'latest_tweets_' . $user_id, function () use ( $user_id ) {
    return get_latest_tweets_for_user( $user_id );
}, false, 15 * MINUTE_IN_SECONDS );

> global $wpdb;
>
> $wpdb->query(
>     "DELETE FROM {$wpdb->options}
>      WHERE option_name LIKE '_transient_duckdev\_cache\_%'
>         OR option_name LIKE '_transient_timeout_duckdev\_cache\_%'"
> );
>
> wp_cache_flush();
> 

src/
├── Cache.php                     # Container + entry point
├── Contracts/
│   ├── ObjectCacheInterface.php
│   └── TransientCacheInterface.php
├── Storage/
│   ├── ObjectCache.php           # wp_cache_* wrapper + version-based group flush
│   └── TransientCache.php        # (site_)transient wrapper
├── Support/
│   └── KeyPrefixer.php           # Shared key + group prefixing
└── Exceptions/
    └── CacheException.php