Giter VIP home page Giter VIP logo

cakephp-simplecache-bridge's Introduction

CakePHP SimpleCache Bridge

A bridge to convert CakePHP Cache to SimpleCache (PSR16)

Build Status Codacy Badge Maintainability Test Coverage License Minimum PHP Version

Why build this?

In a few of my CakePHP apps I make use of 3rd party libraries that require a PSR-16 compatible cache engine. This bridge allows me to reuse the Cache Engines and Cache configs already available within my CakePHP application and eliminates the need of having to build 2 different sets of Cache management libraries.

Isn't this already in CakePHP 3.7 ?

No. CakePHP 3.7 brings with it a PSR-16 CacheEngine; meaning you will be able to decorate a PSR-16 object to implement Cake\Cache\CacheEngine methods. What this bridge offers is the ability to go from a Cake\Cache\CacheEngine to PSR-16, not the other way around.

The new SimpleCacheEngine class ( in CakePHP 3.7 ) implements PSR 16 interface and decorates/wraps CacheEngine classes.

Thanks Admad for the clarification.

Examples

Cache::config('short', [
    'className' => 'File',
    'duration' => '+1 hours',
    'path' => CACHE,
    'prefix' => 'cake_short_'
]);

$cache = new Bridge('short');

//setting cache data
$cache->set('some_key', 'some value');

//getting cache data with a default
$value = $cache->get('some_key', 'some default');

//overriding the Cache engine's TTL with an int
$cache->set('some_key', 'some value', 300); //cached for 300 seconds instead of `+1 hours`

//overriding the Cache engine's TTL with a `\DateTimeInterval` ( as per the SimpleCache Interface )
$interval = new \DateTimeInterval('P1Y'); // an interval of 1 year
$cache->set('some_key', 'some value', $interval); //cached for 1 year instead of `+1 hours`

Requirements

  • PHP >=5.6
  • CakePHP >=3.0

Installation

You can install this library using composer.

The recommended way to install as a composer package:

composer require angelxmoreno/cakephp-simplecache-bridge

Setup

Once you have a cahe configuration defined, you simple have to pass the config name when creating an instance of the Bridge like so:

Cache::config('short', [
    'className' => 'File',
    'duration' => '+1 hours',
    'path' => CACHE,
    'prefix' => 'cake_short_'
]);

$cache = new Bridge('short');

Reporting Issues

If you have a problem with this library please open an issue on GitHub.

License

This code is offered under an MIT license.

cakephp-simplecache-bridge's People

Contributors

angelxmoreno avatar

Watchers

 avatar  avatar  avatar

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.