Skip to content

Commit 15fd0e8

Browse files
committed
Add "unix_socket" setting for Reids, MongoDB, Memcache and Memcached drivers.
1 parent 90a3657 commit 15fd0e8

File tree

11 files changed

+160
-26
lines changed

11 files changed

+160
-26
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@ The required parameters are marked by an asterisk (*)
1717
| Driver name | `($driver)`| PHP modules | `($config)`
1818
| --- | --- | --- | --- |
1919
| File | `file` | - | `*storage` |
20-
| Redis | `redis` | redis | `host`, `port`, `user`, `pass` |
21-
| MongoDB | `mongo` | mongodb | `host`, `port`, `user`, `pass`, `dbname`, `collection` |
20+
| Redis | `redis` | redis | `host`, `port`, `user`, `pass`, `unix_socket` |
21+
| MongoDB | `mongo` | mongodb | `host`, `port`, `user`, `pass`, `dbname`, `collection`, `unix_socket` |
2222
| MySQL | `mysql` | pdo_mysql | `host`, `port`, `*user`, `*pass`, `*dbname`, `table`, `charset` |
2323
| SQLite | `sqlite` | pdo_sqlite | `*storage` |
2424
| APC | `apc` | apc | - |
2525
| APCu | `apcu` | apcu | - |
26-
| Memcache | `memcache` | memcache | `host`, `port` |
27-
| LibMemcached | `memcached` | memcached | `host`, `port` |
26+
| Memcache | `memcache` | memcache | `host`, `port`, `unix_socket` |
27+
| LibMemcached | `memcached` | memcached | `host`, `port`, `unix_socket` |
2828
| WinCache | `wincache` | wincache | - |
2929

30-
Note: **WinCache** is excluded from unit testing since it's only used on Windows, and the testing processes are done on Linux environment.
30+
Note:
31+
32+
- **WinCache** is excluded from unit testing since it's only used on Windows, and the testing processes are done on Linux environment.
33+
- `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.
3134

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

src/SimpleCache/AssertTrait.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,21 @@ protected function assertDirectoryWritable(string $directory): void
108108
{
109109
if (!is_dir($directory)) {
110110
throw new CacheException(
111-
'The directory of the storage does not exist. (' . $directory . ')'
111+
sprintf(
112+
'The directory of the storage does not exist. ( %s )',
113+
$directory
114+
)
112115
);
113116
}
114117

115118
// @codeCoverageIgnoreStart
116119

117120
if (!is_writable($directory)) {
118121
throw new CacheException(
119-
'The directory of the storage must be wriable. (' . $directory . ')'
122+
sprintf(
123+
'The directory of the storage must be wriable. ( %s )',
124+
$directory
125+
)
120126
);
121127
}
122128

src/SimpleCache/Driver/Memcache.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public function __construct(array $setting = [])
4444
$config = [
4545
'host' => '127.0.0.1',
4646
'port' => 11211,
47+
48+
// If the UNIX socket is set, host and port will be ignored.
49+
'unix_socket' => '',
4750
];
4851

4952
foreach (array_keys($config) as $key) {
@@ -69,12 +72,23 @@ protected function connect(array $config): void
6972
if (extension_loaded('memcache')) {
7073
try {
7174
$this->memcache = new MemcacheServer();
72-
$this->memcache->addServer(
73-
$config['host'],
74-
$config['port'],
75-
true,
76-
1
77-
);
75+
76+
if (!empty($config['unix_socket'])) {
77+
// @codeCoverageIgnoreStart
78+
$this->memcache->addServer(
79+
'unix://' . $config['unix_socket'],
80+
0
81+
);
82+
// @codeCoverageIgnoreEnd
83+
} else {
84+
$this->memcache->addServer(
85+
$config['host'],
86+
$config['port'],
87+
true,
88+
1
89+
);
90+
}
91+
7892
// @codeCoverageIgnoreStart
7993
} catch (Exception $e) {
8094
throw new CacheException($e->getMessage());

src/SimpleCache/Driver/Memcached.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public function __construct(array $setting = [])
4444
$config = [
4545
'host' => '127.0.0.1',
4646
'port' => 11211,
47+
48+
// If the UNIX socket is set, host and port will be ignored.
49+
'unix_socket' => '',
4750
];
4851

4952
foreach (array_keys($config) as $key) {
@@ -69,11 +72,22 @@ protected function connect(array $config): void
6972
if (extension_loaded('memcached')) {
7073
try {
7174
$this->memcached = new MemcachedServer();
72-
$this->memcached->addServer(
73-
$config['host'],
74-
$config['port'],
75-
1
76-
);
75+
76+
if (!empty($config['unix_socket'])) {
77+
// @codeCoverageIgnoreStart
78+
$this->memcached->addServer(
79+
$config['unix_socket'],
80+
0
81+
);
82+
// @codeCoverageIgnoreEnd
83+
} else {
84+
$this->memcached->addServer(
85+
$config['host'],
86+
$config['port'],
87+
1
88+
);
89+
}
90+
7791
// @codeCoverageIgnoreStart
7892
} catch (Exception $e) {
7993
throw new CacheException($e->getMessage());

src/SimpleCache/Driver/Mongo.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public function __construct(array $setting = [])
7575
'pass' => null,
7676
'dbname' => 'test',
7777
'collection' => 'cache_data',
78+
79+
// If the UNIX socket is set, host, port will be ignored.
80+
'unix_socket' => '',
7881
];
7982

8083
foreach (array_keys($config) as $key) {
@@ -102,6 +105,7 @@ protected function connect(array $config): void
102105
{
103106
if (extension_loaded('mongodb')) {
104107
try {
108+
105109
$auth = '';
106110
$dababase = '';
107111

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

119-
// Basic => mongodb://127.0.0.1:27017
120-
// mongodb://user:pass@127.0.0.1:27017/dbname
121-
$command = 'mongodb://' . $auth . $config['host'] . ':' . $config['port'] . $dababase;
122-
123+
if (!empty($config['unix_socket'])) {
124+
// @codeCoverageIgnoreStart
125+
$command = 'mongodb://' . $auth . rawurlencode($config['unix_socket']) . $dababase;
126+
// @codeCoverageIgnoreEnd
127+
} else {
128+
// Basic => mongodb://127.0.0.1:27017
129+
// mongodb://user:pass@127.0.0.1:27017/dbname
130+
$command = 'mongodb://' . $auth . $config['host'] . ':' . $config['port'] . $dababase;
131+
}
132+
123133
$this->mongo = new MongoServer($command);
124134
$this->concern = new WriteConcern(WriteConcern::MAJORITY, 1000);
125135

src/SimpleCache/Driver/Redis.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public function __construct(array $setting = [])
4848
'port' => 6379,
4949
'user' => null,
5050
'pass' => null,
51+
52+
// If the UNIX socket is set, host, port, user and pass will be ignored.
53+
'unix_socket' => '',
5154
];
5255

5356
foreach (array_keys($config) as $key) {
@@ -73,8 +76,15 @@ protected function connect(array $config): void
7376
if (extension_loaded('redis')) {
7477
try {
7578
$this->redis = new RedisServer();
76-
$this->redis->connect($config['host'], $config['port']);
77-
$this->auth($config);
79+
80+
if (!empty($config['unix_socket'])) {
81+
// @codeCoverageIgnoreStart
82+
$this->redis->connect($config['unix_socket']);
83+
// @codeCoverageIgnoreEnd
84+
} else {
85+
$this->redis->connect($config['host'], $config['port']);
86+
$this->auth($config);
87+
}
7888

7989
// @codeCoverageIgnoreStart
8090
} catch (Exception $e) {

tests/SimpleCache/Driver/MemcacheTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function getCacheDriver()
2020
{
2121
$cache = new Memcache([
2222
'host' => '127.0.0.1',
23-
'port' => 11211,
23+
'port' => 11211,
2424
]);
2525

2626
return $cache;
@@ -36,4 +36,23 @@ public function testCacheDriver()
3636
$driver = $this->getCacheDriver();
3737
$this->assertTrue($driver instanceof CacheInterface);
3838
}
39+
40+
public function testConnectWithUnixSocket()
41+
{
42+
$unixSocketFilePath = '/var/run/memcached/memcached.sock';
43+
44+
if (file_exists($unixSocketFilePath)) {
45+
$cache = new Memcache([
46+
'unix_socket' => $unixSocketFilePath,
47+
]);
48+
49+
$cache->set('memcache_socket', 'good');
50+
$this->assertSame('good', $cache->get('memcache_socket'));
51+
} else {
52+
$this->console(sprintf(
53+
'Ingore testing with unix domain socket because that file "%s" does not exist.',
54+
$unixSocketFilePath
55+
));
56+
}
57+
}
3958
}

tests/SimpleCache/Driver/MemcachedTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function getCacheDriver()
2020
{
2121
$cache = new Memcached([
2222
'host' => '127.0.0.1',
23-
'port' => 11211,
23+
'port' => 11211,
2424
]);
2525

2626
return $cache;
@@ -36,4 +36,23 @@ public function testCacheDriver()
3636
$driver = $this->getCacheDriver();
3737
$this->assertTrue($driver instanceof CacheInterface);
3838
}
39+
40+
public function testConnectWithUnixSocket()
41+
{
42+
$unixSocketFilePath = '/var/run/memcached/memcached.sock';
43+
44+
if (file_exists($unixSocketFilePath)) {
45+
$cache = new Memcached([
46+
'unix_socket' => $unixSocketFilePath,
47+
]);
48+
49+
$cache->set('memcached_socket', 'good');
50+
$this->assertSame('good', $cache->get('memcached_socket'));
51+
} else {
52+
$this->console(sprintf(
53+
'Ingore testing with unix domain socket because that file "%s" does not exist.',
54+
$unixSocketFilePath
55+
));
56+
}
57+
}
3958
}

tests/SimpleCache/Driver/MongoTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,23 @@ public function testGetAll()
6868
$this->assertSame($items['foo9']['value'], 'bar9');
6969
$this->assertSame($items['foo10']['value'], 'bar10');
7070
}
71+
72+
public function testConnectWithUnixSocket()
73+
{
74+
$unixSocketFilePath = '/var/run/mongodb/mongodb.sock';
75+
76+
if (file_exists($unixSocketFilePath)) {
77+
$cache = new Mongo([
78+
'unix_socket' => $unixSocketFilePath,
79+
]);
80+
81+
$cache->set('mongodb_socket', 'good');
82+
$this->assertSame('good', $cache->get('mongodb_socket'));
83+
} else {
84+
$this->console(sprintf(
85+
'Ingore testing with unix domain socket because that file "%s" does not exist.',
86+
$unixSocketFilePath
87+
));
88+
}
89+
}
7190
}

tests/SimpleCache/Driver/RedisTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,23 @@ public function testInvalidUsernameAndPassword()
4343

4444
// Nothing happended??
4545
}
46+
47+
public function testConnectWithUnixSocket()
48+
{
49+
$unixSocketFilePath = '/var/run/redis/redis.sock';
50+
51+
if (file_exists($unixSocketFilePath)) {
52+
$cache = new Redis([
53+
'unix_socket' => $unixSocketFilePath,
54+
]);
55+
56+
$cache->set('redis_socket', 'good');
57+
$this->assertSame('good', $cache->get('redis_socket'));
58+
} else {
59+
$this->console(sprintf(
60+
'Ingore testing with unix domain socket because that file "%s" does not exist.',
61+
$unixSocketFilePath
62+
));
63+
}
64+
}
4665
}

0 commit comments

Comments
 (0)