22 Commits

Author SHA1 Message Date
Jean-Baptiste Nahan
f675b04591 Update composer.json 2019-04-02 09:16:38 +02:00
Jean-Baptiste Nahan
68072f9008 Update composer.json 2016-11-09 21:05:18 +01:00
Jean-Baptiste Nahan
ddf7b341da Fix #2 2016-09-01 13:29:25 +02:00
macintoshplus
e2de4cdd23 Add auth for redis 2015-10-12 13:42:45 +02:00
macintoshplus
93ac5d6644 fix type 2015-10-08 10:19:44 +02:00
macintoshplus
f2bdb0af19 fix set 2015-09-24 14:01:05 +02:00
macintoshplus
9c64d7ab2b fix set 2015-09-24 13:51:26 +02:00
macintoshplus
e0077001d3 fix exist 2015-09-24 09:53:11 +02:00
macintoshplus
25367c1296 fix class name 2015-09-24 09:39:56 +02:00
Jean-Baptiste Nahan
60571a6616 Update README.md 2015-08-05 22:09:43 +02:00
Jean-Baptiste Nahan
5f1d8143e9 Update README.md 2015-08-05 21:49:41 +02:00
Jean-Baptiste Nahan
c4f7f44713 Update composer.json 2015-07-28 22:48:08 +02:00
macintoshplus
6bb469a411 fix test erroe 2015-07-24 17:16:38 +02:00
macintoshplus
7e0c217f67 Add exception if register twice same driver 2015-07-24 16:58:20 +02:00
macintoshplus
dfba607dbd doc 2015-07-23 20:49:45 +02:00
macintoshplus
c566f0cbc7 doc 2015-07-23 20:48:09 +02:00
macintoshplus
cfc3e57edf doc 2015-07-23 18:44:11 +02:00
macintoshplus
d68ca1ef26 doc 2015-07-23 18:36:24 +02:00
macintoshplus
4200e8ddbf add config for many server 2015-07-23 18:16:18 +02:00
macintoshplus
3c2349d4a7 add config example 2015-07-23 18:09:39 +02:00
macintoshplus
8b6b4bfb8a add config example 2015-07-23 18:03:21 +02:00
macintoshplus
def5cdc24c add config example 2015-07-23 18:00:23 +02:00
7 changed files with 169 additions and 33 deletions

101
README.md
View File

@@ -1,6 +1,10 @@
# phpcache
[![Build Status](https://travis-ci.org/Mactronique/phpcache.svg?branch=master)](https://travis-ci.org/Mactronique/phpcache)
[![Dependency Status](https://www.versioneye.com/user/projects/55c2676e653762001a00287f/badge.svg?style=flat)](https://www.versioneye.com/user/projects/55c2676e653762001a00287f)
[![Latest Stable Version](https://poser.pugx.org/mactronique/phpcache/v/stable)](https://packagist.org/packages/mactronique/phpcache)
[![Latest Unstable Version](https://poser.pugx.org/mactronique/phpcache/v/unstable)](https://packagist.org/packages/mactronique/phpcache)
[![License](https://poser.pugx.org/mactronique/phpcache/license)](https://packagist.org/packages/mactronique/phpcache)
## Supported driver
@@ -8,10 +12,105 @@
- WinCache
- Predis
- Redis
- Memcached
- Null (special for instanciate no effect cache driver)
## Install
```
``` shell
php composer.phar require mactronique/phpcache "~1.0"
```
## Configuration
### for xCache
No configuration need.
### for WinCache
No configuration need.
### for Null
No configuration need.
### For Predis
``` php
$config = array(
"host" => "127.0.0.1",
"port" => "",
"password" => "",
"database" => "",
"timeout" => 1,
"read_write_timeout" => 1
);
```
Only `host` key is required.
## For Redis
``` php
$config = array(
"host" => "127.0.0.1",
"port" => "",
"password" => "",
"database" => "",
"timeout" => 1
);
```
Only `host` key is required.
## For Redis
``` php
$config = array(
"host" => "127.0.0.1",
"port" => 11211,
"sharing" => 100
);
```
Only `host` key is required.
# Example of use NullDriver
This code is example of service class
``` php
use Mactronique\PhpCache\Service\PhpCache;
use Mactronique\PhpCache\Driver\NullDriver;
class myService
{
private $cache
public function __construct(PhpCache $cache = null)
{
if (null === $cache) {
$cache = new PhpCache();
$cache->registerDriver(new NullDriver());
}
$this->cache = $cache;
}
public function myAction()
{
/*
You can use the cache but never key exist and all get return null.
*/
$val = $this->cache->get('key');
[...]
}
}
```

View File

@@ -6,12 +6,11 @@
"authors": [
{
"name": "Jean-Baptiste Nahan",
"email": "jbnahan@gmail.com"
"email": "macintoshplus@users.noreply.github.com"
}
],
"require": {
"php": ">=5.4",
"jms/serializer": "~0.16"
"php": ">=5.4"
},
"require-dev": {
"atoum/atoum": "master-dev"
@@ -31,5 +30,6 @@
"branch-alias": {
"dev-master": "1.x-dev"
}
}
}
},
"abandoned": "psr/simple-cache"
}

View File

@@ -89,19 +89,32 @@ class MemcachedDriver implements Driver
{
if (null === $this->client) {
$this->client = new Memcached();
//Get config
$configs = $this->config;
$keys = array_keys($configs);
$host = array_key_exists('host', $this->config)? $this->config['host']:'127.0.0.1';
$port = array_key_exists('port', $this->config)? (int)$this->config['port']:11211;
$sharing = (array_key_exists('sharing', $this->config))? (int)$this->config['sharing']:100;
if ($sharing > 0) {
if (!$this->client->addServer($host, $port, $sharing)) {
throw new ServerException("Error Unable to connect to server");
}
} else {
if (!$this->client->addServer($host, $port)) {
throw new ServerException("Error Unable to connect to server");
//If only onserver and if the config is in top level, set in new array
if (!is_int($keys[0])) {
$configs = [$this->config];
}
// Loop for add all server
foreach ($$configs as $key => $value) {
$host = array_key_exists('host', $this->config)? $this->config['host']:'127.0.0.1';
$port = array_key_exists('port', $this->config)? (int)$this->config['port']:11211;
$sharing = (array_key_exists('sharing', $this->config))? (int)$this->config['sharing']:100;
if ($sharing > 0) {
if (!$this->client->addServer($host, $port, $sharing)) {
throw new ServerException("Error Unable to connect to server");
}
} else {
if (!$this->client->addServer($host, $port)) {
throw new ServerException("Error Unable to connect to server");
}
}
}
}
}
}

View File

@@ -21,10 +21,10 @@ class RedisDriver implements Driver
public function checkDriver()
{
if (!class_exists('Redis')) {
throw new DriverRequirementFailException("Redis extension not installed", self::NAME);
throw new DriverRequirementFailException('Redis extension not installed', self::NAME);
}
}
/**
* @return string Driver name
*/
@@ -35,6 +35,7 @@ class RedisDriver implements Driver
/**
* @param string $key
*
* @return mixed
*/
public function get($key)
@@ -43,31 +44,38 @@ class RedisDriver implements Driver
$value = $this->client->get($key);
if ($value == false) {
return null;
return;
}
return $value;
}
/**
* @param string $key
* @param mixed $value
* @param integer $ttl
* @param mixed $value
* @param int $ttl
*
* @return mixed
*/
public function set($key, $value, $ttl = null)
{
$this->connectServer();
return $this->client->set($keyword, $value, (null === $ttl)? 0:$ttl);
$this->client->set($key, $value);
if (null !== $ttl) {
$this->client->setTimeout($key, $ttl);
}
}
/**
* @param string $key
* @return boolean
*
* @return bool
*/
public function exists($key)
{
$this->connectServer();
return (null !== $this->client->exists($key));
return $this->client->exists($key);
}
/**
@@ -76,16 +84,18 @@ class RedisDriver implements Driver
public function remove($key)
{
$this->connectServer();
return $this->client->delete($key);
}
/**
* Remove all keys and value from cache
* Remove all keys and value from cache.
*/
public function clean()
{
$this->connectServer();
$this->client->flushDB();
return true;
}
@@ -93,14 +103,18 @@ class RedisDriver implements Driver
{
if (null === $this->client) {
$host = $this->config['host'];
$port = array_key_exists('port', $this->config)? (int)$this->config['port']:6379;
$password = (array_key_exists('password', $this->config))? $this->config['password']:'';
$database = (array_key_exists('database', $this->config)? (int)$this->config['database']:null);
$timeout = (array_key_exists('timeout', $this->config))? (int)$this->config['timeout']:1;
$port = array_key_exists('port', $this->config) ? (int) $this->config['port'] : 6379;
$password = (array_key_exists('password', $this->config)) ? $this->config['password'] : '';
$database = (array_key_exists('database', $this->config) ? (int) $this->config['database'] : null);
$timeout = (array_key_exists('timeout', $this->config)) ? (int) $this->config['timeout'] : 1;
$this->client = new Redis();
$this->client = new \Redis();
if (!$this->client->connect($host, $port, $timeout)) {
throw new ServerException("Error Unable to connect to server");
throw new ServerException('Error Unable to connect to server');
}
if (!empty($password)) {
$this->client->auth($password);
}
if (null !== $database) {

View File

@@ -0,0 +1,7 @@
<?php
namespace Mactronique\PhpCache\Exception;
class AlreadyRegistredDriverException extends DriverRequirementFailException
{
}

View File

@@ -21,6 +21,9 @@ class PhpCache
public function registerDriver(Driver $driver)
{
if (array_key_exists($driver->getName(), $this->drivers)) {
throw new AlreadyRegistredDriverException("Error this driver is already registred", $driver->getName());
}
$driver->checkDriver();
$this->drivers[$driver->getName()] = $driver;
@@ -48,7 +51,7 @@ class PhpCache
*/
public function set($key, $value, $ttl = null, $driverName = null)
{
return $this->getDriver($driverName)->set($key, $value, $ttl = null);
return $this->getDriver($driverName)->set($key, $value, $ttl);
}
/**

View File

@@ -84,7 +84,7 @@ class PhpCache extends atoum
$this->string($service->get('key', 'test'))->isEqualTo('valeur');
$this->mock($driver)->call('get')->once();
$this->mock($driver)->call('getName')->once();
$this->mock($driver)->call('getName')->twice();
}
public function testSet()