tarantool / symfony-lock
Symfony lock Tarantool store
Installs: 4 206
Dependents: 2
Suggesters: 0
Security: 0
Stars: 2
Watchers: 4
Forks: 0
Open Issues: 0
Requires
- php: >=8.2
- symfony/lock: >=7.0
- tarantool/client: >=0.9.0
Requires (Dev)
- phpunit/phpunit: ^9.2.5
- rybakit/msgpack: ^0.7.0
Suggests
- ext-msgpack: For using PeclPacker.
- rybakit/msgpack: For using PurePacker.
README
About
The TarantoolStore
implements symfony PersistingStoreInterface
using Tarantool Database.
Installation
The recommended way to install the library is through Composer:
$ composer require tarantool/symfony-lock
Prepare Tarantool
To start working on locking you need to create schema, the package contains schema manager that can be used for that. For additional documentation on client configuration see tarantool/client repository.
use Tarantool\Client\Client; use Tarantool\SymfonyLock\SchemaManager; $client = Client::fromDefaults(); $schema = new SchemaManager($client); // create spaces and indexes $schema->setup(); // later if you want to cleanup lock space, use $schema->tearDown(); // in addition you can configure TarantoolStore to create schema on demand // pay attention, this option is false by default $store = new TarantoolStore($client, [ 'createSchema' => true, ]);
Using Store
For additional examples on lock factory usage follow symfony/lock docs
use Symfony\Component\Lock\LockFactory; use Tarantool\Client\Client; use Tarantool\SymfonyLock\TarantoolStore; $client = Client::fromDefaults(); $store = new TarantoolStore($client); $factory = new LockFactory($store); $lock = $factory->createLock('pdf-invoice-generation'); if ($lock->acquire()) { // The resource "pdf-invioce-generation" is locked. // You can compute and generate invoice safely here. $lock->release(); }
Expiration helper
When key is expired it will be removed on acquiring new lock with same name. If your key names are not unique, you can use php cleaner to cleanup your database. The best way to cleanup data is start a fiber inside tarantool. For more details see expirationd module documentation
use Tarantool\Client\Client; use Tarantool\SymfonyLock\Cleaner; $client = Client::fromDefaults(); $cleaner = new Cleaner($client); // cleanup keys that are expired $cleaner->process(); // by default cleaner will process upto 100 items // you can override it via optional configuration $cleaner = new Cleaner($client, [ 'limit' => 10, ]);
Customization
Out of the box all classes are using space named lock
. If you want - you can override it via options configuration. All available options and default values are listed below:
use Tarantool\Client\Client; use Tarantool\SymfonyLock\Cleaner; use Tarantool\SymfonyLock\SchemaManager; use Tarantool\SymfonyLock\TarantoolStore; $client = Client::fromDefaults(); $cleaner = new Cleaner($client, [ 'space' => 'lock', 'limit' => '100', ]); $schema = new SchemaManager($client, [ 'engine' => 'memtx', 'space' => 'lock', ]); $store = new TarantoolStore($client, [ 'space' => 'lock', 'initialTtl' => 300, 'createSchema' => false, ]);