mirror of
https://github.com/Mactronique/phpcache.git
synced 2026-03-24 17:02:12 +01:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f675b04591 | ||
|
|
68072f9008 | ||
|
|
ddf7b341da | ||
|
|
e2de4cdd23 | ||
|
|
93ac5d6644 | ||
|
|
f2bdb0af19 | ||
|
|
9c64d7ab2b | ||
|
|
e0077001d3 | ||
|
|
25367c1296 | ||
|
|
60571a6616 | ||
|
|
5f1d8143e9 | ||
|
|
c4f7f44713 | ||
|
|
6bb469a411 | ||
|
|
7e0c217f67 | ||
|
|
dfba607dbd | ||
|
|
c566f0cbc7 | ||
|
|
cfc3e57edf | ||
|
|
d68ca1ef26 | ||
|
|
4200e8ddbf | ||
|
|
3c2349d4a7 | ||
|
|
8b6b4bfb8a | ||
|
|
def5cdc24c |
101
README.md
101
README.md
@@ -1,6 +1,10 @@
|
||||
# phpcache
|
||||
|
||||
[](https://travis-ci.org/Mactronique/phpcache)
|
||||
[](https://www.versioneye.com/user/projects/55c2676e653762001a00287f)
|
||||
[](https://packagist.org/packages/mactronique/phpcache)
|
||||
[](https://packagist.org/packages/mactronique/phpcache)
|
||||
[](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');
|
||||
[...]
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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