Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fadilxcoder/circuit-breaker
HFX - Circuit Breaker (Cache mechanism)
https://github.com/fadilxcoder/circuit-breaker
circuit-breaker notes php poc project redis
Last synced: 25 days ago
JSON representation
HFX - Circuit Breaker (Cache mechanism)
- Host: GitHub
- URL: https://github.com/fadilxcoder/circuit-breaker
- Owner: fadilxcoder
- Created: 2022-10-24T11:30:03.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-10-24T11:30:25.000Z (over 2 years ago)
- Last Synced: 2024-11-09T14:32:18.640Z (3 months ago)
- Topics: circuit-breaker, notes, php, poc, project, redis
- Language: PHP
- Homepage:
- Size: 155 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Notes
- https://github.com/DeGraciaMathieu/php-easy-breaker (PHP implementation of circuit breaker pattern)
- Integrate codebase in project itself **without** using composer```php
require __DIR__.'/vendor/autoload.php';
use Exception;
use DeGraciaMathieu\EasyBreaker\Breaker;
use DeGraciaMathieu\EasyBreaker\CircuitBreaker;$firstBreaker = (new Breaker)
->when(Exception::class)
->do(function(Exception $e){
return "it's broken.";
});$secondBreaker = (new Breaker)
->when(Exception::class)
->do(function(Exception $e){
return "really broken.";
});$thirdBreaker = (new Breaker)
->when(AnotherException::class)
->do(function(AnotherException $e){
return "boom.";
});$results = (new CircuitBreaker())
->addBreaker($firstBreaker)
->addBreaker($secondBreaker)
->addBreaker($thirdBreaker)
->closure(function(){
throw new Exception();
});var_dump($results);
// array(2) {
// [0]=>
// string(12) "it's broken."
// [1]=>
// string(18) "really broken."
// }```
**Actual Code**
```php
class ContentController extends Controller
{
public function show(Request $request, UsersRepository $usersRepository)
{
$users = null;
$flag = false;
$key = ['hfx', 'circuit-breaker', 'users'];try {
$users = $usersRepository->getUsers();
Redis::set($key, $users);
} catch (Exception $e) {
$flag = true;
}
if ($flag) {
$cacheUserCircuitBreaker = (new Breaker)
->when(UsersNotFound::class)
->do(function() use ($key) {
return Redis::get($key);
});$results = (new CircuitBreaker())
->addBreaker($cacheUserCircuitBreaker)
->closure(function(){
throw new UsersNotFound();
});
dump('Exception raised to activate(flag) circuit breaker !');
$users = $results[0];
}
return $this->render('content/index.html.twig', [
'users' => $users
]);
}
}```
```php
class UsersRepository extends Repository
{
public function getUsers()
{
// throw new \Exception(); # <-- Raise exception to activate circuit breaker$query = '
```