Skip to content

Commit

Permalink
Add "unix_socket" setting for Reids, MongoDB, Memcache and Memcached …
Browse files Browse the repository at this point in the history
…drivers.
  • Loading branch information
terrylinooo committed Nov 12, 2020
1 parent 90a3657 commit 15fd0e8
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 26 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ The required parameters are marked by an asterisk (*)
| Driver name | `($driver)`| PHP modules | `($config)`
| --- | --- | --- | --- |
| File | `file` | - | `*storage` |
| Redis | `redis` | redis | `host`, `port`, `user`, `pass` |
| MongoDB | `mongo` | mongodb | `host`, `port`, `user`, `pass`, `dbname`, `collection` |
| Redis | `redis` | redis | `host`, `port`, `user`, `pass`, `unix_socket` |
| MongoDB | `mongo` | mongodb | `host`, `port`, `user`, `pass`, `dbname`, `collection`, `unix_socket` |
| MySQL | `mysql` | pdo_mysql | `host`, `port`, `*user`, `*pass`, `*dbname`, `table`, `charset` |
| SQLite | `sqlite` | pdo_sqlite | `*storage` |
| APC | `apc` | apc | - |
| APCu | `apcu` | apcu | - |
| Memcache | `memcache` | memcache | `host`, `port` |
| LibMemcached | `memcached` | memcached | `host`, `port` |
| Memcache | `memcache` | memcache | `host`, `port`, `unix_socket` |
| LibMemcached | `memcached` | memcached | `host`, `port`, `unix_socket` |
| WinCache | `wincache` | wincache | - |

Note: **WinCache** is excluded from unit testing since it's only used on Windows, and the testing processes are done on Linux environment.
Note:

- **WinCache** is excluded from unit testing since it's only used on Windows, and the testing processes are done on Linux environment.
- `unix_socket` is empty by default, accepting the absolute path of the unix domain socket file. If it is set, `host` and `port` will be ignored.

This command will show a list of the installed PHP modules.

Expand Down
10 changes: 8 additions & 2 deletions src/SimpleCache/AssertTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,21 @@ protected function assertDirectoryWritable(string $directory): void
{
if (!is_dir($directory)) {
throw new CacheException(
'The directory of the storage does not exist. (' . $directory . ')'
sprintf(
'The directory of the storage does not exist. ( %s )',
$directory
)
);
}

// @codeCoverageIgnoreStart

if (!is_writable($directory)) {
throw new CacheException(
'The directory of the storage must be wriable. (' . $directory . ')'
sprintf(
'The directory of the storage must be wriable. ( %s )',
$directory
)
);
}

Expand Down
26 changes: 20 additions & 6 deletions src/SimpleCache/Driver/Memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function __construct(array $setting = [])
$config = [
'host' => '127.0.0.1',
'port' => 11211,

// If the UNIX socket is set, host and port will be ignored.
'unix_socket' => '',
];

foreach (array_keys($config) as $key) {
Expand All @@ -69,12 +72,23 @@ protected function connect(array $config): void
if (extension_loaded('memcache')) {
try {
$this->memcache = new MemcacheServer();
$this->memcache->addServer(
$config['host'],
$config['port'],
true,
1
);

if (!empty($config['unix_socket'])) {
// @codeCoverageIgnoreStart
$this->memcache->addServer(
'unix://' . $config['unix_socket'],
0
);
// @codeCoverageIgnoreEnd
} else {
$this->memcache->addServer(
$config['host'],
$config['port'],
true,
1
);
}

// @codeCoverageIgnoreStart
} catch (Exception $e) {
throw new CacheException($e->getMessage());
Expand Down
24 changes: 19 additions & 5 deletions src/SimpleCache/Driver/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function __construct(array $setting = [])
$config = [
'host' => '127.0.0.1',
'port' => 11211,

// If the UNIX socket is set, host and port will be ignored.
'unix_socket' => '',
];

foreach (array_keys($config) as $key) {
Expand All @@ -69,11 +72,22 @@ protected function connect(array $config): void
if (extension_loaded('memcached')) {
try {
$this->memcached = new MemcachedServer();
$this->memcached->addServer(
$config['host'],
$config['port'],
1
);

if (!empty($config['unix_socket'])) {
// @codeCoverageIgnoreStart
$this->memcached->addServer(
$config['unix_socket'],
0
);
// @codeCoverageIgnoreEnd
} else {
$this->memcached->addServer(
$config['host'],
$config['port'],
1
);
}

// @codeCoverageIgnoreStart
} catch (Exception $e) {
throw new CacheException($e->getMessage());
Expand Down
18 changes: 14 additions & 4 deletions src/SimpleCache/Driver/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public function __construct(array $setting = [])
'pass' => null,
'dbname' => 'test',
'collection' => 'cache_data',

// If the UNIX socket is set, host, port will be ignored.
'unix_socket' => '',
];

foreach (array_keys($config) as $key) {
Expand Down Expand Up @@ -102,6 +105,7 @@ protected function connect(array $config): void
{
if (extension_loaded('mongodb')) {
try {

$auth = '';
$dababase = '';

Expand All @@ -116,10 +120,16 @@ protected function connect(array $config): void
$dababase = '/' . $config['dbname'];
}

// Basic => mongodb://127.0.0.1:27017
// mongodb://user:[email protected]:27017/dbname
$command = 'mongodb://' . $auth . $config['host'] . ':' . $config['port'] . $dababase;

if (!empty($config['unix_socket'])) {
// @codeCoverageIgnoreStart
$command = 'mongodb://' . $auth . rawurlencode($config['unix_socket']) . $dababase;
// @codeCoverageIgnoreEnd
} else {
// Basic => mongodb://127.0.0.1:27017
// mongodb://user:[email protected]:27017/dbname
$command = 'mongodb://' . $auth . $config['host'] . ':' . $config['port'] . $dababase;
}

$this->mongo = new MongoServer($command);
$this->concern = new WriteConcern(WriteConcern::MAJORITY, 1000);

Expand Down
14 changes: 12 additions & 2 deletions src/SimpleCache/Driver/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public function __construct(array $setting = [])
'port' => 6379,
'user' => null,
'pass' => null,

// If the UNIX socket is set, host, port, user and pass will be ignored.
'unix_socket' => '',
];

foreach (array_keys($config) as $key) {
Expand All @@ -73,8 +76,15 @@ protected function connect(array $config): void
if (extension_loaded('redis')) {
try {
$this->redis = new RedisServer();
$this->redis->connect($config['host'], $config['port']);
$this->auth($config);

if (!empty($config['unix_socket'])) {
// @codeCoverageIgnoreStart
$this->redis->connect($config['unix_socket']);
// @codeCoverageIgnoreEnd
} else {
$this->redis->connect($config['host'], $config['port']);
$this->auth($config);
}

// @codeCoverageIgnoreStart
} catch (Exception $e) {
Expand Down
21 changes: 20 additions & 1 deletion tests/SimpleCache/Driver/MemcacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getCacheDriver()
{
$cache = new Memcache([
'host' => '127.0.0.1',
'port' => 11211,
'port' => 11211,
]);

return $cache;
Expand All @@ -36,4 +36,23 @@ public function testCacheDriver()
$driver = $this->getCacheDriver();
$this->assertTrue($driver instanceof CacheInterface);
}

public function testConnectWithUnixSocket()
{
$unixSocketFilePath = '/var/run/memcached/memcached.sock';

if (file_exists($unixSocketFilePath)) {
$cache = new Memcache([
'unix_socket' => $unixSocketFilePath,
]);

$cache->set('memcache_socket', 'good');
$this->assertSame('good', $cache->get('memcache_socket'));
} else {
$this->console(sprintf(
'Ingore testing with unix domain socket because that file "%s" does not exist.',
$unixSocketFilePath
));
}
}
}
21 changes: 20 additions & 1 deletion tests/SimpleCache/Driver/MemcachedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getCacheDriver()
{
$cache = new Memcached([
'host' => '127.0.0.1',
'port' => 11211,
'port' => 11211,
]);

return $cache;
Expand All @@ -36,4 +36,23 @@ public function testCacheDriver()
$driver = $this->getCacheDriver();
$this->assertTrue($driver instanceof CacheInterface);
}

public function testConnectWithUnixSocket()
{
$unixSocketFilePath = '/var/run/memcached/memcached.sock';

if (file_exists($unixSocketFilePath)) {
$cache = new Memcached([
'unix_socket' => $unixSocketFilePath,
]);

$cache->set('memcached_socket', 'good');
$this->assertSame('good', $cache->get('memcached_socket'));
} else {
$this->console(sprintf(
'Ingore testing with unix domain socket because that file "%s" does not exist.',
$unixSocketFilePath
));
}
}
}
19 changes: 19 additions & 0 deletions tests/SimpleCache/Driver/MongoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,23 @@ public function testGetAll()
$this->assertSame($items['foo9']['value'], 'bar9');
$this->assertSame($items['foo10']['value'], 'bar10');
}

public function testConnectWithUnixSocket()
{
$unixSocketFilePath = '/var/run/mongodb/mongodb.sock';

if (file_exists($unixSocketFilePath)) {
$cache = new Mongo([
'unix_socket' => $unixSocketFilePath,
]);

$cache->set('mongodb_socket', 'good');
$this->assertSame('good', $cache->get('mongodb_socket'));
} else {
$this->console(sprintf(
'Ingore testing with unix domain socket because that file "%s" does not exist.',
$unixSocketFilePath
));
}
}
}
19 changes: 19 additions & 0 deletions tests/SimpleCache/Driver/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,23 @@ public function testInvalidUsernameAndPassword()

// Nothing happended??
}

public function testConnectWithUnixSocket()
{
$unixSocketFilePath = '/var/run/redis/redis.sock';

if (file_exists($unixSocketFilePath)) {
$cache = new Redis([
'unix_socket' => $unixSocketFilePath,
]);

$cache->set('redis_socket', 'good');
$this->assertSame('good', $cache->get('redis_socket'));
} else {
$this->console(sprintf(
'Ingore testing with unix domain socket because that file "%s" does not exist.',
$unixSocketFilePath
));
}
}
}
1 change: 1 addition & 0 deletions tests/redis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
php ../vendor/phpunit/phpunit/phpunit --configuration ../phpunit.xml --filter RedisTest ../tests/SimpleCache/Driver/RedisTest.php

0 comments on commit 15fd0e8

Please sign in to comment.