FileLock in Magento 2
题
I want to create a file locker in Magento 2. If i use the default \Magento\Framework\Lock\LockManagerInterface
Magento uses the database to save locks by default. https://devdocs.magento.com/guides/v2.3/install-gde/install/cli/install-cli-subcommands-lock.html
How I can use a fileLock with a custom folder in the var folder?
解决方案
di.xml
<type name="Magento\Framework\Lock\Backend\FileLock">
<arguments>
<argument name="path" xsi:type="string">var/lock</argument>
</arguments>
</type>
<type name="Vendor\ModuleName\Test">
<arguments>
<argument name="fileLock" xsi:type="object">Magento\Framework\Lock\Backend\FileLock</argument>
</arguments>
</type>
Test.php
namespace Vendor\ModuleName;
class Test
{
/**
* @var \Magento\Framework\Lock\LockManagerInterface
*/
private $fileLock;
/**
* @var \Magento\Framework\Lock\LockManagerInterface $fileLock
*/
public function __construct(
\Magento\Framework\Lock\LockManagerInterface $fileLock,
) {
$this->fileLock = $fileLock;
}
}