README.md in redis_lock-0.2.0 vs README.md in redis_lock-0.3.0

- old
+ new

@@ -56,10 +56,15 @@ port: 6379 db: 2 } # logger # default: Logger.new(STDOUT) config.logger = Rails.logger + + # Default ttl for all your locks + # default: 60 + # + # config.default_ttl = 120 end ``` ## Usage @@ -71,57 +76,67 @@ lock.set(20) #=> true lock.locked? #=> true lock.remove #=> true lock.locked? #=> false ``` -__as Mutex__ + + +__semaphore:__ +No one can perform the same operation while this is running the rest of the processes +are waiting while the lock is in use, When lock is released another one takes the lock. + +args: + - key [string] + - opts: `{}` + * :redis + * :ttl, time to leave + * :set_opts, check `set` documentation + * :wait, time waiting for the next check if the lock is in use + ```ruby -lock = RedisLock.new('my_key') -out = lock.if_open do |l| - # no one can perform the same operation while this is running - l.set(30) # place the lock so no one else can perform this tasks +out = RedisLock.semaphore('my_key') do |l| sleep 3 # Do something - l.unlock! # release the lock :hello end out #=> :hello -lock.locked? #=> false +RedisLock.new('my_key').locked? #=> false ``` -__blocking for a time__ +__if_open:__ +**Use case:** Send email to user. The User should receive only 1 email per day ```ruby ttl = (24 * 3600) # one day -lock = RedisLock.new("User:1-sales-products") -lock.if_open do |l| +RedisLock.if_open("User:1-sales-products", ttl: ttl) do |l| # Send Email l.set(ttl) end ``` ## Methods: -__set__ +### set + Will store the key to redis with a ttl (time to live). args: - - ttl # default: 60 - - opts # default: {} - * value (String) - default: time now - * px (true) - miliseconds instead of seconds default: false - * nk (true) - Only set the key if it does not already exist. - * xx (true) - Only set the key if it already exist. + - __ttl__ | default: 60 + - __opts__ | default: {} + * __value__ (String) - default: time now + * __px__ - miliseconds instead of seconds | default: false + * __nk__ - Only set the key if it does not already exist. | default: false + * __xx__ - Only set the key if it already exist. | default: false ```ruby lock = RedisLock.new('my_key') lock.set(60) lock.ttl #=> 60 lock.open? # => false ``` -_with options_ +__with options:__ ```ruby lock = RedisLock.new('my_key') lock.set(60, nx: true) # only if the key does not exists @@ -129,20 +144,21 @@ lock.ttl #=> 60 lock.open? # => false ``` Redis documentation: https://redis.io/commands/set - +``` Set key to hold the string value. If key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation. EX seconds -- Set the specified expire time, in seconds. PX milliseconds -- Set the specified expire time, in milliseconds. NX -- Only set the key if it does not already exist. XX -- Only set the key if it already exist. +``` +### locked? -__locked?__ Returns `true` if lock is set ```ruby lock = RedisLock.new('my_key') lock.set(60) # => true (key has been stored) @@ -150,22 +166,24 @@ lock.remove lock.locked? # => false ``` _alias method:_ `exists?` -__open?__ +### open? + Returns `true` if NO lock is set ```ruby lock = RedisLock.new('my_key') lock.open? # => true lock.set(60) # => true (key has been stored) lock.open? # => false ``` _alias method:_ `unlocked?` -__delete__ +### delete + Removes the key from the Redis store ```ruby lock = RedisLock.new('my_key') lock.set(60) # => true (key has been stored) @@ -173,18 +191,20 @@ lock.delete lock.locked? # => false ``` _alias methods:_ `unlock!`,`open!` -__value__ +### value + Returns the value stored in redis ```ruby lock = RedisLock.new('my_key') lock.set(60, value: 'hi there!') lock.value # => 'hi there!' ``` -__ttl__ +### ttl + Returns the pending ttl (time to live) ```ruby lock = RedisLock.new('my_key') lock.set(60)