Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/felixarntz/wp-psr-cache

Object cache implementation for WordPress that acts as an adapter for PSR-6 and PSR-16 caching libraries.
https://github.com/felixarntz/wp-psr-cache

object-cache psr-16 psr-6 wordpress-dropin wordpress-muplugin

Last synced: about 1 month ago
JSON representation

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

Awesome Lists containing this project

README

        

[![Build Status](https://api.travis-ci.org/felixarntz/wp-psr-cache.png?branch=master)](https://travis-ci.org/felixarntz/wp-psr-cache)
[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors)
[![Code Climate](https://codeclimate.com/github/felixarntz/wp-psr-cache/badges/gpa.svg)](https://codeclimate.com/github/felixarntz/wp-psr-cache)
[![Test Coverage](https://codeclimate.com/github/felixarntz/wp-psr-cache/badges/coverage.svg)](https://codeclimate.com/github/felixarntz/wp-psr-cache/coverage)
[![Latest Stable Version](https://poser.pugx.org/felixarntz/wp-psr-cache/version)](https://packagist.org/packages/felixarntz/wp-psr-cache)
[![License](https://poser.pugx.org/felixarntz/wp-psr-cache/license)](https://packagist.org/packages/felixarntz/wp-psr-cache)

# 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](http://www.php-fig.org/psr/psr-6/) and [PSR-16](http://www.php-fig.org/psr/psr-16/) are standards established by the [PHP-FIG](http://www.php-fig.org/) 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](https://github.com/symfony/cache) or [Stash](https://github.com/tedious/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](https://github.com/roots/bedrock)). You also have to install an actual [PSR-6](https://packagist.org/providers/psr/cache-implementation) or [PSR-16](https://packagist.org/providers/psr/simple-cache-implementation) 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
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`](https://github.com/felixarntz/wp-psr-cache/blob/master/src/CacheSelector/CacheSelector.php) 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](https://github.com/kentcdodds/all-contributors#emoji-key)):

| [
Felix Arntz](https://leaves-and-love.net)
[💻](https://github.com/felixarntz/wp-psr-cache/commits?author=felixarntz "Code") [🐛](https://github.com/felixarntz/wp-psr-cache/issues?q=author%3Afelixarntz "Bug reports") [📖](https://github.com/felixarntz/wp-psr-cache/commits?author=felixarntz "Documentation") [💡](#example-felixarntz "Examples") [🤔](#ideas-felixarntz "Ideas, Planning, & Feedback") [⚠️](https://github.com/felixarntz/wp-psr-cache/commits?author=felixarntz "Tests") | [
Alain Schlesser](https://www.alainschlesser.com/)
[💻](https://github.com/felixarntz/wp-psr-cache/commits?author=schlessera "Code") [🐛](https://github.com/felixarntz/wp-psr-cache/issues?q=author%3Aschlessera "Bug reports") [👀](#review-schlessera "Reviewed Pull Requests") | [
Thorsten Frommen](https://tfrommen.de)
[👀](#review-tfrommen "Reviewed Pull Requests") | [
Jip](http://www.jipmoors.nl)
[🤔](#ideas-moorscode "Ideas, Planning, & Feedback") |
| :---: | :---: | :---: | :---: |

This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!