11 Commits
1.0.0 ... 1.0.1

Author SHA1 Message Date
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
6 changed files with 132 additions and 14 deletions

View File

@@ -8,10 +8,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

@@ -11,7 +11,7 @@
],
"require": {
"php": ">=5.4",
"jms/serializer": "~0.16"
"jms/serializer": "~1.0"
},
"require-dev": {
"atoum/atoum": "master-dev"
@@ -32,4 +32,4 @@
"dev-master": "1.x-dev"
}
}
}
}

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

@@ -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;

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()