redis-unique-queue 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +102 -0
- data/Rakefile +1 -0
- data/lib/redis/unique/queue.rb +76 -0
- data/redis-unique-queue.gemspec +24 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 08e6d28a812e7ea4f3bbd69649e692f0ded84def
|
4
|
+
data.tar.gz: 2569d5650a87dac3266157b1f2311ec3d166ffa3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 36fa4528e09fc89e6d93e246f8fc16cc74e5f40d7d9243da87657446da6e32066b78f9f1f01f37875bdd5eed84bfa777c04009625184ed9e8e1f3ea71f88ca85
|
7
|
+
data.tar.gz: 387f34f683a67af91e6d3d111957f703f5a39e7fabd297b458ce6ddc82cb78b62de32df46f92db8516109ae74cfe500c14cbe4556b8f016fe515257932056ec6
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Misha Conway
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# Redis::Unique::Queue
|
2
|
+
|
3
|
+
A unique FIFO queue with atomic operations built on top of Redis. Useful if you want to enqueue data without worrying about it existing multiple times in the queue.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'redis-unique-queue'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install redis-unique-queue
|
19
|
+
|
20
|
+
## Getting started
|
21
|
+
|
22
|
+
You can instantiate a named queue using your default Redis configuration.
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
q = Redis::Unique::Queue.new 'jobs'
|
26
|
+
```
|
27
|
+
|
28
|
+
Or you can pass in your own instance of the Redis class.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
q = Redis::Unique::Queue.new 'jobs', Redis.new(:host => "10.0.1.1", :port => 6380, :db => 15)
|
32
|
+
```
|
33
|
+
|
34
|
+
A third option is to instead pass your Redis configurations.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
q = Redis::Unique::Queue.new 'jobs', :host => "10.0.1.1", :port => 6380, :db => 15
|
38
|
+
```
|
39
|
+
|
40
|
+
## Using the queue
|
41
|
+
|
42
|
+
You can push data to the queue.
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
q.push "hello"
|
46
|
+
q.push "world"
|
47
|
+
q.push "hello" # the item 'hello' will only exist once in the queue since it is unique
|
48
|
+
```
|
49
|
+
|
50
|
+
You can pop data from the queue.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
result = q.pop
|
54
|
+
```
|
55
|
+
|
56
|
+
You can get the size of the queue.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
q.size
|
60
|
+
```
|
61
|
+
|
62
|
+
You can read the first item in the queue.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
q.front
|
66
|
+
```
|
67
|
+
|
68
|
+
You can see if an item exists in the queue.
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
q.include? "hello"
|
72
|
+
```
|
73
|
+
|
74
|
+
You can remove an arbitrary item from the queue. Note that it doesn't have to be the first item.
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
q.remove "world"
|
78
|
+
```
|
79
|
+
|
80
|
+
You can get all items in the queue.
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
q.all
|
84
|
+
```
|
85
|
+
|
86
|
+
You can also peek into arbitrary ranges in the queue.
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
q.peek 1 #read the item at index 1
|
90
|
+
q.peek 23 #read the item at index 23
|
91
|
+
q.peek 10, 5 #peek at five items starting at index 10
|
92
|
+
```
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
## Contributing
|
97
|
+
|
98
|
+
1. Fork it ( http://github.com/<my-github-username>/redis-unique-queue/fork )
|
99
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
100
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
101
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
102
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require "redis"
|
2
|
+
|
3
|
+
class Redis
|
4
|
+
module Unique
|
5
|
+
class Queue
|
6
|
+
attr_reader :name
|
7
|
+
|
8
|
+
VERSION = "0.0.3"
|
9
|
+
|
10
|
+
def initialize(name, redis_or_options = {})
|
11
|
+
@name = name
|
12
|
+
@redis = if redis_or_options.kind_of? Redis
|
13
|
+
redis_or_options
|
14
|
+
elsif redis_or_options.kind_of? Hash
|
15
|
+
Redis.new redis_or_options
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
#Add an item to the queue. delay is in seconds.
|
20
|
+
def push data, delay=0
|
21
|
+
score = Time.now.to_f + delay
|
22
|
+
@redis.zadd(name, score, data)
|
23
|
+
end
|
24
|
+
|
25
|
+
def pop
|
26
|
+
begin
|
27
|
+
success, result = attempt_atomic_pop
|
28
|
+
end while !success && result
|
29
|
+
result
|
30
|
+
end
|
31
|
+
|
32
|
+
def front
|
33
|
+
@redis.zrange(name, 0, 0).first
|
34
|
+
end
|
35
|
+
|
36
|
+
def remove data
|
37
|
+
@redis.zrem name, data
|
38
|
+
end
|
39
|
+
|
40
|
+
def size
|
41
|
+
@redis.zcard name
|
42
|
+
end
|
43
|
+
|
44
|
+
def all
|
45
|
+
peek 0, size
|
46
|
+
end
|
47
|
+
|
48
|
+
def peek index, amount=1
|
49
|
+
@redis.zrange name, index, index + amount - 1
|
50
|
+
end
|
51
|
+
|
52
|
+
def include? data
|
53
|
+
!@redis.zscore(name, data).nil?
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def attempt_atomic_pop
|
59
|
+
min_score = 0
|
60
|
+
max_score = Time.now.to_f
|
61
|
+
|
62
|
+
result = nil
|
63
|
+
success = @redis.watch(name) do
|
64
|
+
result = @redis.zrangebyscore(name, min_score, max_score, :with_scores => false, :limit => [0, 1]).first
|
65
|
+
if result
|
66
|
+
@redis.multi do |multi|
|
67
|
+
multi.zrem name, result
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
[success, result]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'redis/unique/queue'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "redis-unique-queue"
|
8
|
+
spec.version = Redis::Unique::Queue::VERSION
|
9
|
+
spec.authors = ["Misha Conway"]
|
10
|
+
spec.email = ["mishaAconway@gmail.com"]
|
11
|
+
spec.summary = %q{A unique queue with atomic operations implemented in Redis}
|
12
|
+
spec.description = %q{A unique queue with atomic operations implemented in Redis.}
|
13
|
+
spec.homepage = "https://github.com/MishaConway/ruby-redis-unique-queue"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_runtime_dependency 'redis'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis-unique-queue
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Misha Conway
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-16 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: redis
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: A unique queue with atomic operations implemented in Redis.
|
56
|
+
email:
|
57
|
+
- mishaAconway@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/redis/unique/queue.rb
|
68
|
+
- redis-unique-queue.gemspec
|
69
|
+
homepage: https://github.com/MishaConway/ruby-redis-unique-queue
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata: {}
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 2.2.2
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: A unique queue with atomic operations implemented in Redis
|
93
|
+
test_files: []
|