mirror of
https://github.com/Mactronique/phpcache.git
synced 2026-03-24 17:02:12 +01:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c4f7f44713 | ||
|
|
6bb469a411 | ||
|
|
7e0c217f67 | ||
|
|
dfba607dbd | ||
|
|
c566f0cbc7 | ||
|
|
cfc3e57edf | ||
|
|
d68ca1ef26 | ||
|
|
4200e8ddbf | ||
|
|
3c2349d4a7 | ||
|
|
8b6b4bfb8a | ||
|
|
def5cdc24c |
97
README.md
97
README.md
@@ -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');
|
||||
[...]
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Mactronique\PhpCache\Exception;
|
||||
|
||||
class AlreadyRegistredDriverException extends DriverRequirementFailException
|
||||
{
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user