godredis 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 997523d959196b0d1f84f05ca4d2a1658aa97f46
4
+ data.tar.gz: 6ff84c78859b12c8a9f1ad6c4528c761fcf8de15
5
+ SHA512:
6
+ metadata.gz: 1c8194aace19d508f635cc8b2e6be57815a09f182c301a835aa1ed2bc956a4c14e0aeedf79af899223fc6abb4c43562631d4f8c300155481ee65ae01798bfcee
7
+ data.tar.gz: 999b140c98f51e8f81c9b3a4b1ca47dd5a12564bd062a14abf6e44ed4c14d3dd7930aba6a5281edc4e811d8d8b9358d80709c7efafee66651d38f79041741e90
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in godredis.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Anton
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
+ # Godredis: bulk managing multiply Redis instances
2
+
3
+ Godredis gem provides unified interface for mass managing [Redis](http://redis.io) connections which could be initialized in different modules having each own custom API.
4
+
5
+ It is useful when you need to close or reset connections on forking, for example, with [puma](http://github.com/puma/puma) server in the `on_restart` block:
6
+
7
+ on_restart do
8
+ # Rails.cache.instance_variable_get('@data').quit
9
+ # Redis::Objects.redis.quit
10
+ # Sidekiq.redis(&:quit)
11
+ Godredis.quit_all! # instead of commented lines above
12
+ end
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ gem 'godredis'
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install godredis
27
+
28
+ ## Usage
29
+
30
+ There are several ways to call bulk commands with Godredis:
31
+
32
+ # Godredis.command_all! -- will output command execution result
33
+ Godredis.reconnect_all! # => Redis [cache_store]: reconnect... [OK]
34
+ # => Redis [objects]: reconnect... [OK]
35
+ # => Redis [sidekiq]: reconnect... [OK]
36
+
37
+ # Godredis.command_all -- silent
38
+ Godredis.quit_all
39
+
40
+ # Just different syntax
41
+ Godredis.redises(&:quit)
42
+ Godredis.redises(&:quit!)
43
+
44
+ # It's also return an enumerator, so you can do something like this:
45
+ Godredis.redises.map(&:connected?)
46
+
47
+ Subclass `Godredis::Base` class to collect Redis-related objects and use simple DSL to set mapping for the common methods such as `quit` or `reconnect` if they are different from the defaults.
48
+
49
+ class CacheStoreGodredis < Godredis::Base
50
+ redis ->{ Rails.cache.instance_variable_get('@data') }
51
+ end
52
+
53
+ class ObjectsGodredis < Godredis::Base
54
+ redis ->{ Redis::Objects.redis }
55
+ end
56
+
57
+ class SidekiqGodredis < Godredis::Base
58
+ # define mappings with blocks or lambdas
59
+ redis ->(&block){ Sidekiq.redis &block }
60
+ client { redis &:client }
61
+ quit { redis &:quit }
62
+ end
63
+
64
+ Default mapping:
65
+
66
+ redis { Redis.current }
67
+ client { redis.client }
68
+ connected? { client.connected? }
69
+ reconnect { client.reconnect.connected? }
70
+ quit { redis.quit }
71
+
72
+ You may also add custom commands:
73
+
74
+ class SomeGodredis < Godredis::Base
75
+ del_some_key { redis.del('some_key') }
76
+ end
77
+
78
+ Godredis.redises(&:del_some_key!)
79
+ # etc...
80
+
81
+ Every commands (except question-marked) also has a banged wrapper `command!`, which calls an itself command and puts a short message about its execution result.
82
+
83
+ ## Contributing
84
+
85
+ 1. Fork it ( https://github.com/[my-github-username]/godredis/fork )
86
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
87
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
88
+ 4. Push to the branch (`git push origin my-new-feature`)
89
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/godredis.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "godredis"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Tõnis Simo"]
9
+ spec.email = ["anton.estum@gmail.com"]
10
+ spec.summary = %q{Godredis: bulk managing multiply Redis instances.}
11
+ spec.description = %q{Godredis provides unified interface for mass managing
12
+ Redis connections which could be initialized in
13
+ different modules having each own custom API.}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_dependency "activesupport", ">= 3.0.0"
25
+ end
data/lib/godredis.rb ADDED
@@ -0,0 +1,133 @@
1
+ require 'active_support/core_ext/class/subclasses'
2
+
3
+ # = Godredis: bulk managing multiply Redis instances
4
+ #
5
+ # Godredis provides unified interface for mass managing Redis connections which
6
+ # could be initialized in different modules having each own custom API.
7
+ #
8
+ # It is useful when you need to close or reset connections on forking, for
9
+ # example, with puma server in the <tt>on_restart</tt> block:
10
+ #
11
+ # on_restart do
12
+ # # Rails.cache.instance_variable_get('@data').quit
13
+ # # Redis::Objects.redis.quit
14
+ # # Sidekiq.redis(&:quit)
15
+ # Godredis.quit_all! # instead of commented lines above
16
+ # end
17
+ #
18
+ # There are several ways to call bulk commands with Godredis:
19
+ #
20
+ # # Godredis.command_all! -- will output command execution result
21
+ # Godredis.reconnect_all! # => Redis [cache_store]: reconnect... [OK]
22
+ # # => Redis [objects]: reconnect... [OK]
23
+ # # => Redis [sidekiq]: reconnect... [OK]
24
+ #
25
+ # # Godredis.command_all -- silent
26
+ # Godredis.quit_all
27
+ #
28
+ # # Just different syntax
29
+ # Godredis.redises(&:quit)
30
+ # Godredis.redises(&:quit!)
31
+ #
32
+ # # It's also return an enumerator, so you can do something like this:
33
+ # Godredis.redises.map(&:connected?)
34
+ #
35
+ # See Godredis::Base documentation for collecting your Redis-related objects.
36
+ module Godredis
37
+ # :nodoc:
38
+ def self.redises(&block)
39
+ Base.subclasses.map(&:new).each(&block)
40
+ end
41
+
42
+ def self.method_missing(method, *args, &blk)
43
+ if /^(?<action>.+)_all(?<bang>!)?$/ =~ method.to_s
44
+ redises &:"#{action}#{bang}"
45
+ else
46
+ super
47
+ end
48
+ end
49
+
50
+ # Subclass Godredis base class to collect Redis-related objects and use
51
+ # simple DSL to set mapping for the common methods such as <tt>quit</tt> or
52
+ # <tt>reconnect</tt> if they are different from the defaults.
53
+ #
54
+ # class CacheStoreGodredis < Godredis::Base
55
+ # redis ->{ Rails.cache.instance_variable_get('@data') }
56
+ # end
57
+ #
58
+ # class ObjectsGodredis < Godredis::Base
59
+ # redis ->{ Redis::Objects.redis }
60
+ # end
61
+ #
62
+ # class SidekiqGodredis < Godredis::Base
63
+ # # define mappings with blocks or lambdas
64
+ # redis ->(&block){ Sidekiq.redis &block }
65
+ # client { redis &:client }
66
+ # quit { redis &:quit }
67
+ # end
68
+ #
69
+ # Default mapping:
70
+ #
71
+ # redis { Redis.current }
72
+ # client { redis.client }
73
+ # connected? { client.connected? }
74
+ # reconnect { client.reconnect.connected? }
75
+ # quit { redis.quit }
76
+ #
77
+ # You may also add custom commands:
78
+ #
79
+ # class SomeGodredis < Godredis::Base
80
+ # del_some_key { redis.del('some_key') }
81
+ # end
82
+ #
83
+ # Godredis.redises(&:del_some_key!)
84
+ # # etc...
85
+ #
86
+ # Every commands (except question-marked) also has a banged wrapper
87
+ # <tt>command!</tt>, which calls an itself command and puts a short message
88
+ # about its execution result
89
+ class Base
90
+ class << self
91
+ def tag
92
+ @tag ||= name.demodulize[/^(.+?)((?:god)?redis)?$/i, 1].underscore
93
+ end
94
+
95
+ protected
96
+ def method_missing(method, proc = nil, *args, &block)
97
+ if block_given? || proc.respond_to?(:to_proc)
98
+ define_method method, &(block || proc)
99
+ else
100
+ super
101
+ end
102
+ end
103
+ end
104
+
105
+ # Default command mapping
106
+ redis { Redis.current }
107
+ client { redis.client }
108
+ connected? { client.connected? }
109
+ reconnect { client.reconnect.connected? }
110
+ quit { redis.quit }
111
+
112
+ def method_missing(method, *args, &block)
113
+ if /^(?<action>.+[^\?])!$/ =~ method.to_s && respond_to?(action)
114
+ say(action) { send action }
115
+ else
116
+ super
117
+ end
118
+ end
119
+
120
+ delegate :tag, to: :class
121
+
122
+ private
123
+ def say(action, &block)
124
+ result = _get_short_status_calling(&block)
125
+ puts "Redis [#{tag}]: #{action}... #{result}"
126
+ end
127
+
128
+ def _get_short_status_calling(&block)
129
+ result = begin; block.call rescue nil; end
130
+ result == true ? '[OK]' : result || '[FAIL]'
131
+ end
132
+ end
133
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: godredis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tõnis Simo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-31 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ description: "Godredis provides unified interface for mass managing \n Redis
56
+ connections which could be initialized in \n different
57
+ modules having each own custom API."
58
+ email:
59
+ - anton.estum@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - godredis.gemspec
70
+ - lib/godredis.rb
71
+ homepage: ''
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.2.2
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: 'Godredis: bulk managing multiply Redis instances.'
95
+ test_files: []