-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds cache managers for use with API Gateway (#26)
* implement cache manager * implement contract * Fix code styling * wip * wip
- Loading branch information
Showing
24 changed files
with
792 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb\Concerns; | ||
|
||
use Laravel\Reverb\Contracts\ChannelConnectionManager; | ||
|
||
trait SerializesChannels | ||
{ | ||
/** | ||
* Prepare the connection instance values for serialization. | ||
*/ | ||
public function __serialize(): array | ||
{ | ||
return [ | ||
'name' => $this->name, | ||
]; | ||
} | ||
|
||
/** | ||
* Restore the connection after serialization. | ||
*/ | ||
public function __unserialize(array $values): void | ||
{ | ||
$this->name = $values['name']; | ||
$this->connections = app(ChannelConnectionManager::class)->for($this->name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb\Contracts; | ||
|
||
interface ConnectionManager | ||
{ | ||
/** | ||
* Add a new connection. | ||
*/ | ||
public function connect(Connection $connection): void; | ||
|
||
/** | ||
* Find a connection. | ||
*/ | ||
public function find(string $id): ?Connection; | ||
|
||
/** | ||
* Get all the connections. | ||
*/ | ||
public function all(): array; | ||
|
||
/** | ||
* Update the state of a connection. | ||
*/ | ||
public function update(Connection $connection): void; | ||
|
||
/** | ||
* Forget a connection. | ||
*/ | ||
public function forget(Connection $connection): void; | ||
|
||
/** | ||
* Flush all connections. | ||
*/ | ||
public function flush(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb\Managers; | ||
|
||
use Illuminate\Contracts\Cache\Repository; | ||
use Laravel\Reverb\Contracts\ChannelConnectionManager; | ||
use Laravel\Reverb\Contracts\Connection; | ||
use Laravel\Reverb\Contracts\ConnectionManager; | ||
use Laravel\Reverb\Servers\Reverb\ChannelConnection; | ||
|
||
class CacheChannelConnectionManager implements ChannelConnectionManager | ||
{ | ||
protected string $name; | ||
|
||
public function __construct( | ||
protected Repository $repository, | ||
protected ConnectionManager $connections, | ||
protected $prefix = 'reverb' | ||
) { | ||
// | ||
} | ||
|
||
/** | ||
* The channel name. | ||
*/ | ||
public function for(string $name): ChannelConnectionManager | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get the key for the channels. | ||
*/ | ||
protected function key(): string | ||
{ | ||
return "{$this->prefix}:{$this->name}"; | ||
} | ||
|
||
/** | ||
* Add a connection. | ||
*/ | ||
public function add(Connection $connection, array $data): void | ||
{ | ||
$connections = $this->repository->get($this->key(), []); | ||
|
||
$connections[$connection->identifier()] = $data; | ||
|
||
$this->repository->put($this->key(), $connections); | ||
} | ||
|
||
/** | ||
* Remove a connection. | ||
*/ | ||
public function remove(Connection $connection): void | ||
{ | ||
$connections = $this->repository->get($this->key()); | ||
|
||
unset($connections[$connection->identifier()]); | ||
|
||
$this->repository->put($this->key(), $connections); | ||
} | ||
|
||
/** | ||
* Find a connection in the set. | ||
*/ | ||
public function find(Connection $connection): ?ChannelConnection | ||
{ | ||
return $this->findById($connection->identifier()); | ||
} | ||
|
||
/** | ||
* Find a connection in the set by its ID. | ||
*/ | ||
public function findById(string $id): ?ChannelConnection | ||
{ | ||
$connection = $this->connections->find($id); | ||
|
||
if (! $connection) { | ||
return null; | ||
} | ||
|
||
return new ChannelConnection( | ||
$connection, | ||
$this->repository->get($this->key())[$id] ?? [] | ||
); | ||
} | ||
|
||
/** | ||
* Get all the connections. | ||
*/ | ||
public function all(): array | ||
{ | ||
$connections = $this->connections->all(); | ||
$channelConnections = $this->repository->get($this->key(), []); | ||
$allConnections = array_intersect_key($connections, $channelConnections); | ||
|
||
return array_map(function ($connection) use ($channelConnections) { | ||
return new ChannelConnection($connection, $channelConnections[$connection->identifier()]); | ||
}, $allConnections); | ||
} | ||
|
||
/** | ||
* Flush the channel connection manager. | ||
*/ | ||
public function flush(): void | ||
{ | ||
$this->repository->put($this->key(), []); | ||
} | ||
} |
Oops, something went wrong.