Skip to content

Commit 6bf0712

Browse files
author
Johann Saunier
committed
Merge pull request #48 from damonsson/redis-client-storage-prefix
[feature] possible use prefix on redis client storage
2 parents e6d398e + 875c709 commit 6bf0712

File tree

4 files changed

+39
-28
lines changed

4 files changed

+39
-28
lines changed

Client/Driver/PredisDriver.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,27 @@ class PredisDriver implements DriverInterface
1414
*/
1515
protected $client;
1616

17+
/**
18+
* string $prefix
19+
*/
20+
protected $prefix;
21+
1722
/**
1823
* @param Client $client
24+
* @param string $prefix
1925
*/
20-
public function __construct(Client $client)
26+
public function __construct(Client $client, $prefix = '')
2127
{
2228
$this->client = $client;
29+
$this->prefix = ($prefix !== '' ? $prefix . ':' : '');
2330
}
2431

2532
/**
2633
* {@inheritdoc}
2734
*/
2835
public function fetch($id)
2936
{
30-
$result = $this->client->get($id);
37+
$result = $this->client->get($this->prefix . $id);
3138
if (null === $result) {
3239
return false;
3340
}
@@ -40,7 +47,7 @@ public function fetch($id)
4047
*/
4148
public function contains($id)
4249
{
43-
return $this->client->exists($id);
50+
return $this->client->exists($this->prefix . $id);
4451
}
4552

4653
/**
@@ -49,9 +56,9 @@ public function contains($id)
4956
public function save($id, $data, $lifeTime = 0)
5057
{
5158
if ($lifeTime > 0) {
52-
$response = $this->client->setex($id, $lifeTime, $data);
59+
$response = $this->client->setex($this->prefix . $id, $lifeTime, $data);
5360
} else {
54-
$response = $this->client->set($id, $data);
61+
$response = $this->client->set($this->prefix . $id, $data);
5562
}
5663

5764
return $response === true || $response == 'OK';
@@ -62,6 +69,6 @@ public function save($id, $data, $lifeTime = 0)
6269
*/
6370
public function delete($id)
6471
{
65-
return $this->client->del($id) > 0;
72+
return $this->client->del($this->prefix . $id) > 0;
6673
}
6774
}

DependencyInjection/Configuration.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
class Configuration implements ConfigurationInterface
1212
{
1313
const DEFAULT_TTL = 900;
14+
const DEFAULT_PREFIX = '';
1415
const DEFAULT_CLIENT_STORAGE_SERVICE = '@gos_web_socket.server.in_memory.client_storage.driver';
1516
const DEFAULT_FIREWALL = 'ws_firewall';
1617
const DEFAULT_ORIGIN_CHECKER = false;
@@ -45,6 +46,10 @@ public function getConfigTreeBuilder()
4546
->defaultValue(static::DEFAULT_TTL)
4647
->example(3600)
4748
->end()
49+
->scalarNode('prefix')
50+
->defaultValue(static::DEFAULT_PREFIX)
51+
->example('client')
52+
->end()
4853
->scalarNode('decorator')->end()
4954
->end()
5055
->end()

DependencyInjection/GosWebSocketExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function load(array $configs, ContainerBuilder $container)
5252
}
5353

5454
$container->setParameter('web_socket_server.client_storage.ttl', $configs['client']['storage']['ttl']);
55+
$container->setParameter('web_socket_server.client_storage.prefix', $configs['client']['storage']['prefix']);
5556

5657
//client
5758
if (isset($configs['client'])) {

Resources/docs/SessionSetup.md

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ gos_web_socket:
7171
session_handler: @session.handler.pdo
7272
storage:
7373
driver: @gos_web_scocket.client_storage.predis.driver
74+
ttl: 28800 #(optionally) time to live if you use redis driver
75+
prefix: client #(optionally) prefix if you use redis driver, create key "client:1" instead key "1"
7476
```
7577
7678
### Doctrine Cache Bundle as Client Storage Driver
@@ -147,22 +149,27 @@ class PredisDriver implements DriverInterface
147149
*/
148150
protected $client;
149151

152+
/**
153+
* string $prefix
154+
*/
155+
protected $prefix;
156+
150157
/**
151158
* @param Client $client
159+
* @param string $prefix
152160
*/
153-
public function __construct(Client $client)
161+
public function __construct(Client $client, $prefix = '')
154162
{
155163
$this->client = $client;
164+
$this->prefix = ($prefix !== false ? $prefix . ':' : '');
156165
}
157166

158167
/**
159-
* @param string $id
160-
*
161-
* @return mixed
168+
* {@inheritdoc}
162169
*/
163170
public function fetch($id)
164171
{
165-
$result = $this->client->get($id);
172+
$result = $this->client->get($this->prefix . $id);
166173
if (null === $result) {
167174
return false;
168175
}
@@ -171,43 +178,33 @@ class PredisDriver implements DriverInterface
171178
}
172179

173180
/**
174-
* @param $id
175-
*
176-
* @return bool
181+
* {@inheritdoc}
177182
*/
178183
public function contains($id)
179184
{
180-
return $this->client->exists($id);
185+
return $this->client->exists($this->prefix . $id);
181186
}
182187

183188
/**
184-
* @param string $id
185-
* @param mixed $data
186-
* @param int $lifeTime
187-
*
188-
* @return bool True if saved, false otherwise
189+
* {@inheritdoc}
189190
*/
190191
public function save($id, $data, $lifeTime = 0)
191192
{
192193
if ($lifeTime > 0) {
193-
$response = $this->client->setex($id, $lifeTime, $data);
194+
$response = $this->client->setex($this->prefix . $id, $lifeTime, $data);
194195
} else {
195-
$response = $this->client->set($id, $data);
196+
$response = $this->client->set($this->prefix . $id, $data);
196197
}
197198

198199
return $response === true || $response == 'OK';
199200
}
200201

201202
/**
202-
* Deletes a cache entry.
203-
*
204-
* @param string $id The cache id.
205-
*
206-
* @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
203+
* {@inheritdoc}
207204
*/
208205
public function delete($id)
209206
{
210-
return $this->client->del($id) > 0;
207+
return $this->client->del($this->prefix . $id) > 0;
211208
}
212209
}
213210
```
@@ -220,6 +217,7 @@ services:
220217
class: Gos\Bundle\WebSocketBundle\Client\Driver\PredisDriver
221218
arguments:
222219
- @snc_redis.cache
220+
- %web_socket_server.client_storage.prefix% #(optionally)if you use prefix
223221
```
224222

225223
**NOTE :** Predis driver class is included in GosWebSocketBundle, just register the service like below to use it.

0 commit comments

Comments
 (0)