modis 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/modis/finders.rb +9 -4
- data/lib/modis/persistence.rb +13 -11
- data/lib/modis/transaction.rb +1 -1
- data/lib/modis/version.rb +1 -1
- data/lib/modis.rb +18 -6
- data/modis.gemspec +1 -0
- data/spec/spec_helper.rb +4 -2
- data/spec/transaction_spec.rb +3 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6359a4c5d347150ae7cb19754caf5398fd42d0d
|
4
|
+
data.tar.gz: 330ef4946885313394c85425599e4d1274cda2f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
15
|
-
|
16
|
-
|
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
|
data/lib/modis/persistence.rb
CHANGED
@@ -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
|
-
|
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 =
|
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
|
-
|
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
|
-
|
156
|
+
def track(id, redis)
|
157
|
+
redis.sadd(self.class.key_for(:all), id)
|
156
158
|
end
|
157
159
|
|
158
|
-
def untrack(id)
|
159
|
-
|
160
|
+
def untrack(id, redis)
|
161
|
+
redis.srem(self.class.key_for(:all), id)
|
160
162
|
end
|
161
163
|
end
|
162
164
|
end
|
data/lib/modis/transaction.rb
CHANGED
data/lib/modis/version.rb
CHANGED
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
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
25
|
-
|
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
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
|
-
|
18
|
-
|
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
|
data/spec/transaction_spec.rb
CHANGED
@@ -8,7 +8,9 @@ end
|
|
8
8
|
|
9
9
|
describe Modis::Transaction do
|
10
10
|
it 'yields the block in a transaction' do
|
11
|
-
|
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.
|
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-
|
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
|