1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
mutex.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Core\Base;
4
5use Bitrix\Main\Application;
6use Bitrix\Main\Data\ManagedCache;
7
8class Mutex
9{
10 private string $name;
11 private ManagedCache $cache;
12
13 public function __construct(string $name)
14 {
15 $this->cache = Application::getInstance()->getManagedCache();
16 $this->name = 'calendar_sync_' . $name . '_mutex';
17 }
18
19 public function __destruct()
20 {
21 // $this->unlock();
22 }
23
29 public function lock(int $lockTime = 3600): bool
30 {
31 $currentTime = time();
32 if ($this->cache->read(1800, $this->name))
33 {
34 $value = $this->cache->get($this->name);
35 }
36
37 if (!empty($value) && $value > $currentTime)
38 {
39 return false;
40 }
41
42 $this->cache->setImmediate($this->name, $currentTime + $lockTime);
43 return true;
44 }
45
49 public function unlock(): bool
50 {
51 $this->cache->setImmediate($this->name, time());
52 $this->cache->clean($this->name);
53 return true;
54 }
55}
__construct(string $name)
Определения mutex.php:13
lock(int $lockTime=3600)
Определения mutex.php:29