RedlockForCollection
This is just a Redlock wrapper for collection of objects.
Also it used a connection pool for restrict the redis connections.
Installation
Add this line to your application's Gemfile:
gem 'redlock_for_collection'And then execute:
$ bundle
Or install it yourself as:
$ gem install redlock_for_collection
Usage
collection_manager = RedlockForCollection::Manager.new
options = { key_method: :to_s, key_prefix: 'pref', ttl: 20_000, min_validity: 10_00}
collection_manager.with_lock(objects, options: options) do |locked_objects, unlocked_objects|
... do some things with locked_objects
... do some things with unlocked_objects
endoptions
-
:ttltime to live of lock. -
:key_methodinvoked onobjectofobjects_collectionto generate shared key. -
:key_prefixprepended to the shared key - if lock validity <
:min_validitythen lock is treated as expired -
locked_objectsis successfully locked objects of collections -
unlocked_objectsis unsuccessfully locked objects of collections for any reasons
Configuration
collection_manager = RedlockForCollection::Manager.new
collection_manager.configure do |configuration|
config.pool_size = 2
config.redis_urls = ['redis://localhost:6379']
config.retry_delay = 2
config.retry_count = 2
endAlso you can provide a pool
manager = RedlockForCollection::Manager.new
redlock_pool = ConnectionPool.new { Redlock::Client.new([redis_urls]) }
manager.configure do |configuration|
config.pool = redlock_pool
endDevelopment
After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/CoolElvis/redlock_for_collection.