Offline Locker Bundle
, (*1)
ΠΠΎΠ΄ΡΠ»Ρ Symfony Π΄Π»Ρ Π΄Π»ΠΈΡΠ΅Π»ΡΠ½ΡΡ
Π±Π»ΠΎΠΊΠΈΡΠΎΠ²ΠΎΠΊ ΠΏΠΎ ΠΊΠ»ΡΡΠ°ΠΌ, (*2)
Π£ΡΡΠ°Π½ΠΎΠ²ΠΊΠ°
ΠΠΎΠ΄ΡΠ»Ρ ΡΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅ΡΡΡ ΡΠ΅ΡΠ΅Π· Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ Π·Π°Π²ΠΈΡΠΈΠΌΠΎΡΡΠΈ Π² composer, (*3)
composer require text-media/offline-locker-bundle
Π ΠΊΠΎΠ½ΡΠΈΠ³ Π½ΡΠΆΠ½ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ ΡΠ΅ΠΊΡΠΈΡ offline_locker ΠΈ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡΠ΅Π»ΡΠ½ΡΠΉ ΠΊΠΎΠ½ΡΠΈΠ³ doctrine Π΄Π»Ρ ΠΎΡΠ΄Π΅Π»ΡΠ½ΠΎΠ³ΠΎ ΠΏΠΎΠ΄ΠΊΠ»ΡΡΠ΅Π½ΠΈΡ, (*4)
offline_locker:
# ΠΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΠΌΡΠΉ ΠΌΠ΅Π½Π΅Π΄ΠΆΠ΅Ρ
entity_manager: offline_locker
doctrine:
# ΠΡΠ΄Π΅Π»ΡΠ½ΠΎΠ΅ ΠΏΠΎΠ΄ΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ ΠΊ ΠΠ
dbal:
connections:
offline_locker:
driver: pdo_sqlite
path: "%kernel.cache_dir%/offline_locker.db"
charset: UTF8
orm:
entity_managers:
offline_locker:
connection: offline_locker
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
OfflineLockerBundle: ~
ΠΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΌΠΈΠ³ΡΠ°ΡΠΈΡ Π°Π²ΡΠΎΠΌΠ°ΡΠΈΡΠ΅ΡΠΊΠΈ ΠΈΠ»ΠΈ Π²ΡΡΡΠ½ΡΡ, (*5)
php ./bin/console doctrine:schema:update --em=offline_locker --dump-sql
ΠΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅
Π ΠΊΠΎΠ½ΡΠ΅ΠΉΠ½Π΅ΡΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½ ΡΠ΅ΡΠ²ΠΈΡ \TextMedia\OfflineLockerBundle\OfflineLockerInterface
(ΠΈΠ»ΠΈ offline_locker
) ΡΠ΅Π°Π»ΠΈΠ·ΡΡΡΠΈΠΉ ΠΎΠ΄Π½ΠΎΠΈΠΌΠ΅Π½Π½ΡΠΉ ΠΈΠ½ΡΠ΅ΡΡΠ΅ΠΉΡ, (*6)
<?php
use TextMedia\OfflineLockerBundle\ConcurrencyException;
use TextMedia\OfflineLockerBundle\OfflineLockerInterface;
class MyService
{
public function __construct(OfflineLockerInterface $locker)
{
$lockKey = 'order::1000';
$lockOwner = __CLASS__;
try {
# ΠΠ°Ρ
Π²Π°ΡΠΈΡΡ Π±Π»ΠΎΠΊΠΈΡΠΎΠ²ΠΊΡ
$locker->acquireLock($lockKey, $lockOwner);
} catch (ConcurrencyException $e) {
# ΠΠ»ΠΎΠΊΠΈΡΠΎΠ²ΠΊΠ° Π½Π΅ Π·Π°Ρ
Π²Π°ΡΠ΅Π½Π°
}
try {
$this->doSomething();
} finally {
# ΠΡΠ²ΠΎΠ±ΠΎΠ΄ΠΈΡΡ Π±Π»ΠΎΠΊΠΈΡΠΎΠ²ΠΊΡ
$locker->releaseLock($lockKey, $lockOwner);
}
}
}