modis 1.0.0 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 636b12c6a5c346586ec5fed639ea9c7de44e18f8
4
- data.tar.gz: d8bb31a3245b62c556a8bcee449a3555cf8c2bb6
3
+ metadata.gz: c6359a4c5d347150ae7cb19754caf5398fd42d0d
4
+ data.tar.gz: 330ef4946885313394c85425599e4d1274cda2f8
5
5
  SHA512:
6
- metadata.gz: c379ef03514854bf7221a57c387635fa080c2cd97a9c75ed0620da4ee3166a4cd4ac32d66280f4e1e428679507d74fde8a2faaf2250be0eeb3c41728fe68ebfc
7
- data.tar.gz: 677634bb19c26bfa9bd9f6836629466b45c00f4b39112d06cb82b817707cbfedc41a7d2b875154b743090b8e6478222b5e82d3d8fbe37da49c40620101e7be54
6
+ metadata.gz: dd3275c3569475972681d14c3e1d877d12baf9a872367d4cf0700caf52bb587752414408aeb16afe4886e26a540b5fdcb86da64a0cc6dc3d07dc80b001c4c6e1
7
+ data.tar.gz: 8527e4159f4b2d7c4ab0c2874518f3de5e24a854b646a26ad5a75be8e4e3d457d47878798167eb6fb1c071c25993ef9984431f9fd13cbb657d350127578f9481
data/lib/modis/finders.rb CHANGED
@@ -11,10 +11,15 @@ module Modis
11
11
  end
12
12
 
13
13
  def all
14
- ids = Modis.redis.smembers(key_for(:all))
15
- records = Modis.redis.pipelined do
16
- ids.map { |id| Modis.redis.hgetall(key_for(id)) }
14
+ records = []
15
+
16
+ Modis.with_connection do |redis|
17
+ ids = redis.smembers(key_for(:all))
18
+ records = redis.pipelined do
19
+ ids.map { |id| redis.hgetall(key_for(id)) }
20
+ end
17
21
  end
22
+
18
23
  records.map do |record|
19
24
  klass = model_class(record)
20
25
  klass.new(record, new_record: false)
@@ -25,7 +30,7 @@ module Modis
25
30
  if id.nil?
26
31
  raise RecordNotFound, "Couldn't find #{name} without an ID"
27
32
  end
28
- values = Modis.redis.hgetall(key_for(id))
33
+ values = Modis.with_connection { |redis| redis.hgetall(key_for(id)) }
29
34
  unless values['id'].present?
30
35
  raise RecordNotFound, "Couldn't find #{name} with id=#{id}"
31
36
  end
@@ -86,10 +86,10 @@ module Modis
86
86
  end
87
87
 
88
88
  def destroy
89
- self.class.transaction do
89
+ self.class.transaction do |redis|
90
90
  run_callbacks :destroy do
91
- Modis.redis.del(key)
92
- untrack(id)
91
+ redis.del(key)
92
+ untrack(id, redis)
93
93
  end
94
94
  end
95
95
  end
@@ -126,14 +126,14 @@ module Modis
126
126
  future = nil
127
127
  set_id if new_record?
128
128
 
129
- self.class.transaction do
129
+ self.class.transaction do |redis|
130
130
  run_callbacks :save do
131
131
  callback = new_record? ? :create : :update
132
132
  run_callbacks callback do
133
133
  attrs = []
134
134
  attributes.each { |k, v| attrs << k << coerce_to_string(k, v) }
135
- future = Modis.redis.hmset(self.class.key_for(id), attrs)
136
- track(id) if new_record?
135
+ future = redis.hmset(self.class.key_for(id), attrs)
136
+ track(id, redis) if new_record?
137
137
  end
138
138
  end
139
139
  end
@@ -148,15 +148,17 @@ module Modis
148
148
  end
149
149
 
150
150
  def set_id
151
- self.id = Modis.redis.incr("#{self.class.absolute_namespace}_id_seq")
151
+ Modis.with_connection do |redis|
152
+ self.id = redis.incr("#{self.class.absolute_namespace}_id_seq")
153
+ end
152
154
  end
153
155
 
154
- def track(id)
155
- Modis.redis.sadd(self.class.key_for(:all), id)
156
+ def track(id, redis)
157
+ redis.sadd(self.class.key_for(:all), id)
156
158
  end
157
159
 
158
- def untrack(id)
159
- Modis.redis.srem(self.class.key_for(:all), id)
160
+ def untrack(id, redis)
161
+ redis.srem(self.class.key_for(:all), id)
160
162
  end
161
163
  end
162
164
  end
@@ -6,7 +6,7 @@ module Modis
6
6
 
7
7
  module ClassMethods
8
8
  def transaction
9
- Modis.redis.multi { yield }
9
+ Modis.with_connection { |redis| redis.multi { yield(redis) } }
10
10
  end
11
11
  end
12
12
  end
data/lib/modis/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Modis
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
data/lib/modis.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'redis'
2
+ require 'connection_pool'
2
3
  require 'active_model'
3
4
  require 'active_support/all'
4
5
  require 'multi_json'
@@ -15,13 +16,24 @@ require 'modis/model'
15
16
  module Modis
16
17
  @mutex = Mutex.new
17
18
 
18
- def self.redis
19
- return @redis if @redis
20
- @mutex.synchronize { @redis = Redis.new }
21
- @redis
19
+ class << self
20
+ attr_accessor :connection_pool, :redis_options, :connection_pool_size,
21
+ :connection_pool_timeout
22
22
  end
23
23
 
24
- def self.redis=(redis)
25
- @redis = redis
24
+ self.redis_options = {}
25
+ self.connection_pool_size = 5
26
+ self.connection_pool_timeout = 5
27
+
28
+ def self.connection_pool
29
+ return @connection_pool if @connection_pool
30
+ @mutex.synchronize do
31
+ options = { size: connection_pool_size, timeout: connection_pool_timeout }
32
+ @connection_pool = ConnectionPool.new(options) { Redis.new(redis_options) }
33
+ end
34
+ end
35
+
36
+ def self.with_connection
37
+ connection_pool.with { |connection| yield(connection) }
26
38
  end
27
39
  end
data/modis.gemspec CHANGED
@@ -20,4 +20,5 @@ Gem::Specification.new do |gem|
20
20
  gem.add_dependency 'activemodel', '>= 3.0'
21
21
  gem.add_dependency 'activesupport', '>= 3.0'
22
22
  gem.add_dependency 'redis', '>= 3.0'
23
+ gem.add_dependency 'connection_pool', '>= 2'
23
24
  end
data/spec/spec_helper.rb CHANGED
@@ -14,7 +14,9 @@ end
14
14
 
15
15
  RSpec.configure do |config|
16
16
  config.after :each do
17
- keys = Modis.redis.keys "#{Modis.config.namespace}:*"
18
- Modis.redis.del *keys unless keys.empty?
17
+ Modis.with_connection do |connection|
18
+ keys = connection.keys "#{Modis.config.namespace}:*"
19
+ connection.del *keys unless keys.empty?
20
+ end
19
21
  end
20
22
  end
@@ -8,7 +8,9 @@ end
8
8
 
9
9
  describe Modis::Transaction do
10
10
  it 'yields the block in a transaction' do
11
- Modis.redis.should_receive(:multi).and_yield
11
+ redis = double.as_null_object
12
+ Modis.stub(:with_connection).and_yield(redis)
13
+ redis.should_receive(:multi)
12
14
  TransactionSpec::MockModel.transaction {}
13
15
  end
14
16
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modis
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Leitch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-22 00:00:00.000000000 Z
11
+ date: 2014-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: connection_pool
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '2'
55
69
  description: ActiveModel + Redis
56
70
  email:
57
71
  - port001@gmail.com