代码实现及分析
可用于设定单台openresty实例上的互斥锁,下面简单分析下代码实现
互斥锁是利用shared.dict进程数据共享及所有操作都是原子操作的特点实现。
|
|
缓存锁使用示例
The basic workflow for a cache lock is as follows:
- Check the cache for a hit with the key. If a cache miss happens, proceed to step 2.
- Instantiate a resty.lock object, call the lock method on the key, and check the 1st return value, i.e., the lock waiting time. If it is nil, handle the error; otherwise proceed to step 3.
- Check the cache again for a hit. If it is still a miss, proceed to step 4; otherwise release the lock by calling unlock and then return the cached value.
- Query the backend (the data source) for the value, put the result into the cache, and then release the lock currently held by calling unlock.
Below is a kinda complete code example that demonstrates the idea.
|
|