0
votes

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.

1

1 Answers

1
votes

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.