redis_lock 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/Gemfile +8 -0
- data/README.md +67 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/redis_lock/config.rb +20 -0
- data/lib/redis_lock/version.rb +3 -0
- data/lib/redis_lock.rb +66 -0
- data/redis_lock.gemspec +27 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ee826bd92ec9c79acb13dd5abd4c25cae2b58740
|
4
|
+
data.tar.gz: 8111863df02cc6c004e9ae65cbdfa5106fc7eb89
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 658f04d297764af0e0fe29739972eacd3aebeeb43334c59d51fe070ca86e7455afa75e6c4b3ff6fe1a6b44d7327cf00ee39443295ef793a7080756dfa97d0697
|
7
|
+
data.tar.gz: e7b3e9954828b0fc0d53c15b35476561ecfbf52d201e7977baeeadb4c66861bf677f495bc5334964d34a089eb148470020b42b0571953886b4280447ec35478f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# RedisLock
|
2
|
+
|
3
|
+
Do not allow anyone to perfor de same operation while this is running.
|
4
|
+
Do not perform this operation unless the previous was executed in more than 5 minutes ago.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'redis_lock'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install redis_lock
|
21
|
+
|
22
|
+
## Setup
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
RedisLock.setup do |config|
|
26
|
+
config.redis = { host: '127.0.0.1'
|
27
|
+
port: 6379
|
28
|
+
db: 2 }
|
29
|
+
config.logger = Rails.logger
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
lock = RedisLock.new('my_key')
|
37
|
+
|
38
|
+
lock.locked? #=> false
|
39
|
+
# Add 20 secs time to live (TTL)
|
40
|
+
lock.set(20) #=> true
|
41
|
+
lock.locked? #=> true
|
42
|
+
lock.remove #=> true
|
43
|
+
lock.locked? #=> false
|
44
|
+
```
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
lock = RedisLock.new('my_key')
|
48
|
+
out = subject.perform do
|
49
|
+
#no one can perform the same operation while this is running
|
50
|
+
{}.tap do |t|
|
51
|
+
t[:locked?] = subject.locked?
|
52
|
+
end
|
53
|
+
end
|
54
|
+
out[:locked?] #=> true
|
55
|
+
# once the block has finished releases the lock
|
56
|
+
lock.locked? #=> false
|
57
|
+
```
|
58
|
+
|
59
|
+
## Development
|
60
|
+
|
61
|
+
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.
|
62
|
+
|
63
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/arturictus/redis_lock.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "redis_lock"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
class RedisLock
|
2
|
+
class Config
|
3
|
+
def redis=(hash = {})
|
4
|
+
@redis = hash
|
5
|
+
end
|
6
|
+
|
7
|
+
def redis
|
8
|
+
fail "[RedisLock::Config] redis connection setup is not set" unless @redis
|
9
|
+
@redis
|
10
|
+
end
|
11
|
+
|
12
|
+
def logger=(logger)
|
13
|
+
@logger = logger
|
14
|
+
end
|
15
|
+
|
16
|
+
def logger
|
17
|
+
@logger ? @logger : Logger.new(STDOUT)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/redis_lock.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'redis'
|
2
|
+
require "redis_lock/version"
|
3
|
+
require "redis_lock/config"
|
4
|
+
|
5
|
+
class RedisLock
|
6
|
+
attr_reader :key
|
7
|
+
|
8
|
+
def self.config
|
9
|
+
@config ||= Config.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.setup
|
13
|
+
yield config
|
14
|
+
end
|
15
|
+
|
16
|
+
def config; self.class.config; end
|
17
|
+
|
18
|
+
def initialize(key, opts = {})
|
19
|
+
@key = key
|
20
|
+
@redis = opts[:redis]
|
21
|
+
end
|
22
|
+
|
23
|
+
def redis
|
24
|
+
@redis ||= Redis.new(config.redis)
|
25
|
+
end
|
26
|
+
|
27
|
+
def set(expiration_time = 600)
|
28
|
+
redis.set(
|
29
|
+
key,
|
30
|
+
Time.now.strftime('%FT%T'),
|
31
|
+
ex: expiration_time, # expires in X seconds
|
32
|
+
nx: true # only if it does not exists
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
def perform(args = {}, &block)
|
37
|
+
return if locked?
|
38
|
+
expiration = args[:expiration] || args[:ex] || 600
|
39
|
+
set(expiration)
|
40
|
+
# If error occurs, we remove the lock
|
41
|
+
out = _perform(&block)
|
42
|
+
remove
|
43
|
+
out
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
def locked?
|
49
|
+
redis.ttl(key) == -2 ? false : true
|
50
|
+
end
|
51
|
+
alias_method :exists?, :locked?
|
52
|
+
|
53
|
+
def remove
|
54
|
+
redis.del(key) == 1 ? true : false
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def _perform(&block)
|
60
|
+
yield self
|
61
|
+
rescue => e
|
62
|
+
config.logger.error "[RedisLock] key: `#{key}` error:"
|
63
|
+
config.logger.error e
|
64
|
+
false
|
65
|
+
end
|
66
|
+
end
|
data/redis_lock.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "redis_lock/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "redis_lock"
|
8
|
+
spec.version = RedisLock::VERSION
|
9
|
+
spec.authors = ["Artur Pañach"]
|
10
|
+
spec.email = ["arturictus@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Lock with redis}
|
13
|
+
spec.description = %q{Lock with redis}
|
14
|
+
spec.homepage = "https://github.com/arturictus/redis_lock.git"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.6"
|
26
|
+
spec.add_dependency "redis", "~> 3"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis_lock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Artur Pañach
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.6'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.6'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: redis
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3'
|
69
|
+
description: Lock with redis
|
70
|
+
email:
|
71
|
+
- arturictus@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- Gemfile
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- bin/console
|
82
|
+
- bin/setup
|
83
|
+
- lib/redis_lock.rb
|
84
|
+
- lib/redis_lock/config.rb
|
85
|
+
- lib/redis_lock/version.rb
|
86
|
+
- redis_lock.gemspec
|
87
|
+
homepage: https://github.com/arturictus/redis_lock.git
|
88
|
+
licenses: []
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.4.5
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Lock with redis
|
110
|
+
test_files: []
|