tarantool/symfony-lock

Symfony lock Tarantool 存储

0.2.10 2024-01-10 18:38 UTC

This package is auto-updated.

Last update: 2024-09-11 09:24:19 UTC


README

License Latest Version Total Downloads Scrutinizer Code Quality Code Coverage Telegram

关于

TarantoolStore 通过 Tarantool 数据库实现了 symfony 的 PersistingStoreInterface

安装

推荐通过 Composer 安装此库。

$ composer require tarantool/symfony-lock

准备 Tarantool

要开始进行锁定操作,需要创建模式,该包包含用于此目的的模式管理器。有关客户端配置的附加文档,请参阅 tarantool/client 仓库

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,
]);

使用 Store

有关锁工厂使用的更多示例,请参阅 symfony/lock 文档

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();
}

过期辅助程序

当键过期时,在获取具有相同名称的新锁时,它将被删除。如果您的键名不唯一,您可以使用 php 清洁器清理您的数据库。清理数据的最佳方法是启动 Tarantool 内部的纤维。有关更多详细信息,请参阅 expirationd 模块文档

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,
]);

自定义

默认情况下,所有类都使用名为 lock 的空间。如果您想替换它,可以通过选项配置来实现。以下列出了所有可用选项和默认值

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,
]);