redis-central 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.
data/LICENCE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Simon Wilkins
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Redis::Central
2
+
3
+ redis-central providers a helper for providing access and configuration to a single per-application/thread, optionally namespaced, redis client.
4
+
5
+ ## Installation
6
+
7
+ gem install redis-central
8
+
9
+ ## Usage
10
+
11
+ ### Shared client
12
+ At it's most basic (and it's all pretty basic), redis-central provides your classes and objects with a "redis" helper method for accessing a single redis client. In this form it will return a [Redis](https://github.com/redis/redis-rb) instance
13
+
14
+ require 'redis-central'
15
+
16
+ class User
17
+
18
+ include Redis::Central
19
+
20
+ attr_accessor :id, :name
21
+
22
+ def self.find(id)
23
+ user = User.new
24
+ user.name = redis.get "users:#{id}:name"
25
+ end
26
+
27
+ def save
28
+ redis.set "users:#{id}:name", name
29
+ end
30
+
31
+ end
32
+
33
+ Both the class and its instances will have access to the redis method. Other classes can also include Redis::Central, and these will have access to the same redis client instance.
34
+
35
+ Configuration options which would normally be passed to the Redis instance can be set on the ``Redis::Central`` class, eg.
36
+
37
+ Redis::Central.port = 1234
38
+
39
+ ### Namespacing
40
+ The real motivation for this library, however, is to provide an easy means of centrally namespacing redis keys for an application.
41
+
42
+ This is particularly useful where a single redis server is being used for multiple deployments of the same application, or for applications which share a model/ models which are persisted through redis, or simply scenarios were redis keys could clash between applications.
43
+
44
+ So for instance you could have an initializer containing something like this:
45
+
46
+ app_name = RAILS_ROOT.split(File::SEPARATOR).last
47
+ Redis::Central.namespace = app_name
48
+
49
+ In this scenario calls to the redis method will return an instance of [Redis::Namespace](https://github.com/defunkt/redis-namespace), namespaced with the given setting. All queries to this object will then be prefixed accordingly.
50
+
51
+ ## Testing
52
+
53
+ bundle install
54
+ rake test
55
+
56
+ ## Author
57
+ Si Wilkins
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs.push 'lib'
5
+ t.test_files = FileList['spec/**/*_spec.rb']
6
+ t.verbose = true
7
+ end
@@ -0,0 +1,9 @@
1
+ class Redis
2
+
3
+ module Central
4
+
5
+ VERSION = '0.1.0'
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,67 @@
1
+ require 'redis-namespace'
2
+
3
+ class Redis
4
+
5
+ module Central
6
+
7
+ class << self
8
+
9
+ REDIS_CLIENT_OPTIONS = [
10
+ :scheme,
11
+ :host,
12
+ :port,
13
+ :path,
14
+ :timeout,
15
+ :password,
16
+ :db
17
+ ]
18
+
19
+ attr_writer *REDIS_CLIENT_OPTIONS
20
+ attr_writer :namespace
21
+
22
+ def redis
23
+ @redis ||= begin
24
+ redis = Redis.new(client_options)
25
+ if @namespace && @namespace =~ /\w/
26
+ Redis::Namespace.new(@namespace, :redis => redis)
27
+ else
28
+ redis
29
+ end
30
+ end
31
+ end
32
+
33
+ private
34
+ def client_options
35
+ REDIS_CLIENT_OPTIONS.inject({}) do |memo, option|
36
+ option_value = instance_variable_get("@#{option}")
37
+ memo[option] = option_value if option_value
38
+ memo
39
+ end
40
+ end
41
+
42
+ def reset!
43
+ @redis = nil
44
+ @namespace = nil
45
+ REDIS_CLIENT_OPTIONS.each do |option|
46
+ self.send("#{option}=", nil)
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ def self.included(base)
53
+ base.send(:include, Access)
54
+ base.extend(Access)
55
+ end
56
+
57
+ module Access
58
+
59
+ def redis
60
+ Redis::Central.redis
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path('../redis/central.rb', __FILE__)
@@ -0,0 +1,107 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ class DummyModel
4
+ include Redis::Central
5
+ end
6
+
7
+ describe "a class including Redis::Central" do
8
+
9
+ it "should delegate calls to redis to Redis::Central" do
10
+ redis = mock
11
+ Redis::Central.expects(:redis).returns redis
12
+ DummyModel.redis.must_equal redis
13
+ end
14
+
15
+ end
16
+
17
+ describe "an instance of a class including Redis::Central" do
18
+
19
+ it "should delegate calls to redis to Redis::Central" do
20
+ redis = mock
21
+ Redis::Central.expects(:redis).returns redis
22
+ DummyModel.new.redis.must_equal redis
23
+ end
24
+
25
+ end
26
+
27
+ def test_option(option, value)
28
+ Redis::Central.send("#{option}=", value)
29
+ Redis.expects(:new).with({option => value})
30
+ Redis::Central.redis
31
+ end
32
+
33
+ describe Redis::Central do
34
+
35
+ describe "with options" do
36
+
37
+ before do
38
+ Redis::Central.send(:reset!)
39
+ end
40
+
41
+ it "should pass scheme to Redis.new when set" do
42
+ test_option(:scheme, 'tcp')
43
+ end
44
+
45
+ it "should pass host to Redis.new when set" do
46
+ test_option(:host, 'localhost')
47
+ end
48
+
49
+ it "should pass port to Redis.new when set" do
50
+ test_option(:port, 1234)
51
+ end
52
+
53
+ it "should pass path to Redis.new when set" do
54
+ test_option(:path, '/path/to/redis')
55
+ end
56
+
57
+ it "should pass timeout to Redis.new when set" do
58
+ test_option(:timeout, 10.0)
59
+ end
60
+
61
+ it "should pass password to Redis.new when set" do
62
+ test_option(:password, 'pa55w0rd')
63
+ end
64
+
65
+ it "should pass db to Redis.new when set" do
66
+ test_option(:db, 1)
67
+ end
68
+
69
+ end
70
+
71
+ describe "when namespaced" do
72
+
73
+ before do
74
+ Redis::Central.send(:reset!)
75
+ Redis::Central.namespace = 'my:app'
76
+ end
77
+
78
+ it "should return a Redis::Namespace on call to redis" do
79
+ Redis::Central.redis.must_be_instance_of Redis::Namespace
80
+ end
81
+
82
+ it "should cache the instance" do
83
+ redis = mock
84
+ Redis::Namespace.expects(:new).once.returns redis
85
+ [Redis::Central.redis, Redis::Central.redis].must_equal [redis, redis]
86
+ end
87
+ end
88
+
89
+ describe "when not namespaced" do
90
+
91
+ before do
92
+ Redis::Central.send(:reset!)
93
+ end
94
+
95
+ it "should return a Redis on call to redis" do
96
+ Redis::Central.redis.must_be_instance_of Redis
97
+ end
98
+
99
+ it "should cache the instance" do
100
+ redis = mock
101
+ Redis.expects(:new).once.returns redis
102
+ [Redis::Central.redis, Redis::Central.redis].must_equal [redis, redis]
103
+ end
104
+
105
+ end
106
+
107
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+
3
+ require 'minitest/spec'
4
+ require 'minitest/autorun'
5
+ require 'mocha'
6
+
7
+ require File.expand_path('../../lib/redis-central', __FILE__)
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis-central
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Si Wilkins
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-06-15 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rake
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: redis-namespace
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: minitest
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :development
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: mocha
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ type: :development
75
+ version_requirements: *id004
76
+ description: |
77
+ Provides an interface for providing access and configuration to a single
78
+ per-application/thread, optionally namespaced, redis client.
79
+
80
+ email: si.wilkins@gmail.com
81
+ executables: []
82
+
83
+ extensions: []
84
+
85
+ extra_rdoc_files: []
86
+
87
+ files:
88
+ - README.md
89
+ - Rakefile
90
+ - LICENCE
91
+ - lib/redis/central/version.rb
92
+ - lib/redis/central.rb
93
+ - lib/redis-central.rb
94
+ - spec/redis/central_spec.rb
95
+ - spec/spec_helper.rb
96
+ homepage: http://github.com/siwilkins/redis-central
97
+ licenses: []
98
+
99
+ post_install_message:
100
+ rdoc_options: []
101
+
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ hash: 3
110
+ segments:
111
+ - 0
112
+ version: "0"
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ requirements: []
123
+
124
+ rubyforge_project:
125
+ rubygems_version: 1.8.24
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: Provides easy access, and global namespacing, to a single redis client
129
+ test_files: []
130
+