memq 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in memq.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
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,89 @@
1
+ # Memq
2
+
3
+ Memq is a lightweight simple queue solution, based on Memcached and Dalli. It's a port of this https://github.com/abhinavsingh/memq.git PHP MEMQ class, with some minor changes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'memq'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install memq
18
+
19
+ Memq will attempt to install latest version of dalli gem automaticly.
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'memq'
25
+
26
+ mem = Memq::Queue.new
27
+ ```
28
+ Above code will initialize Queue object and attempt to create a default queue with a key "default".
29
+
30
+ If you wish, you can pass some dalli options, and a queue key name:
31
+
32
+ ```ruby
33
+ mem = Memq::Queue.new "mail", "localhost:11211", {:compress => true}
34
+ ```
35
+ Enqueue something. The method will return queue position..
36
+ ```ruby
37
+ mem.enqueue "Hello world!" # => 0
38
+ mem.enqueue "Aloha world!" # => 1
39
+ mem.total # => 2
40
+ ```
41
+ Call mem.total to see queue length
42
+
43
+ Dequeue.
44
+ ```ruby
45
+ mem.dequeue # => {"mail_0"=>"Hello world!"}
46
+ ```
47
+ The keys are "queuename_position"
48
+
49
+ Enqueue some more
50
+ ```ruby
51
+ mem.enqueue [1,2,3,4,5] # => 2
52
+ mem.enqueue({:hello => 'word', :aloha => 'world'}) # => 3
53
+ mem.total # => 3
54
+ ```
55
+
56
+ Dequeue all in a row
57
+ ```ruby
58
+ mem.dequeue mem.total
59
+ # => {"mail_1"=>"Aloha world!", "mail_2"=>[1, 2, 3, 4, 5], "mail_3"=>{:hello=>"word", :aloha=>"world"}}
60
+ mem.total # => 0
61
+ ```
62
+ Please keep in mind that dequeing currently doesn't actually delete your queued items.
63
+ It only ajusts queue head and tail pointers. So, you can access an already dequeued item directly:
64
+ ```ruby
65
+ mem.client.get "mail_0" # => "Hello world!"
66
+ ```
67
+ mem.client is just a Dalli client instance, so it can do everything Dalli client can.
68
+
69
+ By default the queue is named "default". You can create and manipulate other queues by single object instance:
70
+ ```ruby
71
+ mem.use "process" # => ["process", 0, 0]
72
+ ```
73
+ will create "process" queue if it doesn't exist and switch using it. The method returns an array with queue name,
74
+ head and tail pointer values. If called without arguments, it will return this array for current queue.
75
+
76
+ Also you can reset your queue, check if queue exists and if queue is empty:
77
+ ```ruby
78
+ mem.rst # => true
79
+ mem.exists? # => true
80
+ mem.empty? # => true
81
+ ```
82
+
83
+ ## Contributing
84
+
85
+ 1. Fork it
86
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
87
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
88
+ 4. Push to the branch (`git push origin my-new-feature`)
89
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/lib/memq.rb ADDED
@@ -0,0 +1,77 @@
1
+ require "memq/version"
2
+ require "dalli"
3
+
4
+ module Memq
5
+ class Queue
6
+
7
+ attr_reader :client, :head, :tail
8
+
9
+ def initialize(q = "default", server = "localhost:11211",params = {})
10
+ @client = Dalli::Client.new(server,{:namespace => "queue_"}.merge(params))
11
+ self.use q
12
+ end
13
+
14
+ def empty?(q = @q)
15
+ raise "Queue '#{q}'' doesn't exist. Use 'rst' method to create." if !self.exists?(q)
16
+ head,tail = @client.get("#{q}_head"), @client.get("#{q}_tail")
17
+ if self.exists?(q) && (head.zero? && tail.zero? || (head == tail))
18
+ return true
19
+ end
20
+ return false
21
+ end
22
+
23
+ def exists?(q = @q)
24
+ @head = @client.get "#{q}_head"
25
+ @tail = @client.get "#{q}_tail"
26
+ if !@head.is_a?(Fixnum) || !@tail.is_a?(Fixnum) || @head.to_i > @tail.to_i
27
+ return false
28
+ end
29
+ return true
30
+ end
31
+
32
+ def rst(q = @q)
33
+ @client.set "#{q}_head", 0
34
+ @client.set "#{q}_tail", 0
35
+ @head,@tail = 0,0
36
+ return true
37
+ end
38
+
39
+ def dequeue(r = 1,q = @q)
40
+ return nil if self.empty?
41
+ if r.is_a?(Fixnum)
42
+ if (@tail+1) - (r + @head) > 0
43
+ keys = (@head...(@head+r)).to_a
44
+ @client.set "#{q}_head", @head+=r
45
+ self.rst if @head == @tail
46
+ else
47
+ keys = (@head...@tail).to_a
48
+ self.rst q
49
+ end
50
+ @client.get_multi keys.map{|e| "#{q}_#{e}" }
51
+ else
52
+ raise "Incorrect keynum"
53
+ end
54
+ end
55
+
56
+ def enqueue(v,q = @q)
57
+ @client.set "#{q}_#{@tail}", v
58
+ @client.set "#{q}_tail", @tail+=1
59
+ @tail-1
60
+ end
61
+
62
+ def total(q = @q)
63
+ @tail-@head
64
+ end
65
+
66
+ def use(q = nil)
67
+ if !q.nil?
68
+ self.rst q if !self.exists? q
69
+ @q = q
70
+ @head = @client.get "#{q}_head"
71
+ @tail = @client.get "#{q}_tail"
72
+ end
73
+ [@q,@head,@tail]
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ module Memq
2
+ VERSION = "0.0.1"
3
+ end
data/memq.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/memq/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["SilentShade"]
6
+ gem.email = ["lifeemulation@gmail.com"]
7
+ gem.description = %q{Simple and lightweight memcached based queue solution}
8
+ gem.summary = %q{This gem is a port from PHP MEMQ class}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "memq"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Memq::VERSION
17
+
18
+ gem.add_dependency "dalli"
19
+ gem.add_development_dependency "rspec"
20
+
21
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: memq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - SilentShade
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: dalli
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Simple and lightweight memcached based queue solution
47
+ email:
48
+ - lifeemulation@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - lib/memq.rb
59
+ - lib/memq/version.rb
60
+ - memq.gemspec
61
+ homepage: ''
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 1.8.24
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: This gem is a port from PHP MEMQ class
85
+ test_files: []