8000 symfony-docs/lock.rst at 7.1 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content
{"payload":{"allShortcutsEnabled":false,"fileTree":{"":{"items":[{"name":".github","path":".github","contentType":"directory"},{"name":"_build","path":"_build","contentType":"directory"},{"name":"_images","path":"_images","contentType":"directory"},{"name":"_includes","path":"_includes","contentType":"directory"},{"name":"bundles","path":"bundles","contentType":"directory"},{"name":"components","path":"components","contentType":"directory"},{"name":"configuration","path":"configuration","contentType":"directory"},{"name":"console","path":"console","contentType":"directory"},{"name":"contributing","path":"contributing","contentType":"directory"},{"name":"controller","path":"controller","contentType":"directory"},{"name":"create_framework","path":"create_framework","contentType":"directory"},{"name":"deployment","path":"deployment","contentType":"directory"},{"name":"doctrine","path":"doctrine","contentType":"directory"},{"name":"form","path":"form","contentType":"directory"},{"name":"frontend","path":"frontend","contentType":"directory"},{"name":"getting_started","path":"getting_started","contentType":"directory"},{"name":"http_cache","path":"http_cache","contentType":"directory"},{"name":"introduction","path":"introduction","contentType":"directory"},{"name":"logging","path":"logging","contentType":"directory"},{"name":"messenger","path":"messenger","contentType":"directory"},{"name":"quick_tour","path":"quick_tour","contentType":"directory"},{"name":"reference","path":"reference","contentType":"directory"},{"name":"routing","path":"routing","contentType":"directory"},{"name":"security","path":"security","contentType":"directory"},{"name":"serializer","path":"serializer","contentType":"directory"},{"name":"service_container","path":"service_container","contentType":"directory"},{"name":"setup","path":"setup","contentType":"directory"},{"name":"testing","path":"testing","contentType":"directory"},{"name":"validation","path":"validation","contentType":"directory"},{"name":"workflow","path":"workflow","contentType":"directory"},{"name":".alexrc","path":".alexrc","contentType":"file"},{"name":".doctor-rst.yaml","path":".doctor-rst.yaml","contentType":"file"},{"name":".editorconfig","path":".editorconfig","contentType":"file"},{"name":".gitignore","path":".gitignore","contentType":"file"},{"name":"LICENSE.md","path":"LICENSE.md","contentType":"file"},{"name":"README.md","path":"README.md","contentType":"file"},{"name":"best_practices.rst","path":"best_practices.rst","contentType":"file"},{"name":"bundles.rst","path":"bundles.rst","contentType":"file"},{"name":"cache.rst","path":"cache.rst","contentType":"file"},{"name":"configuration.rst","path":"configuration.rst","contentType":"file"},{"name":"console.rst","path":"console.rst","contentType":"file"},{"name":"controller.rst","path":"controller.rst","contentType":"file"},{"name":"deployment.rst","path":"deployment.rst","contentType":"file"},{"name":"doctrine.rst","path":"doctrine.rst","contentType":"file"},{"name":"emoji.rst","path":"emoji.rst","contentType":"file"},{"name":"event_dispatcher.rst","path":"event_dispatcher.rst","contentType":"file"},{"name":"forms.rst","path":"forms.rst","contentType":"file"},{"name":"frontend.rst","path":"frontend.rst","contentType":"file"},{"name":"html_sanitizer.rst","path":"html_sanitizer.rst","contentType":"file"},{"name":"http_cache.rst","path":"http_cache.rst","contentType":"file"},{"name":"http_client.rst","path":"http_client.rst","contentType":"file"},{"name":"index.rst","path":"index.rst","contentType":"file"},{"name":"lock.rst","path":"lock.rst","contentType":"file"},{"name":"logging.rst","path":"logging.rst","contentType":"file"},{"name":"mailer.rst","path":"mailer.rst","contentType":"file"},{"name":"mercure.rst","path":"mercure.rst","contentType":"file"},{"name":"messenger.rst","path":"messenger.rst","contentType":"file"},{"name":"migration.rst","path":"migration.rst","contentType":"file"},{"name":"notifier.rst","path":"notifier.rst","contentType":"file"},{"name":"page_creation.rst","path":"page_creation.rst","contentType":"file"},{"name":"performance.rst","path":"performance.rst","contentType":"file"},{"name":"profiler.rst","path":"profiler.rst","contentType":"file"},{"name":"rate_limiter.rst","path":"rate_limiter.rst","contentType":"file"},{"name":"routing.rst","path":"routing.rst","contentType":"file"},{"name":"scheduler.rst","path":"scheduler.rst","contentType":"file"},{"name":"security.rst","path":"security.rst","contentType":"file"},{"name":"serializer.rst","path":"serializer.rst","contentType":"file"},{"name":"service_container.rst","path":"service_container.rst","contentType":"file"},{"name":"session.rst","path":"session.rst","contentType":"file"},{"name":"setup.rst","path":"setup.rst","contentType":"file"},{"name":"string.rst","path":"string.rst","contentType":"file"},{"name":"templates.rst","path":"templates.rst","contentType":"file"},{"name":"testing.rst","path":"testing.rst","contentType":"file"},{"name":"translation.rst","path":"translation.rst","contentType":"file"},{"name":"validation.rst","path":"validation.rst","contentType":"file"},{"name":"web_link.rst","path":"web_link.rst","contentType":"file"},{"name":"webhook.rst","path":"webhook.rst","contentType":"file"},{"name":"workflow.rst","path":"workflow.rst","contentType":"file"}],"totalCount":78}},"fileTreeProcessingTime":4.097893,"foldersToFetch":[],"incompleteFileTree":false,"repo":{"id":521583,"defaultBranch":"7.3","name":"symfony-docs","ownerLogin":"symfony","currentUserCanPush":false,"isFork":false,"isEmpty":false,"createdAt":"2010-02-17T08:43:51.000Z","ownerAvatar":"https://avatars.githubusercontent.com/u/143937?v=4","public":true,"private":false,"isOrgOwned":true},"codeLineWrapEnabled":false,"symbolsExpanded":false,"treeExpanded":true,"refInfo":{"name":"7.1","listCacheKey":"v0:1740861846.0","canEdit":false,"refType":"branch","currentOid":"537d2d6dc65a1bd27525092f2b6574f0dbad941a"},"path":"lock.rst","currentUser":null,"blob":{"rawLines":null,"stylingDirectives":null,"colorizedLines":null,"csv":null,"csvError":null,"dependabotInfo":{"showConfigurationBanner":false,"configFilePath":null,"networkDependabotPath":"/symfony/symfony-docs/network/updates","dismissConfigurationNoticePath":"/settings/dismiss-notice/dependabot_configuration_notice","configurationNoticeDismissed":null},"displayName":"lock.rst","displayUrl":"https://github.com/symfony/symfony-docs/blob/7.1/lock.rst?raw=true","headerInfo":{"blobSize":"11.4 KB","deleteTooltip":"You must be signed in to make or propose changes","editTooltip":"You must be signed in to make or propose changes","ghDesktopPath":"https://desktop.github.com","isGitLfs":false,"onBranch":true,"shortPath":"82ec48d","siteNavLoginPath":"/login?return_to=https%3A%2F%2Fgithub.com%2Fsymfony%2Fsymfony-docs%2Fblob%2F7.1%2Flock.rst","isCSV":false,"isRichtext":true,"toc":[{"level":1,"text":"Dealing with Concurrency with Locks","anchor":"dealing-with-concurrency-with-locks","htmlText":"Dealing with Concurrency with Locks"},{"level":2,"text":"Installing","anchor":"installing","htmlText":"Installing"},{"level":2,"text":"Configuring","anchor":"configuring","htmlText":"Configuring"},{"level":2,"text":"Locking a Resource","anchor":"locking-a-resource","htmlText":"Locking a Resource"},{"level":2,"text":"Locking a Dynamic Resource","anchor":"locking-a-dynamic-resource","htmlText":"Locking a Dynamic Resource"},{"level":2,"text":"Naming Locks","anchor":"naming-locks","htmlText":"Naming Locks"}],"lineInfo":{"truncatedLoc":"303","truncatedSloc":"228"},"mode":"file"},"image":false,"isCodeownersFile":null,"isPlain":false,"isValidLegacyIssueTemplate":false,"issueTemplate":null,"discussionTemplate":null,"language":"reStructuredText","languageID":419,"large":false,"planSupportInfo":{"repoIsFork":null,"repoOwnedByCurrentUser":null,"requestFullPath":"/symfony/symfony-docs/blob/7.1/lock.rst","showFreeOrgGatedFeatureMessage":null,"showPlanSupportBanner":null,"upgradeDataAttributes":null,"upgradePath":null},"publishBannersInfo":{"dismissActionNoticePath":"/settings/dismiss-notice/publish_action_from_dockerfile","releasePath":"/symfony/symfony-docs/releases/new?marketplace=true","showPublishActionBanner":false},"rawBlobUrl":"https://github.com/symfony/symfony-docs/raw/refs/heads/7.1/lock.rst","renderImageOrRaw":false,"richText":"\u003carticle class=\"markdown-body entry-content container-lg\" itemprop=\"text\"\u003e\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch1 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eDealing with Concurrency with Locks\u003c/h1\u003e\u003ca id=\"user-content-dealing-with-concurrency-with-locks\" class=\"anchor\" aria-label=\"Permalink: Dealing with Concurrency with Locks\" href=\"#dealing-with-concurrency-with-locks\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eWhen a program runs concurrently, some part of code which modify shared\nresources should not be accessed by multiple processes at the same time.\nSymfony's \u003ca href=\"#id1\"\u003e\u003cspan id=\"user-content-id2\"\u003e:doc:`Lock component \u0026lt;/components/lock\u0026gt;`\u003c/span\u003e\u003c/a\u003e provides a locking mechanism to ensure\nthat only one process is running the critical section of code at any point of\ntime to prevent race conditions from happening.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eThe following example shows a typical usage of the lock:\u003c/p\u003e\n\u003cpre\u003e$lock = $lockFactory-\u0026gt;createLock('pdf-creation');\nif (!$lock-\u0026gt;acquire()) {\n return;\n}\n\n// critical section of code\n$service-\u0026gt;method();\n\n$lock-\u0026gt;release();\n\u003c/pre\u003e\n\u003ca name=\"user-content-installing\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eInstalling\u003c/h2\u003e\u003ca id=\"user-content-installing\" class=\"anchor\" aria-label=\"Permalink: Installing\" href=\"#installing\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eIn applications using \u003ca href=\"#id3\"\u003e\u003cspan id=\"user-content-id4\"\u003e:ref:`Symfony Flex \u0026lt;symfony-flex\u0026gt;`\u003c/span\u003e\u003c/a\u003e, run this command to\ninstall the Lock component:\u003c/p\u003e\n\u003cpre lang=\"terminal\"\u003e$ composer require symfony/lock\n\u003c/pre\u003e\n\u003ca name=\"user-content-configuring\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eConfiguring\u003c/h2\u003e\u003ca id=\"user-content-configuring\" class=\"anchor\" aria-label=\"Permalink: Configuring\" href=\"#configuring\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eBy default, Symfony provides a \u003ca href=\"#id5\"\u003e\u003cspan id=\"user-content-id6\"\u003e:ref:`Semaphore \u0026lt;lock-store-semaphore\u0026gt;`\u003c/span\u003e\u003c/a\u003e\nwhen available, or a \u003ca href=\"#id7\"\u003e\u003cspan id=\"user-content-id8\"\u003e:ref:`Flock \u0026lt;lock-store-flock\u0026gt;`\u003c/span\u003e\u003c/a\u003e otherwise. You can configure\nthis behavior by using the \u003ccode\u003elock\u003c/code\u003e key like:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: yaml\n\n # config/packages/lock.yaml\n framework:\n lock: ~\n lock: 'flock'\n lock: 'flock:///path/to/file'\n lock: 'semaphore'\n lock: 'memcached://m1.docker'\n lock: ['memcached://m1.docker', 'memcached://m2.docker']\n lock: 'redis://r1.docker'\n lock: ['redis://r1.docker', 'redis://r2.docker']\n lock: 'rediss://r1.docker?ssl[verify_peer]=1\u0026amp;ssl[cafile]=...'\n lock: 'zookeeper://z1.docker'\n lock: 'zookeeper://z1.docker,z2.docker'\n lock: 'zookeeper://localhost01,localhost02:2181'\n lock: 'sqlite:///%kernel.project_dir%/var/lock.db'\n lock: 'mysql:host=127.0.0.1;dbname=app'\n lock: 'pgsql:host=127.0.0.1;dbname=app'\n lock: 'pgsql+advisory:host=127.0.0.1;dbname=app'\n lock: 'sqlsrv:server=127.0.0.1;Database=app'\n lock: 'oci:host=127.0.0.1;dbname=app'\n lock: 'mongodb://127.0.0.1/app?collection=lock'\n lock: '%env(LOCK_DSN)%'\n\n # named locks\n lock:\n invoice: ['semaphore', 'redis://r2.docker']\n report: 'semaphore'\n\n .. code-block:: xml\n\n \u0026lt;!-- config/packages/lock.xml --\u0026gt;\n \u0026lt;?xml version=\"1.0\" encoding=\"UTF-8\" ?\u0026gt;\n \u0026lt;container xmlns=\"http://symfony.com/schema/dic/services\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xmlns:framework=\"http://symfony.com/schema/dic/symfony\"\n xsi:schemaLocation=\"http://symfony.com/schema/dic/services\n https://symfony.com/schema/dic/services/services-1.0.xsd\n http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd\"\u0026gt;\n\n \u0026lt;framework:config\u0026gt;\n \u0026lt;framework:lock\u0026gt;\n \u0026lt;framework:resource\u0026gt;flock\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;flock:///path/to/file\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;semaphore\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;memcached://m1.docker\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;memcached://m1.docker\u0026lt;/framework:resource\u0026gt;\n \u0026lt;framework:resource\u0026gt;memcached://m2.docker\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;redis://r1.docker\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;redis://r1.docker\u0026lt;/framework:resource\u0026gt;\n \u0026lt;framework:resource\u0026gt;redis://r2.docker\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;zookeeper://z1.docker\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;zookeeper://z1.docker,z2.docker\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;zookeeper://localhost01,localhost02:2181\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;sqlite:///%kernel.project_dir%/var/lock.db\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;mysql:host=127.0.0.1;dbname=app\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;pgsql:host=127.0.0.1;dbname=app\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;pgsql+advisory:host=127.0.0.1;dbname=app\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;sqlsrv:server=127.0.0.1;Database=app\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;oci:host=127.0.0.1;dbname=app\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;mongodb://127.0.0.1/app?collection=lock\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;framework:resource\u0026gt;%env(LOCK_DSN)%\u0026lt;/framework:resource\u0026gt;\n\n \u0026lt;!-- named locks --\u0026gt;\n \u0026lt;framework:resource name=\"invoice\"\u0026gt;semaphore\u0026lt;/framework:resource\u0026gt;\n \u0026lt;framework:resource name=\"invoice\"\u0026gt;redis://r2.docker\u0026lt;/framework:resource\u0026gt;\n \u0026lt;framework:resource name=\"report\"\u0026gt;semaphore\u0026lt;/framework:resource\u0026gt;\n \u0026lt;/framework:lock\u0026gt;\n \u0026lt;/framework:config\u0026gt;\n \u0026lt;/container\u0026gt;\n\n .. code-block:: php\n\n // config/packages/lock.php\n use Symfony\\Config\\FrameworkConfig;\n\n return static function (FrameworkConfig $framework): void {\n $framework-\u0026gt;lock()\n -\u0026gt;resource('default', ['flock'])\n -\u0026gt;resource('default', ['flock:///path/to/file'])\n -\u0026gt;resource('default', ['semaphore'])\n -\u0026gt;resource('default', ['memcached://m1.docker'])\n -\u0026gt;resource('default', ['memcached://m1.docker', 'memcached://m2.docker'])\n -\u0026gt;resource('default', ['redis://r1.docker'])\n -\u0026gt;resource('default', ['redis://r1.docker', 'redis://r2.docker'])\n -\u0026gt;resource('default', ['zookeeper://z1.docker'])\n -\u0026gt;resource('default', ['zookeeper://z1.docker,z2.docker'])\n -\u0026gt;resource('default', ['zookeeper://localhost01,localhost02:2181'])\n -\u0026gt;resource('default', ['sqlite:///%kernel.project_dir%/var/lock.db'])\n -\u0026gt;resource('default', ['mysql:host=127.0.0.1;dbname=app'])\n -\u0026gt;resource('default', ['pgsql:host=127.0.0.1;dbname=app'])\n -\u0026gt;resource('default', ['pgsql+advisory:host=127.0.0.1;dbname=app'])\n -\u0026gt;resource('default', ['sqlsrv:server=127.0.0.1;Database=app'])\n -\u0026gt;resource('default', ['oci:host=127.0.0.1;dbname=app'])\n -\u0026gt;resource('default', ['mongodb://127.0.0.1/app?collection=lock'])\n -\u0026gt;resource('default', [env('LOCK_DSN')])\n\n // named locks\n -\u0026gt;resource('invoice', ['semaphore', 'redis://r2.docker'])\n -\u0026gt;resource('report', ['semaphore'])\n ;\n };\n\n\u003c/pre\u003e\n\u003ca name=\"user-content-locking-a-resource\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eLocking a Resource\u003c/h2\u003e\u003ca id=\"user-content-locking-a-resource\" class=\"anchor\" aria-label=\"Permalink: Locking a Resource\" href=\"#locking-a-resource\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eTo lock the default resource, autowire the lock factory using\n\u003ca href=\"#id9\"\u003e\u003cspan id=\"user-content-id10\"\u003e:class:`Symfony\\\\Component\\\\Lock\\\\LockFactory`\u003c/span\u003e\u003c/a\u003e:\u003c/p\u003e\n\u003cpre\u003e// src/Controller/PdfController.php\nnamespace App\\Controller;\n\nuse Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController;\nuse Symfony\\Component\\HttpFoundation\\Response;\nuse Symfony\\Component\\Lock\\LockFactory;\n\nclass PdfController extends AbstractController\n{\n #[Route('/download/terms-of-use.pdf')]\n public function downloadPdf(LockFactory $factory, MyPdfGeneratorService $pdf): Response\n {\n $lock = $factory-\u0026gt;createLock('pdf-creation');\n $lock-\u0026gt;acquire(true);\n\n // heavy computation\n $myPdf = $pdf-\u0026gt;getOrCreatePdf();\n\n $lock-\u0026gt;release();\n\n // ...\n }\n}\n\u003c/pre\u003e\n\u003cdiv dir=\"auto\"\u003e\n\u003cp dir=\"auto\"\u003eWarning\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eThe same instance of \u003ccode\u003eLockInterface\u003c/code\u003e won't block when calling \u003ccode\u003eacquire\u003c/code\u003e\nmultiple times inside the same process. When several services use the\nsame lock, inject the \u003ccode\u003eLockFactory\u003c/code\u003e instead to create a separate lock\ninstance for each service.\u003c/p\u003e\n\u003c/div\u003e\n\u003ca name=\"user-content-locking-a-dynamic-resource\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eLocking a Dynamic Resource\u003c/h2\u003e\u003ca id=\"user-content-locking-a-dynamic-resource\" class=\"anchor\" aria-label=\"Permalink: Locking a Dynamic Resource\" href=\"#locking-a-dynamic-resource\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eSometimes the application is able to cut the resource into small pieces in order\nto lock a small subset of processes and let others through. The previous example\nshowed how to lock the \u003ccode\u003e$pdf-\u0026gt;getOrCreatePdf()\u003c/code\u003e call for everybody,\nnow let's see how to lock a \u003ccode\u003e$pdf-\u0026gt;getOrCreatePdf($version)\u003c/code\u003e call only for\nprocesses asking for the same \u003ccode\u003e$version\u003c/code\u003e:\u003c/p\u003e\n\u003cpre\u003e// src/Controller/PdfController.php\nnamespace App\\Controller;\n\nuse Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController;\nuse Symfony\\Component\\HttpFoundation\\Response;\nuse Symfony\\Component\\Lock\\LockFactory;\n\nclass PdfController extends AbstractController\n{\n #[Route('/download/{version}/terms-of-use.pdf')]\n public function downloadPdf($version, LockFactory $lockFactory, MyPdfGeneratorService $pdf): Response\n {\n $lock = $lockFactory-\u0026gt;createLock('pdf-creation-'.$version);\n $lock-\u0026gt;acquire(true);\n\n // heavy computation\n $myPdf = $pdf-\u0026gt;getOrCreatePdf($version);\n\n $lock-\u0026gt;release();\n\n // ...\n }\n}\n\u003c/pre\u003e\n\u003ca name=\"user-content-naming-locks\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eNaming Locks\u003c/h2\u003e\u003ca id=\"user-content-naming-locks\" class=\"anchor\" aria-label=\"Permalink: Naming Locks\" href=\"#naming-locks\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eIf the application needs different kind of Stores alongside each other, Symfony\nprovides \u003ca href=\"#id11\"\u003e\u003cspan id=\"user-content-id12\"\u003e:ref:`named lock \u0026lt;reference-lock-resources-name\u0026gt;`\u003c/span\u003e\u003c/a\u003e:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: yaml\n\n # config/packages/lock.yaml\n framework:\n lock:\n invoice: ['semaphore', 'redis://r2.docker']\n report: 'semaphore'\n\n .. code-block:: xml\n\n \u0026lt;!-- config/packages/lock.xml --\u0026gt;\n \u0026lt;?xml version=\"1.0\" encoding=\"UTF-8\" ?\u0026gt;\n \u0026lt;container xmlns=\"http://symfony.com/schema/dic/services\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xmlns:framework=\"http://symfony.com/schema/dic/symfony\"\n xsi:schemaLocation=\"http://symfony.com/schema/dic/services\n https://symfony.com/schema/dic/services/services-1.0.xsd\n http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd\"\u0026gt;\n\n \u0026lt;framework:config\u0026gt;\n \u0026lt;framework:lock\u0026gt;\n \u0026lt;framework:resource name=\"invoice\"\u0026gt;semaphore\u0026lt;/framework:resource\u0026gt;\n \u0026lt;framework:resource name=\"invoice\"\u0026gt;redis://r2.docker\u0026lt;/framework:resource\u0026gt;\n \u0026lt;framework:resource name=\"report\"\u0026gt;semaphore\u0026lt;/framework:resource\u0026gt;\n \u0026lt;/framework:lock\u0026gt;\n \u0026lt;/framework:config\u0026gt;\n \u0026lt;/container\u0026gt;\n\n .. code-block:: php\n\n // config/packages/lock.php\n use Symfony\\Config\\FrameworkConfig;\n\n return static function (FrameworkConfig $framework): void {\n $framework-\u0026gt;lock()\n -\u0026gt;resource('invoice', ['semaphore', 'redis://r2.docker'])\n -\u0026gt;resource('report', ['semaphore']);\n ;\n };\n\n\u003c/pre\u003e\n\u003cp dir=\"auto\"\u003eAn autowiring alias is created for each named lock with a name using the camel\ncase version of its name suffixed by \u003ccode\u003eLockFactory\u003c/code\u003e.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eFor instance, the \u003ccode\u003einvoice\u003c/code\u003e lock can be injected by naming the argument\n\u003ccode\u003e$invoiceLockFactory\u003c/code\u003e and type-hinting it with\n\u003ca href=\"#id13\"\u003e\u003cspan id=\"user-content-id14\"\u003e:class:`Symfony\\\\Component\\\\Lock\\\\LockFactory`\u003c/span\u003e\u003c/a\u003e:\u003c/p\u003e\n\u003cpre\u003e// src/Controller/PdfController.php\nnamespace App\\Controller;\n\nuse Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController;\nuse Symfony\\Component\\HttpFoundation\\Response;\nuse Symfony\\Component\\Lock\\LockFactory;\n\nclass PdfController extends AbstractController\n{\n #[Route('/download/terms-of-use.pdf')]\n public function downloadPdf(LockFactory $invoiceLockFactory, MyPdfGeneratorService $pdf): Response\n {\n // ...\n }\n}\n\u003c/pre\u003e\n\n\u003c/article\u003e","renderedFileInfo":null,"shortPath":null,"symbolsEnabled":true,"tabSize":4,"topBannersInfo":{"overridingGlobalFundingFile":false,"globalPreferredFundingPath":"/symfony/.github/blob/6f2ca452c856184a28812bb364b4e34ed50309da/FUNDING.yml","showInvalidCitationWarning":false,"citationHelpUrl":"https://docs.github.com/github/creating-cloning-and-archiving-repositories/creating-a-repository-on-github/about-citation-files","actionsOnboardingTip":null},"truncated":false,"viewable":true,"workflowRedirectUrl":null,"symbols":{"timed_out":false,"not_analyzed":true,"symbols":[]}},"copilotInfo":null,"copilotAccessAllowed":false,"modelsAccessAllowed":false,"modelsRepoIntegrationEnabled":false,"csrf_tokens":{"/symfony/symfony-docs/branches":{"post":"7AfygJptQCxSZ7vhmPtCDQPdC9uJcE4C-cJWtrJZVVJbtsA4wUbgTGFLYCkCPLGU_i_IMQcfTifcETDY-qGPvw"},"/repos/preferences":{"post":"-fAY6dbu0yqBD1w-ePI3jh1E_UKNTmhSpBUHoH_Vg7WRN-rO92Fu1lIIayzBvRodlIooF9cleoVXHhY8B8t-JQ"}}},"title":"symfony-docs/lock.rst at 7.1 · symfony/symfony-docs","appPayload":{"helpUrl":"https://docs.github.com","findFileWorkerPath":"/assets-cdn/worker/find-file-worker-7d7eb7c71814.js","findInFileWorkerPath":"/assets-cdn/worker/find-in-file-worker-1ae9fa256942.js","githubDevUrl":null,"enabled_features":{"code_nav_ui_events":false,"react_blob_overlay":false,"accessible_code_button":true,"github_models_repo_integration":false}}}
0