68. 锁lock服务

龻龺龹龶 web 龭龬龩龥龤龣龢龞龛龗 php 龏龋龊龇龅龛龁龀齽齹龀齵齱龗齭龺齪齧齤齡龛龅齟齵齱龗齞齚齖齕齒齎齌齊齉齅齁鼿鼾鼼龬鼻齚鼹鼷鼶鼴齧龊齵齱鼰鼮龗鼫鼨鼦鼢龤龣鼠鼞齪齧鼚齞龞鼖龗鼒齎龅齟齵齱龛鼎鼊齪齧鼈鼄鼒鼮齵齱龗鼃黿黾黻鼦齎龞鼖龻龺鼞黷黶黲黱 LOCK 黦黿黤

LOCK 龭龬黡黝黛

黱龥鼹鼷黡黝龛鼰齎龭龬龗黡黝齁默黛

  lock:

    class: Drupal\Core\Lock\DatabaseLockBackend

    arguments: ['@database']

    tags:

      - { name: backend_overridable }

   lazy: true

麇齱齟麃龭龬龥麂麀鹿鹻鹺齵齱龛龗鹶鼚鹳龩黡黝鹲鹱齪鹭鹬鹨龭龬 id 黛黲 lock 黦鹦鹣龛龥鹿鹻鹺黛

Drupal\Core\ProxyClass\Lock\DatabaseLockBackend

齚鹳龩鼶龗鹜齤齡龛龭龬鹚龥黛

drupal.proxy_original_service.lock

鹿鹻鹺齪鹭鹔龭龬鼴齒齚齤齡鹒鹏齟鹍龊鹋鹊鹇龗鹃龇龥鸿鸼齟鸹鸶鹋鹊鹇黤

鸿鸼黱龭龬龛鸳鸱黛 $lock=\Drupal::lock()

鸟鸜 $lock 鸘鼃黱鹦鸖龗鼫鹜鼄鸕鸹鸒鸑鹻黱龗鸍鹳龩鼶鼴齧鸿鸼鼰齎黱鹦鸖龗鸌鸈鸅鸃鸂齌齊黷黶鷾齎黱鹦鸖齟齪鹭鷼鷺鹋鹊鹇黛

$lock_1=new \Drupal\Core\Lock\DatabaseLockBackend($database);

鷷黷黶鷾齎黱鹦鸖龛鷵鷱鷯鷬鷫鷨龗鼹鼷鼶鷧鷣齒鹏鹨龛鷢鹊龗鼰鷞龇黷黶黤

黱鷝鹻黛

黱龛齊鹏龥齹齱鷙齭齌齊龗鷗鷓齚龅鼰齟鷐鷏鷌鼴齒鷋鹨黱龛鼎鼊鹍齧齹齱龊黱黡龛齌齊龗齪鷗鷓齚龅鼰齟鷐鷏鷌龛鷉鼰鼠龗鷅鷂龤龣龞鼖龗龇龊鷀齭鼎鼊鶿鶽黤

drupal 黱鼹鼷齚鹲鹱鶹鼶齕鶵鼰齎鹲鹱鶲鼄鶯鶮黱鶬鶪龗鶲鹚鼃黛黲 semaphore 龗齒鶦齎鶤鷏黛

name

黱鹚鸘龗鹿鶲鶢鸈鼰齎齌齊龗鼫鶞龣鶛黻黡龗鶚鶚鹒鹏鶗鹲鹚龗鹲鹱鶲鶖鶔龗龇齪鶑鶏

value

id 龗鼰齎齒鷉鼰鼠龛鶋鶊鶤鶆鶅龗鹿鶲鶢鼰齎黱鹦鸖龗鶃龅黱鹦鸖齕鶵鸑鹻龛鶁齒黱鵿鵾鶃龅黱 id 龗鶚鶚鼰齎鼎鼊鼴齒鼰齎黱鹦鸖龗鶁鹭齒齟齪鹭鵼鵹龛鵵鼰齎鼎鼊鼶齕鶵龛鶁齒黱鹒鹏鶃龅 id 龗麂麀鹜齪鹭鹏鼄鵲鶮鼰齎鼎鼊鼶龛鶁齒黱龗鷷鸌鼰齎鼎鼊齒鷾齎黱鹦鸖齟鹣麃龹龶鹜鹿鶲黱鹦鸖鹃鷯鼎鼊

expire

unix 齟鷐鵮龗鵭鵪鵨龗鹿鶲黱龛鹨鵤齟鷐龗鵣鵤鵢黱鵠鵜鵘齉鵕鸘鼃黱龊鵲鶮鼻

鸌鼒齎齽鷾齎龅齟齵齱龛龤龣鼎鼊鵔齵齱鼰齎鼴齧齵齱鼰鼮龛齌齊齟龗鹜龺鹣麃鸿鸼鼰齎黱龗鼴齒鷋鹨黱鵢鹍齧鶞鵒齌齊龗鵑鵐黱龛麀龀鼞龥鹬鵍鵌齚黱鶬鶪鶲鼶鵉鵈鹲鹱龛麀龀龗鼰鵆鷀鼶鼰齎鼎鼊齚麃鶲鼶鵉鵈黿鶬鵂龗鼞鵁鴾鶢鹜鷋鹨黿黱龗鼫鼨黱鹚 name 龥鹲鹱鶹鶲龛鶖鶔龗鶖鶔龇齪鹭鶑鶏龗鼰鵆齒鵉鵈龗鴼鴺鷀齭鼎鼊鵠龇齧鴶齹齱鵉鵈龗鷧鼞龇齧鷋鹨黱龗鸍鹃鷗鷓黿齚龤龣鼶鼴齒鼰齎鼎鼊齧鷋鹨黱龗鸍鹃龊黱黡龛齌齊鼴齧齵齱鼰鼮龗龋鴵鴴黱龥鼫鹲鹱鶹鼄齹齱鷗鴳龛龗鸌黱黡龛齌齊齵齱鴰鴮鹣麃鴫鴧鵲鶮黱龗鷧鼞龥鸍鹲鹱鶹鼶鴦鴢黱鶬鵂黤

鹏鸱鴡鹊黛

        $operationID = "yunke";
       //代表着将要进行的操作的标识符,往往可以设置为函数名,就是数据库的name值
        $lock = \Drupal::lock(); //得到锁对象
        if ($lock->acquire($operationID)) {
            // 只有得到了锁才能执行操作,否则应该继续等待或放弃执行
            sleep(30); //在本代码运行期间暂停30秒,30内你可以看到数据库被写入了锁信息
            $lock->release($operationID);
            // 当操作结束后应当明确释放锁,依赖锁自动过期有性能损耗

        }

黱鹦鸖鸳鸱鳜黾黛

齚鷺鳙黛

\Drupal\Core\Lock\LockBackendInterface

鼶齒鳖鴫龗鼦鷌鳔齹鼰鳐龛黾鵲黛

public function acquire($name, $timeout = 30.0)

鳉鼎鼰齎黱龗齁鳈鳉鼎鹨黿鵠鳆鳄 true 龗鴌鴋 false 龗鳀鹲 $name 鼃黶鹒鹏龛黱鹚龗鹣麃龥鹿鶲鸈鼢齌齊龛鴗鴖鶆龗鵕鶞龣鶛黻黡龗龇鲼鲸龗鶚鶚龥鶗鹲鹚龗黱鹚鼃 ASCII 鲲鳻龛鶤鶆鶅龗鲮鼨鴇鼨 255 齎鶤鶆龗鴌鴋鳮鲦鵠鹒鹏 Base64 齹齱鲲鳻龗 $timeout 鲠黡黱龛齒鵘齟鷐龗鼰鵆鵣麀鵠鵜鵘龗鲜鲚鼃 30 鳰龗鶞龣鶛鹣麃鲘鲔鲐鲌龛齟鷐鵨龗齚鳉鼎鴮鲋鵢鲊齪鹭鴶鼮鲈鹏麃鸳鸱龗鶲鴡鵠黱鲄鵤龗鲃龛黱齒鵘鵤龥鸍鸌鱿鶞鵒鱻 $timeout 鲠黡龛齟鷐龗鹃龇龥齚鱸鱿龛鹨鵤齟鷐鱷鱵鴴鱻 $timeout 龗鼫鼨鼦鵪鸌鱸鱿鲠黡龛齒鵘鵤鱲鱱齟齪鹭鴶鼮鲈鹏鲠黡鼰齎鱭龛齒鵘鵤鹭鱪鱩鵲鶮黱龗齁鳈鴶鼮鲈鹏齟鳆鳄 false 龗鴼鴺鶲鴡黱鱦鱥龊鷀齭鼎鼊鱤鹏龗鹣麃鶮鴃鸌鱿鱡齊齽鴉鴈鴇鴅黤

public function lockMayBeAvailable($name)

鱛鱗黱龥鴌齪鹭齹齱鳉鼎龗齅齁鱓鱏黱龊鱋鱉龛鼎鼊鳉鼎鹨黿龗鵤鵘鱦麀鷷鱅鷣齒龊鵲鶮龗鴼鴺麃鸳鸱鵠鲼鲸鵲鶮龗龤鳆鳄 true 鶲鴡黱齪鳉鼎龗鳆鳄 true 齟鶲鴡鸟齟齪齧鴮鲋鳉鼎鹨黱龗鳆鳄 false 齟鶲鴡黱鲊齚鱤鹏鼶

public function release($name)

鱁鹱黱鹚鵲鶮黱龗鸌黱鹏鴰齟龗鹣麃鵲鶮鹜鹭鱀鷀齭鼎鼊鹒鹏龗齁鳈黱鵣鵤龗鸶鱀鷣齒鴫鴧鵲鶮龗齚鷀齭鼎鼊鳉鼎齟鼹鼷鷧鰽鰺鳄鹜

public function releaseAll($lock_id = NULL)

麂麀黱 id 鵲鶮黱龗鼦鵠鵲鶮鼰齎黱鹦鸖鸑鹻龛鶁齒黱龗麃鸳鸱龊鰲鰮鶗鹲鰪鰦鼃鰤鶊鶗鹲龗齚龏龋鳬鳩齟鳤鳡齵齱龗鳀鹲 $lock_id 鲜鲚鼃 null 龗鸟齟鵠鵲鶮龋黱鹦鸖鸑鹻龛鶁齒黱龗鰣鰠鲚鼃麃鸳鸱黷黶鰝齹龗鶞龣鶛齪齧龋鵁鰛麂麀黱 id 齧鰙鵲鶮鷀齭黱鹦鸖鸑鹻龛黱龗鷷齚麃鸳鸱鳮鱅鵕鰘齚龋鹦鸖齒鰕鰓黱齟鹍齹齱鵲鶮齌齊龗鰐齚麂鷬鼰齎鼎鼊鼴齒鼰齎黱鹦鸖龗鰎黷鼦鰋龛齌齊鰊龏龋鳬鳩齟鲜鲚鰽鵲鶮龋黱鹦鸖鸑鹻龛鶁齒黱

public function getLockId()

鷋鹨黱 id 龗齚黱龭龬齉龇龥黱龗鹃龥黱鹦鸖龗 $lock = \Drupal::lock(); 鼻鰇鶯鰄鵤鳮 id 龇鰂龗麇鹏鼨鶁齒龛黱龗龥鼰齎鶋鶊鶤鶆鶅龗鰁齎黱鹦鸖鯿龇鼰鰋龗齚龅鼰齎鼎鼊鼶齁鳈鯾鵒鹇鷾齎黱鹦鸖龗鴼鴺麃鼎鼊鷋鹨龛黱鵠齒鷾齎 id 龗鴦鴢齟黷黶鹏 releaseAll 鸳鸱龗齽鶛鴇鴅鵣鵤鳤鳡鴦鴢黤

public function wait($name, $delay = 30)

鸌鳉鼎黱鵜鯽齟龗齪鹭鲈鹏麃鸳鸱鹭齚 $delay 鲠黡龛齟鷐 鳮鴇鴅龗鯺鷾鴇鴅 $delay 鲠黡龛齟鷐龗鯶鯲鳰龗鲜鲚 30 鳰龗鴇鴅鵤鷐鼰鵆龣齞黱齒齪齧鳉鼎鴮鲋龗鴼鴺鹜鸹鸶鳆鳄 false 龗鼦鶲鴡龇鹏鴶鴇龗齪鯱鴴鳉鼎龗鷷龇鷗鷓鼰黡齧鳉鼎鴮鲋龗齁鳈鳆鳄 true 鴋鶲鴡鳦鱏龇齧鳉鼎鹨黱龗黷黶鴉鴈鴇鴅龗齚鳮鲦鹒鹏 php 鶗鹲 usleep 鷐鯭鼠鳸鳴鹿鳻齵齱鹭鴇鴅鳉鼎齟鶊龗鴡鹊齁默黛

        $lock = \Drupal::lock();
        $operationID = "yunke";
        $is_get_lock = false;
        $is_get_lock = $lock->acquire($operationID);
        if (!$is_get_lock) {
            if (!$lock->wait($operationID, 5)) {
                $is_get_lock = $lock->acquire($operationID);
            }
        }
        if ($is_get_lock) {
            //do something
            $lock->release($operationID);
        }

鯪鯩黱黛

鼹鼷鲊黡黝黿鼰齎鯪鯩黱龗鹏鼨鼹鼷鯨鯤麀龀鼶龗鹜龛黱 id 龥鯡黡龛鯞鷣齒鰪鰦鰤鶊鶗鹲龗鷀齭鸒黱龭龬鼰鰋龗龇鳔鳜黾龗黡黝齁默黛

  lock.persistent:
    class: Drupal\Core\Lock\PersistentDatabaseLockBackend
    arguments: ['@database']
    tags:
      - { name: backend_overridable }
    lazy: true

鯝鯛鰪鵁黛

  1. 黱鼹鼷鯚鹏龛龥龏龋麇齱鶁齚鶖鶊鴴龛齟鯖龗齚鸑鹻鯔鰝鰂龭龬龩齟鷐齽鶛麂麀鯒鯎齟鷐鯌鯊 NTP 龅鳐齟鷐齟鵠鹦黱鼹鼷鯅鰇鯃鮿
  2. 齁鳈鯒鮻齒鷾鮺鶖鶊龗鰁齎鶖鶊齟鷐齪齧齒鮸鮵龗鼦鵠齪齧鹶鮲龞鼖黤
  3. 齚齹齱龊黱黡鮰鮭龛鮫鮪齟龗鷧鮦鮢鮫鮪鶯鶮黱龛鶬鵂鶲黲 semaphore 黦龗鹦鼨鼹鼷鱸鱉龛齌齊龗黱鼹鼷龤龇齧鮡鮟

本书共69小节:

评论 (写第一个评论)