All Projects → CHH → Cache Service Provider

CHH / Cache Service Provider

Licence: mit
A Cache Service Provider for Silex, using the doctrine/cache package

Projects that are alternatives of or similar to Cache Service Provider

lambda-cache
Python utility for caching in Lambda Functions
Stars: ✭ 28 (+21.74%)
Mutual labels:  caching, cache
Memento
Memento is a development-only tool that caches HTTP calls once they have been executed.
Stars: ✭ 380 (+1552.17%)
Mutual labels:  cache, caching
Scrapbook
PHP cache library, with adapters for e.g. Memcached, Redis, Couchbase, APC(u), SQL and additional capabilities (e.g. transactions, stampede protection) built on top.
Stars: ✭ 279 (+1113.04%)
Mutual labels:  cache, caching
Ccache.cmake
🚅 Compile faster with Ccache! A Ccache integration for CMake with Xcode support.
Stars: ✭ 24 (+4.35%)
Mutual labels:  caching, cache
Libmc
Fast and light-weight memcached client for C++ / #python / #golang #libmc
Stars: ✭ 429 (+1765.22%)
Mutual labels:  cache, caching
salad
Asynchronous Scala Redis Client supporting Sentinel and Redis Cluster
Stars: ✭ 14 (-39.13%)
Mutual labels:  caching, cache
Cachier
Persistent, stale-free, local and cross-machine caching for Python functions.
Stars: ✭ 359 (+1460.87%)
Mutual labels:  cache, caching
Simple Spring Memcached
A drop-in library to enable memcached caching in Spring beans via annotations
Stars: ✭ 185 (+704.35%)
Mutual labels:  cache, caching
Lada Cache
A Redis based, fully automated and scalable database cache layer for Laravel
Stars: ✭ 424 (+1743.48%)
Mutual labels:  cache, caching
Stackexchange.redis.extensions
Stars: ✭ 419 (+1721.74%)
Mutual labels:  cache, caching
HAProxy-2-RPM-builder
Build latest HAProxy binary with prometheus metrics support
Stars: ✭ 28 (+21.74%)
Mutual labels:  caching, cache
Cached
Rust cache structures and easy function memoization
Stars: ✭ 530 (+2204.35%)
Mutual labels:  cache, caching
punic
Punic is a remote cache CLI built for Carthage and Apple .xcframework
Stars: ✭ 25 (+8.7%)
Mutual labels:  caching, cache
transitory
In-memory cache with high hit rates via LFU eviction for Node and browsers. Supports time-based expiration, automatic loading and metrics.
Stars: ✭ 24 (+4.35%)
Mutual labels:  caching, cache
Scaffeine
Thin Scala wrapper for Caffeine (https://github.com/ben-manes/caffeine)
Stars: ✭ 195 (+747.83%)
Mutual labels:  cache, caching
Cache
The Cache component provides an extended PSR-6 implementation for adding cache to your applications.
Stars: ✭ 3,606 (+15578.26%)
Mutual labels:  cache, caching
Cachemanager
CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.
Stars: ✭ 2,049 (+8808.7%)
Mutual labels:  cache, caching
Haproxy
HAProxy Load Balancer's development branch (mirror of git.haproxy.org)
Stars: ✭ 2,463 (+10608.7%)
Mutual labels:  cache, caching
Wp Rocket
Performance optimization plugin for WordPress
Stars: ✭ 394 (+1613.04%)
Mutual labels:  cache, caching
Bigcache
Efficient cache for gigabytes of data written in Go.
Stars: ✭ 5,304 (+22960.87%)
Mutual labels:  cache, caching

Silex Cache Service Provider

This service provider for Silex uses the Doctrine Cache library to provide a cache service to a Silex application as well as to other service providers.

Install

Install with composer:

% composer require chh/cache-service-provider

Usage

Configuration

For an application wide cache use the default cache by setting the default key in cache.options with the cache definition.

The cache definition is an array of options, with driver being the only mandatory one. All other options in the array, are treated as constructor arguments to the driver class.

The cache named default is the cache available through the container's cache service.

<?php

use Doctrine\Common\Cache\ApcuCache;

$app = new Silex\Application;

$app->register(new \CHH\Silex\CacheServiceProvider, [
    'cache.options' => [
        'default' => [
            'driver' => ApcuCache::class,
        ],
    ],
]);

The driver name can be one of the following:

  • A fully qualified class name of a class which implements the Doctrine\Common\Cache\Cache interface
  • An alias like "apc", which then gets translated to \Doctrine\Common\Cache\ApcCache.
  • A Closure, which returns an object implementing \Doctrine\Common\Cache\Cache.

This cache is then available through the cache service, and provides an instance of Doctrine\Common\Cache\Cache:

if ($app['cache']->contains('foo')) {
    echo $app['cache']->fetch('foo'), "<br>";
} else {
    $app['cache']->save('foo', 'bar');
}

To configure multiple caches, define them as additional keys in cache.options:

$app->register(new \CHH\Silex\CacheServiceProvider, [
    'cache.options' => [
        'default' => ['driver' => ApcuCache::class],
        'file' => [
            'driver' => 'filesystem',
            'directory' => '/tmp/myapp',
        ],
        'global' => [
            'driver' => function () use ($app) {
                $redis = new \Doctrine\Common\Cache\RedisCache;
                $redis->setRedis($app['redis']);

                return $redis;
            },
        ],
    ],
]);

All caches (including the default) are then available as a key of the caches service:

$app['caches']['file']->save('foo', 'bar');
$app['caches']['default']->save('bar', 'baz');

Usage from within extensions

Extensions should make no assumptions about their environment. Therefore it's best to use the application's default cache most of the time. But when you do need a cache with a specific driver, then you can use the cache.factory service.

This factory takes an array of cache options, just like in each key of cache.options, and returns a Factory suitable for Pimple.

<?php

$app['caches']['myext'] = $app['cache.factory']([
    'driver' => 'filesystem',
    'directory' => sys_get_temp_dir() . '/myext',
]);

Extensions should prefix their cache keys to avoid conflicts with user specified cache IDs.

To make this easier the Cache Service Provider ships with a CacheNamespace class. This class decorates any \Doctrine\Common\Cache\Cache, and prefixes the keys on all cache operations.

For caches with builtin namespacing support via a setNamespace method, there's also a namespace configuration option.

<?php

use Pimple\Container;
use Pimple\ServiceProviderInterface;
use CHH\Silex\CacheServiceProvider\CacheNamespace;

class ExampleServiceProvider extends ServiceProviderInterface
{
    function register(Container $app)
    {
        // Check if Cache Service Provider is registered:
        if (isset($app['caches'])) {
            $app['caches'] = $app->extend(function ($caches) use ($app) {
                // Use a CacheNamespace to safely add keys to the default
                // cache.
                $caches['example'] = function () use ($caches) {
                    return new CacheNamespace('example', $caches['default']);
                };

                return $caches;
            });
        }
    }
}

This library also provides the cache.namespace factory, which returns a Closure suitable for assigning directly to a Pimple container. Using this, the above code can be further simplified:

// Check if Cache Service Provider is registered:
if (isset($app['caches'])) {
    $app['caches'] = $app->extend(function ($caches) use ($app) {
        // Use a CacheNamespace to safely add keys to the default cache
        $caches['example'] = $app['cache.namespace']('example');

        return $caches;
    });
}

License

Copyright (c) 2016 Christoph Hochstrasser

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE\

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].