Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
688 views
in Technique[技术] by (71.8m points)

locking - Difference between owner release and forceRelease of Laravel Cache

I was reading Laravel Cache Documentation, and it said that a lock could be migrated to a job and then

  1. released after restoring the instance via the owner, like Cache::restoreLock('processing', $this->owner)->release();
  2. or doesn't respect to the current owner, like Cache::lock('processing')->forceRelease();

Which leads me to a question, what's the difference between them?, or maybe what are the use cases for each of them?

Anyone could explain it will be so much appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The difference is when you should use them.

The Cache::restoreLock('lock-name', $this->owner)->release() let's you retrieve the lock only if its the owner the one that's asking. This could be useful in cases in which your users can perform operations, but only them can cancel them. For example you could use this to cancel a mass-deletion operation in a mailing app. Only the user (the owner) is allowed to cancel a bulk delete cancelling the job and releasing its lock.

On the other hand, the forceRelease() let's you release a lock without taking into account who performs this operation. You have a good example of use in Mohamed Said's blog (here you can read it in full):

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...