|
| 1 | +# Tarantool symfony-lock |
| 2 | +[](https://packagist.org/packages/tarantool/symfony-lock) |
| 3 | +[](https://travis-ci.org/tarantool-php/symfony-lock) |
| 4 | +[](https://github.com/tarantool-php/symfony-lock/releases) |
| 5 | +[](https://packagist.org/packages/tarantool/symfony-lock) |
| 6 | +[](https://scrutinizer-ci.com/g/tarantool-php/symfony-lock/?branch=master) |
| 7 | +[](https://scrutinizer-ci.com/g/tarantool-php/symfony-lock/?branch=master) |
| 8 | + |
| 9 | +# About |
| 10 | + |
| 11 | +The `TarantoolStore` implements symfony `PersistingStoreInterface` and persist locks using Tarantool Database. |
| 12 | + |
| 13 | +# Installation |
| 14 | + |
| 15 | +The recommended way to install the library is through [Composer](http://getcomposer.org): |
| 16 | +``` |
| 17 | +$ composer require tarantool/symfony-lock |
| 18 | +``` |
| 19 | + |
| 20 | +# Prepare Tarantool |
| 21 | + |
| 22 | +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](https://github.com/tarantool-php/client#creating-a-client). |
| 23 | + |
| 24 | +```php |
| 25 | +use Tarantool\Client\Client; |
| 26 | +use Tarantool\SymfonyLock\SchemaManager; |
| 27 | + |
| 28 | +$client = Client::fromDefaults(); |
| 29 | +$schema = new SchemaManager($client); |
| 30 | + |
| 31 | +// create spaces and indexes |
| 32 | +$schema->setup(); |
| 33 | + |
| 34 | +// later if you want to cleanup lock space, use |
| 35 | +$schema->tearDown(); |
| 36 | + |
| 37 | +``` |
| 38 | + |
| 39 | +# Using Store |
| 40 | + |
| 41 | +For additional examples on lock factory usage follow [symfony/lock docs](https://symfony.com/doc/current/components/lock.html) |
| 42 | + |
| 43 | +```php |
| 44 | +use Symfony\Component\Lock\LockFactory; |
| 45 | +use Tarantool\Client\Client; |
| 46 | +use Tarantool\SymfonyLock\TarantoolStore; |
| 47 | + |
| 48 | +$client = Client::fromDefaults(); |
| 49 | +$store = new TarantoolStore($client); |
| 50 | +$factory = new LockFactory($store); |
| 51 | + |
| 52 | +$lock = $factory->createLock('pdf-invoice-generation'); |
| 53 | + |
| 54 | +if ($lock->acquire()) { |
| 55 | + // The resource "pdf-invioce-generation" is locked. |
| 56 | + // You can compute and generate invoice safely here. |
| 57 | + $lock->release(); |
| 58 | +} |
| 59 | + |
| 60 | +``` |
| 61 | + |
| 62 | +# Expiration helper |
| 63 | + |
| 64 | +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 expiration daemon to cleanup your database. |
| 65 | + |
| 66 | +```php |
| 67 | +use Tarantool\Client\Client; |
| 68 | +use Tarantool\SymfonyLock\ExpirationDaemon; |
| 69 | + |
| 70 | +$client = Client::fromDefaults(); |
| 71 | +$expiration = new ExpirationDaemon($client); |
| 72 | + |
| 73 | +// cleanup keys that are expired |
| 74 | +$expiration->process(); |
| 75 | + |
| 76 | +// by default expiration daemon will clean upto 100 items |
| 77 | +// you can override it via optional configuration |
| 78 | +$expiration = new ExpirationDaemon($client, [ |
| 79 | + 'limit' => 10, |
| 80 | +]); |
| 81 | + |
| 82 | +``` |
| 83 | + |
| 84 | +# Customization |
| 85 | + 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: |
| 86 | + |
| 87 | +```php |
| 88 | +use Tarantool\Client\Client; |
| 89 | +use Tarantool\SymfonyLock\ExpirationDaemon; |
| 90 | +use Tarantool\SymfonyLock\SchemaManager; |
| 91 | +use Tarantool\SymfonyLock\TarantoolStore; |
| 92 | + |
| 93 | +$client = Client::fromDefaults(); |
| 94 | +$expiration = new ExpirationDaemon($client, [ |
| 95 | + 'space' => 'lock', |
| 96 | + 'limit' => '100', |
| 97 | +]); |
| 98 | + |
| 99 | +$schema = new SchemaManager($client, [ |
| 100 | + 'engine' => 'memtx', |
| 101 | + 'space' => 'lock', |
| 102 | +]); |
| 103 | + |
| 104 | +$store = new TarantoolStore($client, [ |
| 105 | + 'space' => 'lock', |
| 106 | + 'initialTtl' => 300, |
| 107 | +]); |
| 108 | +``` |
0 commit comments