Code Monkey home page Code Monkey logo

wp-psr-cache's Introduction

Build Status All Contributors Code Climate Test Coverage Latest Stable Version License

WP PSR Cache

Object cache implementation for WordPress that acts as an adapter for PSR-6 and PSR-16 caching libraries.

What do PSR-6 and PSR-16 mean?

PSR-6 and PSR-16 are standards established by the PHP-FIG organization. These standards are commonly used in PHP projects of any kind (WordPress is unfortunately an exception), and since this library acts as an adapter, you can use any compatible caching library of your choice with WordPress now. Popular examples include the Symfony Cache Component or Stash.

Features

  • Any PSR-6 or PSR-16 cache implementation can be used
  • Persistent and non-persistent cache implementations can be individually specified
  • Support for reading/writing/deleting multiple cache keys at once
  • Only checks persistent cache if value not already present in non-persistent cache
  • Full multisite support, including site and network switching
  • Allows registration of further cache implementations for fine-grained control per cache group

How to Install

Require this library as a dependency when managing your project with Composer (for example by using Bedrock). You also have to install an actual PSR-6 or PSR-16 cache implementation.

After the installation, you need to move the includes/object-cache.php file into your wp-content directory. If you prefer, you can also automate that process by adding the following to your project's composer.json:

	"scripts": {
		"post-install-cmd": [
			"cp -rp web/app/mu-plugins/wp-psr-cache/includes/object-cache.php web/app/object-cache.php"
		]
	}

Then, replace the inline comment in the object-cache.php file with the actual instantiations of the classes you want to use. You need to provide two implementations, one for the persistent cache and another for the non-persistent cache.

To prevent conflicts with multiple WordPress installations accessing the same cache service, it is recommended to define a unique WP_CACHE_KEY_PREFIX constant in your wp-config.php file.

Example

The following example uses the symfony/cache library, so you have to require it in your composer.json. It then uses that library's Memcached implementation as persistent cache and its array storage as non-persistent cache.

<?php
/**
 * Object cache drop-in
 *
 * @package LeavesAndLove\WpPsrCache
 * @license GNU General Public License, version 2
 * @link    https://github.com/felixarntz/wp-psr-cache
 */

use LeavesAndLove\WpPsrCache\ObjectCacheService;
use Symfony\Component\Cache\Simple\MemcachedCache;
use Symfony\Component\Cache\Simple\ArrayCache;

defined( 'ABSPATH' ) || exit;

ObjectCacheService::loadApi();

/**
 * Defines and thus starts the object cache.
 *
 * @since 1.0.0
 */
function wp_psr_start_cache() {
	$memcached = new Memcached();
	$memcached->addServer( '127.0.0.1', 11211, 20 );

	wp_cache_start( new MemcachedCache( $memcached ), new ArrayCache() );
}

wp_psr_start_cache();

If you prefer to have more granular control and use more than just one persistent and one non-persistent cache, you can register additional cache adapters using the LeavesAndLove\WpPsrCache\CacheSelector\CacheSelector interface. The implementation used by the object cache can easily be retrieved via wp_object_cache()->getSelector().

Requirements

  • PHP >= 7.0

Contributors

Thanks goes to these wonderful people (emoji key):


Felix Arntz

๐Ÿ’ป ๐Ÿ› ๐Ÿ“– ๐Ÿ’ก ๐Ÿค” โš ๏ธ

Alain Schlesser

๐Ÿ’ป ๐Ÿ› ๐Ÿ‘€

Thorsten Frommen

๐Ÿ‘€

Jip

๐Ÿค”

This project follows the all-contributors specification. Contributions of any kind welcome!

wp-psr-cache's People

Contributors

felixarntz avatar schlessera avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

wp-psr-cache's Issues

I is it possible to give a proper way to install this...

it's not possible to install

  • i install the depencies
  • move the object-cache.php to wp-content
  • don'T know what to do with function.php
  • don't know where to install it, i install it in mu-plugins.

i hope you can give a propre way to install it cause you skip a lot's of step

i tried to installed it

sincerly you give too little information on how to install it and make it work.

is it possible to have a procedure because i really love the idea to get this working on my wordpress

or can you explain it to me in a way a slow person get it...

Rename `Router`

The name Router is confusing as it is associated with actual routing functionality. While it technically routes a group to a cache adapter to use, let's find a more suitable name.

Prefer actual methods over `__callStatic()`

The magic method __callStatic() is nice as a dynamic fallback for classes where you don't know upfront what methods to use.

However, in the case of your ObjectService, I would recommend adding all the ObjectCache methods directly to the facade, as the __callStatic() magic method comes at a steep overhead cost. You can keep it in for fallbacks in case someone uses more complex object cache implementations, but as this code is performance-sensitive, it should not be used for the default code paths.

Allow registering more cache adapters

Currently, cache adapters are required for persistent and non-persistent cache. It should be possible to optionally register further adapters and have them be used under certain conditions, falling back to the default ones.

Introduce `wp_cache_start()`

A function wp_cache_start() should be introduced that contains pretty much all functionality currently part of wp_psr_start_cache() in object-cache.php. It should take the two cache implementations as parameters.

This makes object-cache.php much lighter and easier to use.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.