Skip to content

Commit c959393

Browse files
committed
Add LockManager and Backend to Framework functionalities
1 parent e6e275b commit c959393

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

app/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<preference for="Magento\Framework\Locale\ListsInterface" type="Magento\Framework\Locale\TranslatedLists" />
3838
<preference for="Magento\Framework\Locale\AvailableLocalesInterface" type="Magento\Framework\Locale\Deployed\Codes" />
3939
<preference for="Magento\Framework\Locale\OptionInterface" type="Magento\Framework\Locale\Deployed\Options" />
40+
<preference for="Magento\Framework\Lock\LockManagerInterface" type="Magento\Framework\Lock\Backend\Database" />
4041
<preference for="Magento\Framework\Api\AttributeTypeResolverInterface" type="Magento\Framework\Reflection\AttributeTypeResolver" />
4142
<preference for="Magento\Framework\Api\Search\SearchResultInterface" type="Magento\Framework\Api\Search\SearchResult" />
4243
<preference for="Magento\Framework\Api\Search\SearchCriteriaInterface" type="Magento\Framework\Api\Search\SearchCriteria"/>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\lock\Backend;
8+
9+
class Database implements \Magento\Framework\Lock\LockManagerInterface
10+
{
11+
/** @var \Magento\Framework\DB\Adapter\AdapterInterface */
12+
private $adapter;
13+
14+
public function __construct(
15+
\Magento\Framework\DB\Adapter\AdapterInterface $adapter
16+
)
17+
{
18+
$this->adapter = $adapter;
19+
}
20+
21+
/**
22+
* Sets a lock for name
23+
*
24+
* @param string $name lock name
25+
* @return bool
26+
*/
27+
public function setLock($name, $timeout = -1)
28+
{
29+
return (bool) $this->adapter->query("SELECT GET_LOCK(?, ?);", array((string)$name, (int)$timeout))
30+
->fetchColumn();
31+
}
32+
33+
/**
34+
* Releases a lock for name
35+
*
36+
* @param string $name lock name
37+
* @return bool
38+
*/
39+
public function releaseLock($name)
40+
{
41+
return (bool) $this->adapter->query("SELECT RELEASE_LOCK(?);", array((string)$name))->fetchColumn();
42+
}
43+
44+
/**
45+
* Tests of lock is set for name
46+
*
47+
* @param string $name lock name
48+
* @return bool
49+
*/
50+
public function isLocked($name)
51+
{
52+
return (bool) $this->adapter->query("SELECT IS_USED_LOCK(?);", array((string)$name))->fetchColumn();
53+
}
54+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Lock;
7+
8+
/**
9+
* Interface of a lock manager
10+
*
11+
* @api
12+
*/
13+
interface LockManagerInterface
14+
{
15+
/**
16+
* Sets a lock for name
17+
*
18+
* @param string $name lock name
19+
* @param int $timeout Timeout in seconds, negative value means infinite timeout
20+
* @return bool
21+
*/
22+
public function setLock($name, $timeout = -1);
23+
24+
/**
25+
* Releases a lock for name
26+
*
27+
* @param string $name lock name
28+
* @return bool
29+
* @api
30+
*/
31+
public function releaseLock($name);
32+
33+
/**
34+
* Tests of lock is set for name
35+
*
36+
* @param string $name lock name
37+
* @return bool
38+
* @api
39+
*/
40+
public function isLocked($name);
41+
}

0 commit comments

Comments
 (0)