Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.


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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...