globalis/wp-cubi-transient-cache

0.3.0 2023-04-21 14:36 UTC

This package is auto-updated.

Last update: 2024-12-21 18:37:40 UTC


README

PHP Version Require Latest Stable Version License

Persistent cache library based on WordPress transients

wp-cubi

Features

  • Provides Cache::set() and Cache::get() methods, using transients
  • Provides Template::get() method, that automatically caches required template part
  • Automatically cache WordPress nav-menus out of the box
  • Clear cache by group, when menus are saved, when posts are saved, when site URL changes

Installation

composer require globalis/wp-cubi-transient-cache

Usage

Generic caching

Save a value in cache :

<?php

use Globalis\WP\Cubi\TransientCache\Cache;

$my_value = my_expensive_function();
Cache::set('my_key', $my_value, 'my_group');

Get a value from cache :

<?php

use Globalis\WP\Cubi\TransientCache\Cache;

$my_value_cached = Cache::get('my_key', 'my_group');

Clear a single cache entry :

<?php

use Globalis\WP\Cubi\TransientCache\Cache;

Cache::clear('my_key', 'my_group');

Clear multiple cache entries :

<?php

use Globalis\WP\Cubi\TransientCache\Cache;

Cache::clearGroups(['my_group']);

Templates caching

Get a template part from cache :

<?php

use Globalis\WP\Cubi\TransientCache\Template;

Template::get('templates/my-part', ['my_arg' => 'example'], 'my_group');

When a template part cache value doesn't exists, Template::get() will automatically loads required template part, and cache it, so you just have to replace calls to WordPress core function get_template_part() with Template::get() to enable cache on a template part.

Templates cache uses generic clear methods, using file path as key (e.g. Cache::clear('templates/my-part', 'my_group');).

Nav-menus caching

Nav-menus are automatically cached and several hooks are setup out of the box to clear their cache when needed.

You can always manually clear all menus cache with :

<?php

use Globalis\WP\Cubi\TransientCache\Cache;

Cache::clearGroups(['menus']);

Procedural style

Most of the above methods can be called in a procedural style, without any namespace :

<?php

wp_cubi_cache_set(string $key, mixed $value, string $group = 'all');
wp_cubi_cache_get(string $key, string $group = 'all');
wp_cubi_cache_clear(string $key, string $group = 'all');
wp_cubi_cache_clear_group(string $group = 'all');
wp_cubi_cache_clear_groups(array $groups = ['all']);
wp_cubi_get_template_part_cached(string $file, array $data = [], string $group = 'all', bool $return = false);

Configuration

Bypass cache when developping, or in a specific environment config file :

<?php

define('WP_CUBI_TRANSIENT_CACHE_BYPASS_ALL', true);

Bypass cache only for template parts :

<?php

define('WP_CUBI_TRANSIENT_CACHE_BYPASS_TEMPLATES', true);

Disable nav-menus automatic caching :

<?php

define('WP_CUBI_TRANSIENT_CACHE_DISABLE_AUTO_CACHE_NAV_MENUS', true);

Hooks

Clear all cache :

do_action('wp-cubi\transient-cache\clear');

Filter clear hooks :

apply_filters('wp-cubi\transient-cache\clear-hooks', $hooks);

Development

Before opening pull requests, please check and apply project coding standards with ./vendor/bin/phpcs . and/or ./vendor/bin/phpcbf .