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 黾龎麃鼯鼾鼼鱺鱒龐齍齌龎鼆鼾鼼龜齾齨鮮鮬

本书共88小节:

评论 (写第一个评论)