Code Monkey home page Code Monkey logo

cache-implementations's Introduction

Lightweight library that discovers available PSR-6 Cache implementations by searching for a list of well-known classes that implement the relevant interface, and returns an instance of the first one that is found.

This package is part of the PSR Discovery utility suite, which also supports PSR-18 HTTP Clients, PSR-17 HTTP Factories, PSR-14 Event Dispatcher, PSR-11 Containers and PSR-3 Logs.

This is largely intended for inclusion in libraries like SDKs that wish to support PSR-6 Caches without requiring hard dependencies on specific implementations or demanding extra configuration by users.

Requirements

  • PHP 8.1+
  • Composer 2.0+

Successful discovery requires the presence of a compatible implementation in the host application. This library does not install any implementations for you.

Implementations

The following psr/cache-implementation implementations are discovered and instantiated automatically:

The following implementations can be discovered, but require manual instantiation due to their configuration requirements:

The following mock implementations are also available:

If a particular implementation is missing that you'd like to see, please open a pull request adding support.

Installation

composer require psr-discovery/cache-implementations

Usage

use PsrDiscovery\Discover;

// Return an instance of the first discovered PSR-6 Cache implementation.
$cache = Discover::cache();

$cache->set('foo', 'bar');

You can also use Discover::caches() to retrieve an array with all discovered implementations. This is useful if you want to support implementations that can't be instantiated without configuration.

use PsrDiscovery\Discover;

$caches = Discover::caches();

foreach ($caches as $cache) {
    echo sprintf('Discovered %s v%s', $cache->getPackage(), $cache->getVersion());
}

Handling Failures

If the library is unable to discover a suitable PSR-6 implementation, the Discover::cache() discovery method will simply return null. This allows you to handle the failure gracefully, for example by falling back to a default implementation.

Example:

use PsrDiscovery\Discover;

$cache = Discover::cache();

if ($cache === null) {
    // No suitable Cache implementation was discovered.
    // Fall back to a default implementation.
    $cache = new DefaultCache();
}

Singletons

By default, the Discover::cache() method will always return a new instance of the discovered implementation. If you wish to use a singleton instance instead, simply pass true to the $singleton parameter of the discovery method.

Example:

use PsrDiscovery\Discover;

// $cache1 !== $cache2 (default)
$cache1 = Discover::cache();
$cache2 = Discover::cache();

// $cache1 === $cache2
$cache1 = Discover::cache(singleton: true);
$cache2 = Discover::cache(singleton: true);

Mocking Priority

This library will give priority to searching for a known, available mocking library before searching for a real implementation. This is to allow for easier testing of code that uses this library.

The expectation is that these mocking libraries will always be installed as development dependencies, and therefore if they are available, they are intended to be used.

Preferring an Implementation

If you wish to prefer a specific implementation over others, you can prefer() it by package name:

use PsrDiscovery\Discover;
use PsrDiscovery\Implementations\Psr6\Caches;

// Prefer the a specific implementation of PSR-6 over others.
Caches::prefer('league/container');

// Return an instance of League\Container\Container,
// or the next available from the list of candidates,
// Returns null if none are discovered.
$cache = Discover::cache();

This will cause the cache() method to return the preferred implementation if it is available, otherwise, it will fall back to the default behavior.

Note that assigning a preferred implementation will give it priority over the default preference of mocking libraries.

Using a Specific Implementation

If you wish to force a specific implementation and ignore the rest of the discovery candidates, you can use() its package name:

use PsrDiscovery\Discover;
use PsrDiscovery\Implementations\Psr6\Caches;

// Only discover a specific implementation of PSR-6.
Caches::use('league/container');

// Return an instance of League\Container\Container,
// or null if it is not available.
$cache = Discover::cache();

This will cause the cache() method to return the preferred implementation if it is available, otherwise, it will return null.


This library is not produced or endorsed by, or otherwise affiliated with, the PHP-FIG.

cache-implementations's People

Contributors

evansims avatar janmikes avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

janmikes

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.