redis-cacheable 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.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +116 -0
- data/Rakefile +6 -0
- data/lib/redis-cacheable.rb +2 -0
- data/lib/redis_cacheable/active_record.rb +26 -0
- data/lib/redis_cacheable/configuration.rb +17 -0
- data/lib/redis_cacheable/connectable.rb +44 -0
- data/lib/redis_cacheable/version.rb +3 -0
- data/lib/redis_cacheable.rb +87 -0
- data/redis-cacheable.gemspec +34 -0
- data/spec/lib/redis_cacheable/active_record_spec.rb +72 -0
- data/spec/lib/redis_cacheable/configuration_spec.rb +15 -0
- data/spec/lib/redis_cacheable/connectable_spec.rb +25 -0
- data/spec/lib/redis_cacheable_spec.rb +59 -0
- data/spec/spec_helper.rb +24 -0
- metadata +236 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 87a7aef6b828de4bb822b88f6e46e3a937ce2f69
|
|
4
|
+
data.tar.gz: 89fa8e85dd690c00b140f15cc9bcce76b990f0e5
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: bc8acfa1d2e11812c0e1518eda829963fb3e3422f31da021ff7aed52ce527a7e1b29e936b57a082288d349a94a1ade60afef11d4dadc0801f879efe4737bb8fa
|
|
7
|
+
data.tar.gz: e1861ebe039c1f7276fabb4e349407b5a19c79b5a7e71b7a06cad2a22dcf0c8e509a2337594df1a1d1d01bea0d0c282660f1a6feff68f3ad455c414ef0ce75ac
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 joker1007
|
|
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,116 @@
|
|
|
1
|
+
# redis-cacheable
|
|
2
|
+
[](https://travis-ci.org/joker1007/redis-cacheable)
|
|
3
|
+
[](https://coveralls.io/r/joker1007/redis-cacheable?branch=master)
|
|
4
|
+
[](https://codeclimate.com/github/joker1007/redis-cacheable)
|
|
5
|
+
|
|
6
|
+
It is concern style redis caching helper.
|
|
7
|
+
It makes very easy to cache object.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Add this line to your application's Gemfile:
|
|
12
|
+
|
|
13
|
+
gem 'redis-cacheable'
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install redis-cacheable
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Plain Object
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
class MyObject
|
|
29
|
+
include RedisCacheable
|
|
30
|
+
|
|
31
|
+
attr_reader :id, :name
|
|
32
|
+
|
|
33
|
+
redis_key :id # optional (default: :id)
|
|
34
|
+
redis_attrs :id, :name # method names
|
|
35
|
+
|
|
36
|
+
def initialize(attributes)
|
|
37
|
+
@id = attributes[:id]
|
|
38
|
+
@name = attributes[:name]
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
class ProcObject < MyObject
|
|
43
|
+
redis_attrs ->(obj) { obj.id * 10 } # can give proc
|
|
44
|
+
end
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
my_object = MyObject.new(id: 1, name: "my_object")
|
|
49
|
+
my_object.cache_to_redis # KEY = my_object.id, VALUE = MultiJson.dump({"id" => my_object.id, "name" => my_object.name})
|
|
50
|
+
|
|
51
|
+
MyObject.find_from_redis(1) # => {"id" => 1, "name" => "my_object"}
|
|
52
|
+
|
|
53
|
+
proc_object = ProcObject.new(id: 1, name: "proc_object")
|
|
54
|
+
proc_object.cache_to_redis # different namespace with MyObject
|
|
55
|
+
ProcObject.find_from_redis(1) # => 10
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### ActiveRecord
|
|
59
|
+
|
|
60
|
+
If you use ActiveRecord, can include more specialized module.
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
# id: integer
|
|
64
|
+
# name: string
|
|
65
|
+
# rate: float
|
|
66
|
+
|
|
67
|
+
class MyRecord < ActiveRecord::Base
|
|
68
|
+
include RedisCacheable::ActiveRecord
|
|
69
|
+
redis_attrs :id, :name, :rate # method names
|
|
70
|
+
end
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
```ruby
|
|
74
|
+
record = MyRecord.create(name: "my_record", rate: 4.5)
|
|
75
|
+
record.cache_to_redis
|
|
76
|
+
MyRecord.find_from_redis(record.id) #=> #<MyRecord id: 1, name: "my_record", rate: "4.5">
|
|
77
|
+
|
|
78
|
+
MyRecord.cache_all # cache all records
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Configuration
|
|
82
|
+
|
|
83
|
+
```ruby
|
|
84
|
+
RedisCacheable::Configuration.configure do |config|
|
|
85
|
+
config.host = "10.0.0.1" # redis host (default: "localhost")
|
|
86
|
+
config.port = 6380 # redis port (default: 6379)
|
|
87
|
+
config.driver = :hiredis # redis port (default: :ruby)
|
|
88
|
+
config.namespace_prefix = "myapp" # see below.
|
|
89
|
+
config.pool_size = 10 # connection pool size (default: 5)
|
|
90
|
+
config.timeout = 10 # timeout seconds (default: 5)
|
|
91
|
+
end
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
`config.namespace_prefix` is useful, If multi application use same redis.
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
RedisCacheable.configure do |config|
|
|
98
|
+
config.namespace_prefix = "myapp" # see below.
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
class MyObject
|
|
102
|
+
include RedisCacheable
|
|
103
|
+
|
|
104
|
+
# ...
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# MyObject redis namespace is "myapp_my_object"
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Contributing
|
|
111
|
+
|
|
112
|
+
1. Fork it
|
|
113
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
114
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
115
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
116
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require 'redis_cacheable'
|
|
2
|
+
|
|
3
|
+
module RedisCacheable
|
|
4
|
+
module ActiveRecord
|
|
5
|
+
extend ActiveSupport::Concern
|
|
6
|
+
include ::RedisCacheable
|
|
7
|
+
|
|
8
|
+
module ClassMethods
|
|
9
|
+
def find_from_redis(key)
|
|
10
|
+
if data = super(key)
|
|
11
|
+
instantiate(data)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def cache_all
|
|
16
|
+
find_each do |record|
|
|
17
|
+
record.cache_to_redis
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
redis do |conn|
|
|
21
|
+
conn.save
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require 'singleton'
|
|
2
|
+
require 'active_support/configurable'
|
|
3
|
+
|
|
4
|
+
module RedisCacheable
|
|
5
|
+
class Configuration
|
|
6
|
+
include Singleton
|
|
7
|
+
include ActiveSupport::Configurable
|
|
8
|
+
|
|
9
|
+
config_accessor :host, :port, :driver, :namespace_prefix, :pool_size, :timeout
|
|
10
|
+
|
|
11
|
+
config.host ||= "localhost"
|
|
12
|
+
config.port ||= 6379
|
|
13
|
+
config.driver ||= :ruby
|
|
14
|
+
config.pool_size ||= 5
|
|
15
|
+
config.timeout ||= 5
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'active_support/core_ext/module'
|
|
2
|
+
require 'active_support/concern'
|
|
3
|
+
require 'redis_cacheable/configuration'
|
|
4
|
+
require 'connection_pool'
|
|
5
|
+
require 'redis-namespace'
|
|
6
|
+
|
|
7
|
+
module RedisCacheable
|
|
8
|
+
module Connectable
|
|
9
|
+
mattr_accessor :redis_connection
|
|
10
|
+
|
|
11
|
+
extend ActiveSupport::Concern
|
|
12
|
+
|
|
13
|
+
module ClassMethods
|
|
14
|
+
def redis_namespace
|
|
15
|
+
config = RedisCacheable::Configuration.config
|
|
16
|
+
namespace = [to_s.underscore]
|
|
17
|
+
namespace.unshift config.namespace_prefix if config.namespace_prefix
|
|
18
|
+
namespace.join("_")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def redis(&blk)
|
|
22
|
+
raise ArgumentError.new("Need block") unless blk
|
|
23
|
+
|
|
24
|
+
unless Connectable.redis_connection
|
|
25
|
+
config = RedisCacheable::Configuration.config
|
|
26
|
+
Connectable.redis_connection = ConnectionPool.new(size: config.pool_size, timeout: config.timeout) {
|
|
27
|
+
Redis.new(host: config.host, port: config.driver, driver: config.driver.to_sym)
|
|
28
|
+
}
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
Connectable.redis_connection.with do |conn|
|
|
32
|
+
namespaced_redis = Redis::Namespace.new(redis_namespace, redis: conn)
|
|
33
|
+
blk.call(namespaced_redis)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def redis(&blk)
|
|
39
|
+
raise ArgumentError.new("Need block") unless blk
|
|
40
|
+
|
|
41
|
+
self.class.redis(&blk)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
require 'active_support/concern'
|
|
2
|
+
require 'active_support/core_ext/class'
|
|
3
|
+
require 'multi_json'
|
|
4
|
+
|
|
5
|
+
require 'redis_cacheable/version'
|
|
6
|
+
require 'redis_cacheable/configuration'
|
|
7
|
+
require 'redis_cacheable/connectable'
|
|
8
|
+
|
|
9
|
+
module RedisCacheable
|
|
10
|
+
extend ActiveSupport::Concern
|
|
11
|
+
include Connectable
|
|
12
|
+
|
|
13
|
+
module ClassMethods
|
|
14
|
+
def redis_key(key)
|
|
15
|
+
@__redis_cache_key__ = key
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def redis_attrs(*attributes)
|
|
19
|
+
if attributes.first.is_a?(Proc)
|
|
20
|
+
@__redis_cache_attrs__ = attributes.first
|
|
21
|
+
else
|
|
22
|
+
@__redis_cache_attrs__ = attributes.flatten
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def find_from_redis(key)
|
|
27
|
+
redis do |conn|
|
|
28
|
+
json = conn.get(key)
|
|
29
|
+
return nil unless json
|
|
30
|
+
|
|
31
|
+
MultiJson.load(json)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @private
|
|
36
|
+
# for internal use
|
|
37
|
+
def __redis_cache_key__
|
|
38
|
+
@__redis_cache_key__ || :id
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# @private
|
|
42
|
+
# for internal use
|
|
43
|
+
def __redis_cache_attrs__
|
|
44
|
+
@__redis_cache_attrs__ || []
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def cache_to_redis
|
|
49
|
+
redis do |conn|
|
|
50
|
+
conn.set(redis_cache_key, MultiJson.dump(redis_cache_data))
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
def redis_cache_key
|
|
56
|
+
case __redis_cache_key__
|
|
57
|
+
when Proc
|
|
58
|
+
__redis_cache_key__.call(self)
|
|
59
|
+
when Symbol
|
|
60
|
+
send(__redis_cache_key__)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def redis_cache_data
|
|
65
|
+
case __redis_cache_attrs__
|
|
66
|
+
when Proc
|
|
67
|
+
__redis_cache_attrs__.call(self)
|
|
68
|
+
when Array
|
|
69
|
+
if respond_to?(:as_json)
|
|
70
|
+
options = __redis_cache_attrs__.present? ? {only: __redis_cache_attrs__} : {}
|
|
71
|
+
as_json(options)
|
|
72
|
+
else
|
|
73
|
+
__redis_cache_attrs__.each_with_object({}) do |attr, hash|
|
|
74
|
+
hash[attr] = send(attr)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def __redis_cache_key__
|
|
81
|
+
self.class.__redis_cache_key__
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def __redis_cache_attrs__
|
|
85
|
+
self.class.__redis_cache_attrs__
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'redis_cacheable/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "redis-cacheable"
|
|
8
|
+
spec.version = RedisCacheable::VERSION
|
|
9
|
+
spec.authors = ["joker1007"]
|
|
10
|
+
spec.email = ["kakyoin.hierophant@gmail.com"]
|
|
11
|
+
spec.summary = %q{Concern style helper of caching object to Redis}
|
|
12
|
+
spec.description = %q{Concern style helper of caching object to Redis}
|
|
13
|
+
spec.homepage = ""
|
|
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 "activesupport", ">= 3"
|
|
22
|
+
spec.add_dependency "redis"
|
|
23
|
+
spec.add_dependency "redis-namespace"
|
|
24
|
+
spec.add_dependency "connection_pool"
|
|
25
|
+
spec.add_dependency "multi_json"
|
|
26
|
+
|
|
27
|
+
spec.add_development_dependency "bundler", ">= 1.3"
|
|
28
|
+
spec.add_development_dependency "rake"
|
|
29
|
+
spec.add_development_dependency "activerecord"
|
|
30
|
+
spec.add_development_dependency "sqlite3"
|
|
31
|
+
spec.add_development_dependency "oj"
|
|
32
|
+
spec.add_development_dependency "rspec"
|
|
33
|
+
spec.add_development_dependency "mock_redis"
|
|
34
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
require 'active_record'
|
|
4
|
+
|
|
5
|
+
db_file = File.join(File.dirname(File.expand_path(__FILE__)), "..", "..", "test.sqlite3")
|
|
6
|
+
|
|
7
|
+
ActiveRecord::Base.establish_connection(
|
|
8
|
+
adapter: "sqlite3",
|
|
9
|
+
database: db_file
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
unless File.exists?(db_file)
|
|
13
|
+
ActiveRecord::Migration.create_table :cacheable_active_records, force: true do |t|
|
|
14
|
+
t.string :name
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class CacheableActiveRecord < ActiveRecord::Base
|
|
19
|
+
include RedisCacheable::ActiveRecord
|
|
20
|
+
redis_attrs :name
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
describe RedisCacheable::ActiveRecord do
|
|
24
|
+
let(:record) { CacheableActiveRecord.create(name: "target") }
|
|
25
|
+
|
|
26
|
+
before do
|
|
27
|
+
CacheableActiveRecord.delete_all
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
describe "#cache_to_redis" do
|
|
31
|
+
subject { record.cache_to_redis }
|
|
32
|
+
|
|
33
|
+
it "cache redis_attrs data to redis" do
|
|
34
|
+
record.cache_to_redis
|
|
35
|
+
record.redis do |conn|
|
|
36
|
+
expect(MultiJson.load(conn.get(record.id))).to eq({"name" => "target"})
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
describe ".find_from_redis" do
|
|
42
|
+
subject { CacheableActiveRecord.find_from_redis(record.id) }
|
|
43
|
+
|
|
44
|
+
before { record.cache_to_redis }
|
|
45
|
+
|
|
46
|
+
it { should be_a(CacheableActiveRecord) }
|
|
47
|
+
it { should be_persisted }
|
|
48
|
+
|
|
49
|
+
it "has cached attributes" do
|
|
50
|
+
expect(subject.name).to eq "target"
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
describe ".cache_all" do
|
|
55
|
+
subject { CacheableActiveRecord.cache_all }
|
|
56
|
+
|
|
57
|
+
before do
|
|
58
|
+
@record1 = CacheableActiveRecord.create(name: "target1")
|
|
59
|
+
@record2 = CacheableActiveRecord.create(name: "target2")
|
|
60
|
+
@record3 = CacheableActiveRecord.create(name: "target3")
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it "caches all saved records" do
|
|
64
|
+
subject
|
|
65
|
+
CacheableActiveRecord.redis do |conn|
|
|
66
|
+
expect(conn.get(@record1.id)).to be_present
|
|
67
|
+
expect(conn.get(@record2.id)).to be_present
|
|
68
|
+
expect(conn.get(@record3.id)).to be_present
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe RedisCacheable::Configuration do
|
|
4
|
+
describe ".config" do
|
|
5
|
+
subject { described_class.config }
|
|
6
|
+
|
|
7
|
+
it "has initial config data" do
|
|
8
|
+
expect(subject.host).to eq "localhost"
|
|
9
|
+
expect(subject.port).to eq 6379
|
|
10
|
+
expect(subject.driver).to eq :ruby
|
|
11
|
+
expect(subject.pool_size).to eq 5
|
|
12
|
+
expect(subject.timeout).to eq 5
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe RedisCacheable::Connectable do
|
|
4
|
+
describe "Class including RedisCacheable::Connectable" do
|
|
5
|
+
describe ".redis" do
|
|
6
|
+
it "call block with namespaced redis connection" do
|
|
7
|
+
ConnectableObject.redis do |conn|
|
|
8
|
+
expect(conn.namespace).to eq "connectable_object"
|
|
9
|
+
conn.set("key", "value")
|
|
10
|
+
expect(conn.get("key")).to eq "value"
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
describe "#redis" do
|
|
16
|
+
it "call block with namespaced redis connection" do
|
|
17
|
+
ConnectableObject.new.redis do |conn|
|
|
18
|
+
expect(conn.namespace).to eq "connectable_object"
|
|
19
|
+
conn.set("key", "value")
|
|
20
|
+
expect(conn.get("key")).to eq "value"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
class CacheableObject
|
|
4
|
+
include RedisCacheable
|
|
5
|
+
|
|
6
|
+
attr_reader :id, :name
|
|
7
|
+
|
|
8
|
+
redis_key :id
|
|
9
|
+
redis_attrs :id, :name
|
|
10
|
+
|
|
11
|
+
def initialize(attrs)
|
|
12
|
+
@id = attrs[:id]
|
|
13
|
+
@name = attrs[:name]
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class ProcCacheableObject < CacheableObject
|
|
18
|
+
redis_key :id
|
|
19
|
+
redis_attrs ->(object) {
|
|
20
|
+
object.id * 10
|
|
21
|
+
}
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class ProcKeyObject < CacheableObject
|
|
25
|
+
redis_key ->(object) { "object_#{object.id}" }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
describe RedisCacheable do
|
|
29
|
+
describe "Object including RedisCacheable" do
|
|
30
|
+
describe "#cache_to_redis" do
|
|
31
|
+
context "if redis_attrs is symbols" do
|
|
32
|
+
subject { CacheableObject.new(id: 1, name: "target").cache_to_redis }
|
|
33
|
+
|
|
34
|
+
it "cache redis_attrs data to redis" do
|
|
35
|
+
subject
|
|
36
|
+
expect(CacheableObject.find_from_redis(1)).to eq({"id" => 1, "name" => "target"})
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
context "if redis_attrs is proc" do
|
|
41
|
+
subject { ProcCacheableObject.new(id: 1, name: "target").cache_to_redis }
|
|
42
|
+
|
|
43
|
+
it "cache redis_attrs data to redis" do
|
|
44
|
+
subject
|
|
45
|
+
expect(ProcCacheableObject.find_from_redis(1)).to eq(10)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
context "if redis_key is proc" do
|
|
50
|
+
subject { ProcKeyObject.new(id: 1, name: "target").cache_to_redis }
|
|
51
|
+
|
|
52
|
+
it "use proc result as redis key" do
|
|
53
|
+
subject
|
|
54
|
+
expect(ProcKeyObject.find_from_redis("object_1")).to eq({"id" => 1, "name" => "target"})
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'coveralls'
|
|
2
|
+
Coveralls.wear!
|
|
3
|
+
|
|
4
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
|
5
|
+
require 'redis_cacheable'
|
|
6
|
+
require 'redis_cacheable/active_record'
|
|
7
|
+
require 'redis'
|
|
8
|
+
require 'mock_redis'
|
|
9
|
+
|
|
10
|
+
RSpec.configure do |config|
|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
|
12
|
+
config.run_all_when_everything_filtered = true
|
|
13
|
+
config.filter_run :focus
|
|
14
|
+
|
|
15
|
+
config.order = 'random'
|
|
16
|
+
|
|
17
|
+
config.before(:each) do
|
|
18
|
+
Redis.stub(:new).with(any_args) { MockRedis.new }
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class ConnectableObject
|
|
23
|
+
include RedisCacheable::Connectable
|
|
24
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: redis-cacheable
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- joker1007
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2013-11-05 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activesupport
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '3'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '3'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: redis
|
|
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: redis-namespace
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :runtime
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '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: '0'
|
|
62
|
+
type: :runtime
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: multi_json
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :runtime
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: bundler
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '1.3'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '1.3'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: rake
|
|
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: activerecord
|
|
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
|
+
- !ruby/object:Gem::Dependency
|
|
126
|
+
name: sqlite3
|
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - ">="
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: '0'
|
|
132
|
+
type: :development
|
|
133
|
+
prerelease: false
|
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
135
|
+
requirements:
|
|
136
|
+
- - ">="
|
|
137
|
+
- !ruby/object:Gem::Version
|
|
138
|
+
version: '0'
|
|
139
|
+
- !ruby/object:Gem::Dependency
|
|
140
|
+
name: oj
|
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
|
142
|
+
requirements:
|
|
143
|
+
- - ">="
|
|
144
|
+
- !ruby/object:Gem::Version
|
|
145
|
+
version: '0'
|
|
146
|
+
type: :development
|
|
147
|
+
prerelease: false
|
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
149
|
+
requirements:
|
|
150
|
+
- - ">="
|
|
151
|
+
- !ruby/object:Gem::Version
|
|
152
|
+
version: '0'
|
|
153
|
+
- !ruby/object:Gem::Dependency
|
|
154
|
+
name: rspec
|
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
|
156
|
+
requirements:
|
|
157
|
+
- - ">="
|
|
158
|
+
- !ruby/object:Gem::Version
|
|
159
|
+
version: '0'
|
|
160
|
+
type: :development
|
|
161
|
+
prerelease: false
|
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
163
|
+
requirements:
|
|
164
|
+
- - ">="
|
|
165
|
+
- !ruby/object:Gem::Version
|
|
166
|
+
version: '0'
|
|
167
|
+
- !ruby/object:Gem::Dependency
|
|
168
|
+
name: mock_redis
|
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
|
170
|
+
requirements:
|
|
171
|
+
- - ">="
|
|
172
|
+
- !ruby/object:Gem::Version
|
|
173
|
+
version: '0'
|
|
174
|
+
type: :development
|
|
175
|
+
prerelease: false
|
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
177
|
+
requirements:
|
|
178
|
+
- - ">="
|
|
179
|
+
- !ruby/object:Gem::Version
|
|
180
|
+
version: '0'
|
|
181
|
+
description: Concern style helper of caching object to Redis
|
|
182
|
+
email:
|
|
183
|
+
- kakyoin.hierophant@gmail.com
|
|
184
|
+
executables: []
|
|
185
|
+
extensions: []
|
|
186
|
+
extra_rdoc_files: []
|
|
187
|
+
files:
|
|
188
|
+
- ".gitignore"
|
|
189
|
+
- ".rspec"
|
|
190
|
+
- ".travis.yml"
|
|
191
|
+
- Gemfile
|
|
192
|
+
- LICENSE.txt
|
|
193
|
+
- README.md
|
|
194
|
+
- Rakefile
|
|
195
|
+
- lib/redis-cacheable.rb
|
|
196
|
+
- lib/redis_cacheable.rb
|
|
197
|
+
- lib/redis_cacheable/active_record.rb
|
|
198
|
+
- lib/redis_cacheable/configuration.rb
|
|
199
|
+
- lib/redis_cacheable/connectable.rb
|
|
200
|
+
- lib/redis_cacheable/version.rb
|
|
201
|
+
- redis-cacheable.gemspec
|
|
202
|
+
- spec/lib/redis_cacheable/active_record_spec.rb
|
|
203
|
+
- spec/lib/redis_cacheable/configuration_spec.rb
|
|
204
|
+
- spec/lib/redis_cacheable/connectable_spec.rb
|
|
205
|
+
- spec/lib/redis_cacheable_spec.rb
|
|
206
|
+
- spec/spec_helper.rb
|
|
207
|
+
homepage: ''
|
|
208
|
+
licenses:
|
|
209
|
+
- MIT
|
|
210
|
+
metadata: {}
|
|
211
|
+
post_install_message:
|
|
212
|
+
rdoc_options: []
|
|
213
|
+
require_paths:
|
|
214
|
+
- lib
|
|
215
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
216
|
+
requirements:
|
|
217
|
+
- - ">="
|
|
218
|
+
- !ruby/object:Gem::Version
|
|
219
|
+
version: '0'
|
|
220
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
221
|
+
requirements:
|
|
222
|
+
- - ">="
|
|
223
|
+
- !ruby/object:Gem::Version
|
|
224
|
+
version: '0'
|
|
225
|
+
requirements: []
|
|
226
|
+
rubyforge_project:
|
|
227
|
+
rubygems_version: 2.1.4
|
|
228
|
+
signing_key:
|
|
229
|
+
specification_version: 4
|
|
230
|
+
summary: Concern style helper of caching object to Redis
|
|
231
|
+
test_files:
|
|
232
|
+
- spec/lib/redis_cacheable/active_record_spec.rb
|
|
233
|
+
- spec/lib/redis_cacheable/configuration_spec.rb
|
|
234
|
+
- spec/lib/redis_cacheable/connectable_spec.rb
|
|
235
|
+
- spec/lib/redis_cacheable_spec.rb
|
|
236
|
+
- spec/spec_helper.rb
|