PHP code example of helick / cache-helpers

1. Go to this page and download the library: Download helick/cache-helpers 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/ */

    

helick / cache-helpers example snippets


use function Helick\CacheHelpers\cache_remember;

function get_latest_posts()
{
    return cache_remember('latest', function () {
        return new WP_Query([
            'posts_per_page' => 5,
            'orderby'        => 'post_date',
            'order'          => 'desc',
        ]);
    }, 'posts', HOUR_IN_SECONDS);
}

use function Helick\CacheHelpers\cache_forget;

function display_error_message()
{
    $error_message = cache_forget('form_errors', 'my-cache-group', false);

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

use function Helick\CacheHelpers\transient_remember;

function get_tweets()
{
    $user_id = get_current_user_id();
    $key     = 'latest_tweets_' . $user_id;

    return transient_remember($key, function () use ($user_id) {
        return get_latest_tweets_for_user($user_id);
    }, 15 * MINUTE_IN_SECONDS);
}

use function Helick\CacheHelpers\transient_forget;

function display_error_message()
{
    $error_message = transient_forget('form_errors', false);

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

use function Helick\CacheHelpers\site_transient_remember;

function get_tweets()
{
    $user_id = get_current_user_id();
    $key     = 'latest_tweets_' . $user_id;

    return site_transient_remember($key, function () use ($user_id) {
        return get_latest_tweets_for_user($user_id);
    }, 15 * MINUTE_IN_SECONDS);
}

use function Helick\CacheHelpers\site_transient_forget;

function display_error_message()
{
    $error_message = site_transient_forget('form_errors', false);

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