Code Monkey home page Code Monkey logo

caching-object-wrapper's Introduction

๐ŸŽ Caching Object Wrapper

Build Status Coverage Status StyleCI

Wraps up any PHP object so all its methods are cached.

Installation

You can install this package easily via Composer. Just run the following command from the root of your project.

composer require rapidwebltd/caching-object-wrapper

Usage

Here is a very simple usage example, showing how we can easily cache a simple object.

use RapidWeb\CachingObjectWrapper\CachingObjectWrapper;
use rapidweb\RWFileCachePSR6\CacheItemPool;

// This is an example class that we are going to use. It just generates random numbers.
class RandomNumberGenerator
{
    public function generate()
    {
        return rand();
    }
}

// First, we need to install and setup a PSR6 cache item pool. 
// As an example, we'll use the PSR-6 adapter for RW File Cache.

// $ composer require rapidwebltd/rw-file-cache-psr-6

$cache = new CacheItemPool();
$cache->changeConfig(
    [
        'cacheDirectory'  => '/tmp/cow-tests/',
        'gzipCompression' => true,
        ]
    );

// Next, we can wrap up a new RandomNumberGenerator.
// We also pass in the $cache object we just created, and the desired cache expiry time in seconds.

$randomNumberGenerator = new CachingObjectWrapper(new RandomNumberGenerator(), $cache, 60 * 60);

// That's it!
// To test, we can tell the wrapped object to generate us two random numbers.

$randomNumber1 = $randomNumberGenerator->generate();
$randomNumber2 = $randomNumberGenerator->generate();

// Due to our caching, $randomNumber1 and $randomNumber2 should be identical.

caching-object-wrapper's People

Contributors

divineomega avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  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.