承接 duckdev/wp-cache-helper 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

duckdev/wp-cache-helper

最新稳定版本:v1.0.0

Composer 安装命令:

composer require duckdev/wp-cache-helper

包简介

Helper class for the WordPress object cache and transients with group flush feature.

README 文档

README

WP Cache Helper

WP Cache Helper is a simple WordPress library class to introduce convenient new caching functions.

Built to support group cache flush for WordPress' object cache, which is not supported by core yet.

This helper can simplify something like this:

function do_something() {
    $cache_key = 'some-cache-key';
    $cached    = wp_cache_get( $cache_key );

    // Return the cached value.
    if ( $cached ) {
        return $cached;
    }

    // Do all the work to calculate the value.
    $value = a_whole_lotta_processing();

    // Cache the value.
    wp_cache_set( $cache_key, $value );

    return $value;
}

That pattern works well, but there's a lot of repeated code. This package draws inspiration from Laravel's Cache::remember() method; using Cache::remember(), the same code from above becomes:

// Use this as a global variable or something.
$cache = new \DuckDev\Cache\Cache();

function do_something() {
    return $cache->remember( 'some-cache-key', function () {
        return a_whole_lotta_processing();
    } );
}

Installation

The recommended way to install this library in your project is via Composer:

$ composer require duckdev/wp-cache-helper

Usage

WP Cache Remember provides the following functions for WordPress:

Each function checks the response of the callback for a WP_Error object, ensuring you're not caching temporary errors for long periods of time. PHP Exceptions will also not be cached.

$cache->remember()

Retrieve a value from the object cache. If it doesn't exist, run the $callback to generate and cache the value.

Parameters

(string) $key
The cache key.
(callable) $callback
The callback used to generate and cache the value.
(string) $group
Optional. The cache group. Default is empty.
(int) $expire
Optional. The number of seconds before the cache entry should expire. Default is 0 (as long as possible).

Example

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

$cache->forget()

Retrieve and subsequently delete a value from the object cache.

Parameters

(string) $key
The cache key.
(string) $group
Optional. The cache group. Default is empty.
(mixed) $default
Optional. The default value to return if the given key doesn't exist in the object cache. Default is null.

Example

function show_error_message() {
    $error_message = $cache->forget( 'form_errors', 'my-cache-group', false );

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

$cache->persist()

Retrieve a value from transients. If it doesn't exist, run the $callback to generate and cache the value.

Parameters

(string) $key
The cache key.
(callable) $callback
The callback used to generate and cache the value.
(string) $site
Should use site transients.
(int) $expire
Optional. The number of seconds before the cache entry should expire. Default is 0 (as long as possible).

Example

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

    return $cache->persist( $key, function () use ( $user_id ) {
        return get_latest_tweets_for_user( $user_id );
    }, 15 * MINUTE_IN_SECONDS );
}

$cache->cease()

Retrieve and subsequently delete a value from the transient cache.

Parameters

(string) $key
The cache key.
(string) $site
Should use site transients.
(mixed) $default
Optional. The default value to return if the given key doesn't exist in transients. Default is null.

$cache->flush_group()

Flush a cache group items. Use this and do not flush entire cache.

Parameters

(string) $group
The cache group name.

$cache->flush()

Wrapper for wp_cache_flush to check if other method is available for flushing if wp_cache_flush is disabled.

Credits

License

GPLv2+

统计信息

  • 总下载量: 27
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2021-08-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固