redis-helper 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.rubocop.yml +10 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +51 -0
- data/Rakefile +9 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/redis/helper.rb +92 -0
- data/lib/redis/helper/version.rb +6 -0
- data/redis-helper.gemspec +33 -0
- metadata +170 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2f8a91bd9c3a1637aeb3459abfa889f481fc8a57
|
4
|
+
data.tar.gz: 5bcb97d1fa9b302f3eb34eb6163ea8db5c63a22d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 420927ee72553a458e32c89da390f9814cd3c46b47da61c84eb8061ec242dde87dad555b0cf1caa0e7b0aa74351372582b28badc7f5095ec7125fdf323b0aa06
|
7
|
+
data.tar.gz: da61908ece984ba363d22f54309d43f87a5feab499e6b73a9c65704e41f45fc81f6845d79e29186d49aacb50574c6631ea1e15bf7337e46f2243a50981f4dc92
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Narazaka
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Redis::Helper
|
2
|
+
|
3
|
+
Redisを扱うクラスで利用するモジュール
|
4
|
+
|
5
|
+
Redis::Objectsがmulti使えないとかアレっていう [@i2bskn](https://github.com/i2bskn) さん等の想いのカケラ。
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem "redis-helper"
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install redis-helper
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Foo < ActiveRecord::Base
|
27
|
+
include Redis::Helper
|
28
|
+
|
29
|
+
def bar_count
|
30
|
+
# attr_key(:bar_count) => "Foo:<id>:bar_count"
|
31
|
+
redis.get(attr_key(:bar_count)).to_i
|
32
|
+
end
|
33
|
+
|
34
|
+
def update_bar_count(count)
|
35
|
+
# ttl_to(self.end_at) => self.end_at - Time.current
|
36
|
+
redis.setex(attr_key(:bar_count), ttl_to(self.end_at), count)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
foo = Foo.find(id)
|
41
|
+
foo.update_bar_count(10)
|
42
|
+
foo.bar_count => 10
|
43
|
+
```
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it ( https://github.com/Narazaka/redis-helper/fork )
|
48
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
50
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "redis/helper"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/redis/helper.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require "redis"
|
2
|
+
require "active_support"
|
3
|
+
require "active_support/core_ext/object/blank"
|
4
|
+
require "active_support/core_ext/numeric/time"
|
5
|
+
require "redis/helper/version"
|
6
|
+
|
7
|
+
# Redisを扱うクラスで利用するモジュール
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# class Foo < ActiveRecord::Base
|
11
|
+
# include Redis::Helper
|
12
|
+
#
|
13
|
+
# def bar_count
|
14
|
+
# # attr_key(:bar_count) => "Foo:<id>:bar_count"
|
15
|
+
# redis.get(attr_key(:bar_count)).to_i
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# def update_bar_count(count)
|
19
|
+
# # ttl_to(self.end_at) => self.end_at - Time.current
|
20
|
+
# redis.setex(attr_key(:bar_count), ttl_to(self.end_at), count)
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# foo = Foo.find(id)
|
25
|
+
# foo.update_bar_count(10)
|
26
|
+
# foo.bar_count => 10
|
27
|
+
#
|
28
|
+
class Redis
|
29
|
+
module Helper
|
30
|
+
# 正しくない固有キー(固有キー値が空?)
|
31
|
+
class UnknownUniqueValue < StandardError; end
|
32
|
+
|
33
|
+
# デフォルトの固有キー名
|
34
|
+
DEFAULT_UNIQUE_ATTR_NAME = :id
|
35
|
+
# redisキーの区切り文字
|
36
|
+
REDIS_KEY_DELIMITER = ":".freeze
|
37
|
+
|
38
|
+
def self.included(klass)
|
39
|
+
klass.extend ClassMethods
|
40
|
+
end
|
41
|
+
|
42
|
+
# クラスメソッド
|
43
|
+
module ClassMethods
|
44
|
+
# Redis.currentへのショートカット
|
45
|
+
def redis
|
46
|
+
@redis ||= ::Redis.current
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# instance固有のkeyとattr_nameからkeyを生成する
|
51
|
+
# (instanceに複数のkeyを設定したい場合やkeyにattr_nameを含めたい場合に使用する)
|
52
|
+
# @param [String|Symbol] name キー名
|
53
|
+
# @param [String|Symbol] unique_attr インスタンスの固有キーとして使用するメソッド名
|
54
|
+
# @return [String]
|
55
|
+
def attr_key(name, unique_attr = nil)
|
56
|
+
[instance_key(unique_attr), name].join(REDIS_KEY_DELIMITER)
|
57
|
+
end
|
58
|
+
|
59
|
+
# instance固有のkeyを生成する ("<Class Name>:<unique key>")
|
60
|
+
# @param [String|Symbol] unique_attr インスタンスの固有キーとして使用するメソッド名
|
61
|
+
# @return [String]
|
62
|
+
def instance_key(unique_attr = nil)
|
63
|
+
attr_name = unique_attr || DEFAULT_UNIQUE_ATTR_NAME
|
64
|
+
if (unique_key = self.public_send(attr_name)).blank?
|
65
|
+
raise UnknownUniqueValue, "unique keyとして指定された値(#{attr_name})が取得できません"
|
66
|
+
end
|
67
|
+
[self.class.name, unique_key].join(REDIS_KEY_DELIMITER)
|
68
|
+
end
|
69
|
+
|
70
|
+
# 引数で指定した時間にexpireするためのttl値を生成
|
71
|
+
# @example
|
72
|
+
# # 2016/10/26 正午にexpireする
|
73
|
+
# redis.setex(key, ttl_to(Time.zone.parse("2016-10-26 12:00:00")), value)
|
74
|
+
# # 24時間後にexpireする
|
75
|
+
# redis.setex(key, ttl_to(1.day.since), value)
|
76
|
+
# @param [Time] to_time expireする時間
|
77
|
+
# @param [Time] from_time 現在時間
|
78
|
+
# @param [Boolean] unsigned_non_zero 計算結果のttlが0の場合1を返す
|
79
|
+
# @return [Integer]
|
80
|
+
def ttl_to(to_time, from_time = Time.current, unsigned_non_zero: true)
|
81
|
+
ttl = (to_time - from_time).to_i
|
82
|
+
return ttl if ttl > 0
|
83
|
+
unsigned_non_zero ? 1 : ttl
|
84
|
+
end
|
85
|
+
|
86
|
+
# Redis.currentへのショートカット
|
87
|
+
# @return [Redis]
|
88
|
+
def redis
|
89
|
+
self.class.redis
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "redis/helper/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "redis-helper"
|
8
|
+
spec.version = Redis::Helper::VERSION
|
9
|
+
spec.authors = ["Narazaka"]
|
10
|
+
spec.email = ["info@narazaka.net"]
|
11
|
+
|
12
|
+
spec.summary = "helper module for models using Redis"
|
13
|
+
spec.homepage = "https://github.com/Narazaka/redis-helper"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
if spec.respond_to?(:metadata)
|
22
|
+
spec.metadata["yard.run"] = "yri"
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.add_dependency "redis"
|
26
|
+
spec.add_dependency "activesupport"
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.5"
|
30
|
+
spec.add_development_dependency "yard"
|
31
|
+
spec.add_development_dependency "simplecov"
|
32
|
+
spec.add_development_dependency "onkcop"
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis-helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Narazaka
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-13 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: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.12'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.12'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.5'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: yard
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: onkcop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description:
|
126
|
+
email:
|
127
|
+
- info@narazaka.net
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".rubocop.yml"
|
135
|
+
- ".travis.yml"
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- bin/console
|
141
|
+
- bin/setup
|
142
|
+
- lib/redis/helper.rb
|
143
|
+
- lib/redis/helper/version.rb
|
144
|
+
- redis-helper.gemspec
|
145
|
+
homepage: https://github.com/Narazaka/redis-helper
|
146
|
+
licenses:
|
147
|
+
- MIT
|
148
|
+
metadata:
|
149
|
+
yard.run: yri
|
150
|
+
post_install_message:
|
151
|
+
rdoc_options: []
|
152
|
+
require_paths:
|
153
|
+
- lib
|
154
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - ">="
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
159
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
requirements: []
|
165
|
+
rubyforge_project:
|
166
|
+
rubygems_version: 2.5.1
|
167
|
+
signing_key:
|
168
|
+
specification_version: 4
|
169
|
+
summary: helper module for models using Redis
|
170
|
+
test_files: []
|