redisable 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 33c1cbd877def31fda7be5d4acbcd80a577ad51e
4
+ data.tar.gz: 71449a1feee54a530e45cc7cb467cb4d28eaf65b
5
+ SHA512:
6
+ metadata.gz: baa2e1cc3c9393c2f343fac7afa2f264e75de330935784d5bf9579b36b71d5ff469298dd8265062d12f21e23ff4c456bc6e617a8ebdb8425581351c46a3bacb0
7
+ data.tar.gz: de00dfb8e9fb9df9f48b47d874f072a1f231a20dbd08f5beb54194c908d3e784ecee23bbf1450815bb518aa2a4750ee5df25e603cf6b99006e2ea59e0279c97f
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .rspec
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - jruby
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redisable.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 yoppi
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.
@@ -0,0 +1,97 @@
1
+ # Redisable
2
+
3
+ Thin wrapper library for Redis, enable any Ruby class to access Redis.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'redisable'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install redisable
18
+
19
+ ## Usage
20
+
21
+ Use in Rails application. Below redis settings in `config/redis.yml`.
22
+
23
+ ```yaml
24
+ # condig/redis.yml
25
+ development:
26
+ host: localhost
27
+ port: 6379
28
+ db: 1
29
+ test:
30
+ host: localhost
31
+ port: 6379
32
+ db: 0
33
+ production:
34
+ master_name: apps
35
+ failover_reconnect_timeout: 30
36
+ sentinels:
37
+ - host: redis1.example.com
38
+ port: 26379
39
+ - host: redis2.example.com
40
+ port: 26379
41
+ ```
42
+
43
+ Then, initialize Redisable at `config/initializers/redis.rb`.
44
+
45
+ ```ruby
46
+ Redisable::Config.load YAML.load_file(File.join(Rails.root, "config/redis.yml"))
47
+ ```
48
+
49
+ In model, `users`, User is ActiveRecord object, but some `user_status` is ephemeral or volatile data, so won't record to RDBMS.
50
+ Below code is store `user_stauts` in Redis.
51
+
52
+ ```ruby
53
+ class User < ActiveRecord::Base
54
+ def user_status
55
+ @user_status ||= UserStatus.new(id)
56
+ end
57
+ end
58
+
59
+ class UserStatus
60
+ include Redisable
61
+ kvs_key :followers_ids
62
+ kvs_key :unread_news_ids
63
+
64
+ def initialize(id)
65
+ @user_id = id
66
+ end
67
+
68
+ def id
69
+ @user_id
70
+ end
71
+
72
+ def followers
73
+ redis.get followers_ids
74
+ end
75
+
76
+ def unread_news
77
+ redis.lrange unread_news_ids, 0, -1
78
+ end
79
+ ...
80
+ end
81
+
82
+ class UsersController
83
+ def show(id)
84
+ user_status = current_user.user_status
85
+ user_status.followers
86
+ user_status.unread_news
87
+ end
88
+ end
89
+ ```
90
+
91
+ ## Contributing
92
+
93
+ 1. Fork it
94
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
95
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
96
+ 4. Push to the branch (`git push origin my-new-feature`)
97
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ require "redisable/version"
2
+ require "redis"
3
+
4
+ module Redisable
5
+ autoload :Config, "redisable/config"
6
+ autoload :Connection, "redisable/connection"
7
+ autoload :KVSKey, "redisable/kvs_key"
8
+
9
+ def self.included(base)
10
+ base.extend ClassMethods
11
+ base.send :include, InstanceMethods
12
+ base.send :include, KVSKey
13
+ end
14
+
15
+ module ClassMethods
16
+ attr_accessor :redis_server
17
+ def redis
18
+ server = redis_server || :application
19
+ Redisable::Connection.conn(server)
20
+ end
21
+ end
22
+
23
+ module InstanceMethods
24
+ private
25
+ def redis
26
+ @_redis ||= self.class.redis
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Redisable
3
+ class Config
4
+ def self.load(conf, server=:application)
5
+ @conf ||= {}
6
+ @conf[server] = conf
7
+ end
8
+
9
+ def self.conf(server=:application)
10
+ @conf[server]
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,31 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Redisable
3
+ module Connection
4
+ def self.conn(server)
5
+ @pool ||= {}
6
+ @pool[server] ||=
7
+ begin
8
+ conf = Redisable::Config.conf(server)
9
+ redis = ::Redis.new(conf)
10
+ raise "Redis server[#{conf["host"]}:#{conf["port"]}] is down!" unless reachable?(redis)
11
+ redis
12
+ end
13
+ end
14
+
15
+ def self.quit(server)
16
+ redis = @pool[server]
17
+ redis.quit if redis && reachable?(redis)
18
+ @pool[server] = nil
19
+ end
20
+
21
+ def self.reset(server)
22
+ quit(server)
23
+ conn(server)
24
+ end
25
+
26
+ private
27
+ def self.reachable?(redis)
28
+ redis.ping == "PONG"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,34 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Redisable
3
+ module KVSKey
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ # return model_name:id:field_name
10
+ def kvs_key(name, options={})
11
+ klass_name ||= self.name
12
+ define_method(name) do |id_=nil|
13
+ id_ ||= if options[:id]
14
+ options[:id].call(self)
15
+ else
16
+ self.id
17
+ end
18
+ KVSKey.join_key(klass_name, id_, name, options[:blank_field])
19
+ end
20
+ define_singleton_method(name) do |id_=nil|
21
+ id_ ||= opts[:id].call(self)
22
+ KVSKey.join_key(klass_name, id_, name, options[:blank_field])
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.join_key(klass_name, id, name, is_blank_field)
28
+ k = "#{klass_name}"
29
+ k << ":#{id}" if id
30
+ k << ":#{name}" unless is_blank_field
31
+ k
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module Redisable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redisable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "redisable"
8
+ spec.version = Redisable::VERSION
9
+ spec.authors = ["yoppi"]
10
+ spec.email = ["y.hirokazu@gmail.com"]
11
+ spec.description = %q{Thin wrapper library for Redis, enable any Ruby class to access Redis.}
12
+ spec.summary = %q{Thin wrapper library for Redis.}
13
+ spec.homepage = "http://github.com/yoppi/redisable"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "redis"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Redisable::Config do
4
+ let(:conf) {
5
+ {
6
+ host: "localhost",
7
+ port: "6379",
8
+ db: 1,
9
+ }
10
+ }
11
+ describe ".load/.conf" do
12
+ it "store and get Redis configuration" do
13
+ Redisable::Config.load(conf)
14
+ Redisable::Config.conf.should == conf
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,35 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Redisable::Connection do
5
+ let(:server) {:application}
6
+ let(:conf) {
7
+ {
8
+ host: "localhost",
9
+ port: "6379",
10
+ db: "1",
11
+ }
12
+ }
13
+ describe ".conn" do
14
+ context "in access ':application' first time" do
15
+ it "create connection" do
16
+ Redisable::Config.stub(:conf) { conf }
17
+ Redis.any_instance.stub(:ping) { "PONG" }
18
+ Redisable::Connection.conn(server)
19
+ Redisable::Connection.instance_variable_get("@pool").size.should == 1
20
+ end
21
+ after do
22
+ Redisable::Connection.instance_variable_set("@pool", {})
23
+ end
24
+ end
25
+ context "in request timeout" do
26
+ it "raise RuntimeError" do
27
+ Redisable::Config.stub(:conf) { conf }
28
+ Redis.any_instance.stub(:ping) { "" }
29
+ expect {
30
+ Redisable::Connection.conn(server)
31
+ }.to raise_error(RuntimeError)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ class User
5
+ include Redisable::KVSKey
6
+ end
7
+
8
+ describe Redisable::KVSKey do
9
+ describe ".join_key" do
10
+ context "Field name is not blank" do
11
+ it "join with ':' specified key, id and name" do
12
+ Redisable::KVSKey.join_key("user", "100", "status", false).should == "user:100:status"
13
+ end
14
+ end
15
+
16
+ context "Field name is blank" do
17
+ it "join with ':' specified key, id" do
18
+ Redisable::KVSKey.join_key("user", "100", "status", true).should == "user:100"
19
+ end
20
+ end
21
+ end
22
+
23
+ describe "to include" do
24
+ it "defined 'kvs_key' method" do
25
+ defined?(User.kvs_key).should == "method"
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ class User
4
+ include Redisable
5
+ end
6
+
7
+ describe Redisable do
8
+ it "autoload modules" do
9
+ defined?(Redisable::Config).should == "constant"
10
+ defined?(Redisable::Connection).should == "constant"
11
+ defined?(Redisable::KVSKey).should == "constant"
12
+ end
13
+
14
+ describe "Include module" do
15
+ it "define kvs_key method" do
16
+ defined?(User.kvs_key).should == "method"
17
+ end
18
+ it "define redis method" do
19
+ defined?(User.redis).should == "method"
20
+ end
21
+ it "define redis instance method" do
22
+ defined?(User.new.send(:redis)).should == "method"
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
2
+ require 'redisable'
3
+
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ config.filter_run :focus
8
+
9
+ # Run specs in random order to surface order dependencies. If you find an
10
+ # order dependency and want to debug it, you can fix the order by providing
11
+ # the seed, which is printed after each run.
12
+ # --seed 1234
13
+ config.order = 'random'
14
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redisable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - yoppi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Thin wrapper library for Redis, enable any Ruby class to access Redis.
56
+ email:
57
+ - y.hirokazu@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/redisable.rb
69
+ - lib/redisable/config.rb
70
+ - lib/redisable/connection.rb
71
+ - lib/redisable/kvs_key.rb
72
+ - lib/redisable/version.rb
73
+ - redisable.gemspec
74
+ - spec/config_spec.rb
75
+ - spec/connection_spec.rb
76
+ - spec/kvs_key_spec.rb
77
+ - spec/redisable_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: http://github.com/yoppi/redisable
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.0.0
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Thin wrapper library for Redis.
103
+ test_files:
104
+ - spec/config_spec.rb
105
+ - spec/connection_spec.rb
106
+ - spec/kvs_key_spec.rb
107
+ - spec/redisable_spec.rb
108
+ - spec/spec_helper.rb
109
+ has_rdoc: