GithubHelp home page GithubHelp logo

cache's Introduction

Cache

Build Status SensioLabsInsight Coverage Status

Cache adds features to Doctrine Cache implementation

  • Default lifetime
  • Fetch with a namespace
  • Save with a namespace
  • Cache invalidation through namespace strategy
  • CacheProvider Builder

Installation

The easiest way to install Cache is via composer.

Create the following composer.json file and run the php composer.phar install command to install it.

{
    "require": {
        "openclassrooms/cache": "*"
    }
}
<?php
require 'vendor/autoload.php';

use OpenClassrooms\Cache\Cache\Cache;

//do things

Instantiation

OC Cache needs a Doctrine CacheProvider to be instantiate.

$cacheProvider = new ArrayCache();

$cache = new Cache($cacheProvider);

A Cache builder can be used.

// Default builder, build a cache using ArrayCache Provider
$cache = new CacheBuilderImpl()->build();

// Using a CacheProvider
$cache = new CacheBuilderImpl()
    ->withCacheProvider($redisCache)
    ->build();

// Optional default lifetime
$cache = new CacheBuilderImpl()
    ->withCacheProvider($redisCache)
    ->withDefaultLifetime(300)
    ->build();

Default lifetime

$cache->setDefaultLifetime(300);
$cache->save($id, $data);

Fetch with namespace

$data = $cache->fetchWithNamespace($id, $namespaceId);

Save with namespace

// Namespace and life time can be null
$data = $cache->saveWithNamespace($id, $data, $namespaceId, $lifeTime);

Cache invalidation

$cache->invalidate($namespaceId);

CacheProvider Builder

The library provides a CacheProvider Builder

// Memcache
$cacheProvider = new CacheProviderBuilderImpl()
    ->create(CacheProviderType::MEMCACHE)
    ->withHost('127.0.0.1')
    ->withPort(11211) // Default 11211
    ->withTimeout(1) // Default 1
    ->build();

// Memcached
$cacheProvider = new CacheProviderBuilderImpl()
    ->create(CacheProviderType::MEMCACHED)
    ->withHost('127.0.0.1')
    ->withPort(11211) // Default 11211
    ->build();

// Redis
$cacheProvider = new CacheProviderBuilderImpl()
    ->create(CacheProviderType::REDIS)
    ->withHost('127.0.0.1')
    ->withPort(6379) // Default 6379
    ->withTimeout(0.0) // Default 0.0
    ->build();

// Array
$cacheProvider = new CacheProviderBuilderImpl()
    ->create(CacheProviderType::ARRAY_CACHE)
    ->build();

cache's People

Contributors

romainkuzniak avatar arnaud-23 avatar openclassrooms-admin avatar

Watchers

James Cloos 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.