ko tt les 5 appels
This commit is contained in:
parent
2f32ad5e00
commit
8cd71b7521
@ -1,19 +1,3 @@
|
||||
framework:
|
||||
cache:
|
||||
# Unique name of your app: used to compute stable namespaces for cache keys.
|
||||
#prefix_seed: your_vendor_name/app_name
|
||||
|
||||
# The "app" cache stores to the filesystem by default.
|
||||
# The data in this cache should persist between deploys.
|
||||
# Other options include:
|
||||
|
||||
# Redis
|
||||
#app: cache.adapter.redis
|
||||
#default_redis_provider: redis://localhost
|
||||
|
||||
# APCu (not recommended with heavy random-write workloads as memory fragmentation can cause perf issues)
|
||||
#app: cache.adapter.apcu
|
||||
|
||||
# Namespaced pools use the above "app" backend by default
|
||||
#pools:
|
||||
#my.dedicated.cache: null
|
||||
app: cache.adapter.filesystem
|
||||
|
@ -10,6 +10,7 @@ services:
|
||||
_defaults:
|
||||
autowire: true # Automatically injects dependencies in your services.
|
||||
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
|
||||
public: true
|
||||
|
||||
# makes classes in src/ available to be used as services
|
||||
# this creates a service per class whose id is the fully-qualified class name
|
||||
|
@ -4,6 +4,7 @@ namespace App\Controller;
|
||||
|
||||
use App\Dto\DemandeLogementRequestVO;
|
||||
use App\Dto\ErreurReponseVO;
|
||||
use App\Service\CacheCounterService;
|
||||
use Nelmio\ApiDocBundle\Annotation\Model;
|
||||
use OpenApi\Attributes as OA;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
@ -12,6 +13,13 @@ use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
class DemandeLogementController
|
||||
{
|
||||
private CacheCounterService $cacheCounterService;
|
||||
|
||||
public function __construct(CacheCounterService $cacheCounterService)
|
||||
{
|
||||
$this->cacheCounterService = $cacheCounterService;
|
||||
}
|
||||
|
||||
#[Route('/api/v1/etudiants/{typeid}/{id}/demande-logement', methods: ['POST'])]
|
||||
#[OA\Tag(name: 'Service de demande de logement d\'un étudiant')]
|
||||
#[OA\Post(
|
||||
@ -83,14 +91,15 @@ class DemandeLogementController
|
||||
return new JsonResponse(['error' => 'Invalid request'], 400);
|
||||
}
|
||||
|
||||
if ($typid="id-vpe" && $id % 2 == 0) {
|
||||
$error=new ErreurReponseVO;
|
||||
$error->setStatus(400);
|
||||
$error->setError('IdPVE paire Mock force KO');
|
||||
$error->setPath($request->getUri());
|
||||
return new JsonResponse($error);
|
||||
// Incrémenter le compteur d'appel pour ce idPve
|
||||
$count = $this->cacheCounterService->incrementCounter('logement',$id);
|
||||
|
||||
// Générer une erreur tout les 5 appels associés à cet idE
|
||||
if ($count % 5 == 0) {
|
||||
$this->cacheCounterService->resetCounter('logement',$id);
|
||||
return new JsonResponse(['status' => 'KO', 'message' => '5eme appel pour cet ID = KO'], 404);
|
||||
}
|
||||
|
||||
return new JsonResponse(['status' => 'OK', 'message' => 'IdPVE impaire Mock force OK'], 201);
|
||||
return new JsonResponse(['status' => 'OK', 'message' => 'OK nb appel = '.($count%5)], 201);
|
||||
}
|
||||
}
|
@ -2,17 +2,26 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Dto\DemandeLogementRequestVO;
|
||||
use App\Dto\ErreurReponseVO;
|
||||
use App\Dto\ProfilEtudiantRequestVO;
|
||||
use App\Service\CacheCounterService;
|
||||
use Nelmio\ApiDocBundle\Annotation\Model;
|
||||
use OpenApi\Attributes as OA;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
class ProfilEtudiantController
|
||||
{
|
||||
private CacheCounterService $cacheCounterService;
|
||||
|
||||
public function __construct(CacheCounterService $cacheCounterService)
|
||||
{
|
||||
$this->cacheCounterService = $cacheCounterService;
|
||||
}
|
||||
|
||||
#[Route('/api/v1/notifications/mse/profil-etudiant', name: 'notifier_modification_profil_etudiant', methods: ['POST'])]
|
||||
#[OA\Tag(name: 'Service de notifications')]
|
||||
#[OA\Post(
|
||||
@ -74,11 +83,17 @@ class ProfilEtudiantController
|
||||
return new JsonResponse(['error' => 'idPVE doit être un entier'], 400);
|
||||
}
|
||||
|
||||
if ($data['idPVE'] % 2 == 0) {
|
||||
return new JsonResponse(['status' => 'KO', 'message' => 'IdPVE paire Mock force KO'], 404);
|
||||
}
|
||||
// Incrémenter le compteur d'appel pour ce idPve
|
||||
$id=$data['idPVE'];
|
||||
$count = $this->cacheCounterService->incrementCounter('profil',$id);
|
||||
|
||||
return new JsonResponse(['status' => 'OK', 'message' => 'IdPVE impaire Mock force OK'], 201);
|
||||
// Générer une erreur tout les 5 appels associés à cet idPVE
|
||||
if ($count % 5 == 0) {
|
||||
$this->cacheCounterService->resetCounter('profil',$id);
|
||||
return new JsonResponse(['status' => 'KO', 'message' => '5eme appel pour cet ID = KO'], 404);
|
||||
}
|
||||
|
||||
return new JsonResponse(['status' => 'OK', 'message' => 'OK nb appel = '.($count%5)], 201);
|
||||
}
|
||||
|
||||
|
||||
|
51
src/Service/CacheCounterService.php
Normal file
51
src/Service/CacheCounterService.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
class CacheCounterService
|
||||
{
|
||||
private CacheInterface $cache;
|
||||
|
||||
public function __construct(CacheInterface $cache)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gère l'incrémentation du compteur dans le cache pour un ID donné
|
||||
*
|
||||
* @param string $id L'identifiant unique pour lequel on suit le nombre d'occurrences
|
||||
* @return int Le nombre d'occurrences de cet ID dans le cache
|
||||
*/
|
||||
public function incrementCounter(string $type, string $id): int
|
||||
{
|
||||
$cacheKey = 'id_count_'.$type.'_'.$id;
|
||||
|
||||
// Récupérer ou initialiser le compteur à 0 si l'ID n'existe pas dans le cache
|
||||
$count = $this->cache->get($cacheKey, function (ItemInterface $item) {
|
||||
$item->expiresAfter(3600); // Expire après 1 heure
|
||||
return 0;
|
||||
});
|
||||
|
||||
// Incrémenter le compteur
|
||||
$count++;
|
||||
|
||||
// Mettre à jour le cache avec la nouvelle valeur
|
||||
// Supprimer l'ancienne valeur du cache et réécrire la nouvelle
|
||||
$this->cache->delete($cacheKey); // Supprime l'ancienne valeur
|
||||
$this->cache->get($cacheKey, function (ItemInterface $item) use ($count) {
|
||||
$item->expiresAfter(3600); // Expire après 1 heure
|
||||
return $count;
|
||||
});
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
public function resetCounter(string $type, string $id): void{
|
||||
$cacheKey = 'id_count_'.$type.'_'.$id;
|
||||
$this->cache->delete($cacheKey);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user