Laravel 缓存的所有者释放和强制释放之间的区别

Difference between owner release and forceRelease of Laravel Cache

我在看Laravel Cache Documentation,上面说一个锁可以迁移到一个job然后

  1. 通过所有者恢复实例后发布,例如Cache::restoreLock('processing', $this->owner)->release();
  2. 或者不尊重现在的主人,比如Cache::lock('processing')->forceRelease();

这让我想到了一个问题,what's the difference between them?,或者 what are the use cases for each of them?

任何人都可以解释一下,将不胜感激。

区别在于何时应该使用它们。

Cache::restoreLock('lock-name', $this->owner)->release() 让您仅在锁的所有者是请求者时取回锁。这在您的用户可以执行操作但只有他们可以取消操作的情况下很有用。例如,您可以使用它来取消邮件应用程序中的批量删除操作。仅允许用户(所有者)取消批量删除以取消作业并释放其锁定。

另一方面,forceRelease() 允许您释放锁而无需考虑谁执行此操作。您在 Mohamed Said 的博客中有一个很好的使用示例(here 您可以完整阅读):

Each time a product is updated, we want to dispatch a job that updates the search index. If several products were updated during a short period of time, we don't want to dispatch multiple jobs. There should be only a single index update job in the queue at any time.