redis-cacheable 0.1.1 → 0.1.2
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 +4 -4
- data/README.md +1 -0
- data/lib/redis_cacheable/active_record.rb +8 -2
- data/lib/redis_cacheable/connectable.rb +21 -4
- data/lib/redis_cacheable/version.rb +1 -1
- data/lib/redis_cacheable.rb +31 -0
- data/redis-cacheable.gemspec +1 -1
- data/spec/lib/redis_cacheable/connectable_spec.rb +4 -0
- data/spec/spec_helper.rb +0 -4
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d39f90df0af7cdab3b63487d59fdd1dae095cdd5
|
4
|
+
data.tar.gz: f34c8c7764c257b3a9a361b420b317a4b90a68e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c537829ce3c8b5c26e0b27fbf3d55180b6e7bcfadebb9c4b4910bc0bd77b98ecbabe6d0736e9d7930867f701d258f77023dd3fe0f195e89cd13174d30f691d50
|
7
|
+
data.tar.gz: 1dda563700067ef3f3fa5b4b64bebed28b10c1d7936aa265e203c37e267b59366c5a39b425f9ff5e4d57e2fac02b6e3274ae24732caa053e89c86d6aa3823f2b
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# redis-cacheable
|
2
|
+
[](http://badge.fury.io/rb/redis-cacheable)
|
2
3
|
[](https://travis-ci.org/joker1007/redis-cacheable)
|
3
4
|
[](https://coveralls.io/r/joker1007/redis-cacheable?branch=master)
|
4
5
|
[](https://codeclimate.com/github/joker1007/redis-cacheable)
|
@@ -1,19 +1,25 @@
|
|
1
1
|
require 'redis_cacheable'
|
2
2
|
|
3
|
+
# Specialized for ActiveRecord
|
3
4
|
module RedisCacheable
|
4
5
|
module ActiveRecord
|
5
6
|
extend ActiveSupport::Concern
|
6
7
|
include ::RedisCacheable
|
7
8
|
|
8
9
|
module ClassMethods
|
10
|
+
# @param [Object] key
|
11
|
+
# @return [ActiveRecord::Base] ActiveRecord instantiated object
|
9
12
|
def find_from_redis(key)
|
10
13
|
if data = super(key)
|
11
14
|
instantiate(data)
|
12
15
|
end
|
13
16
|
end
|
14
17
|
|
15
|
-
|
16
|
-
|
18
|
+
# cache all persisted records
|
19
|
+
# @param [Hash] options pass to find_each method
|
20
|
+
# @return [void]
|
21
|
+
def cache_all(options = {})
|
22
|
+
find_each(options) do |record|
|
17
23
|
record.cache_to_redis
|
18
24
|
end
|
19
25
|
|
@@ -21,6 +21,22 @@ module RedisCacheable
|
|
21
21
|
def redis(&blk)
|
22
22
|
raise ArgumentError.new("Need block") unless blk
|
23
23
|
|
24
|
+
connection = __ensure_redis_connection__
|
25
|
+
|
26
|
+
case connection
|
27
|
+
when ConnectionPool
|
28
|
+
connection.with do |conn|
|
29
|
+
blk.call(__wrap_namespace__(conn))
|
30
|
+
end
|
31
|
+
when Redis
|
32
|
+
blk.call(__wrap_namespace__(connection))
|
33
|
+
else
|
34
|
+
raise "Not redis connection"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
def __ensure_redis_connection__
|
24
40
|
unless Connectable.redis_connection
|
25
41
|
config = RedisCacheable::Configuration.config
|
26
42
|
Connectable.redis_connection = ConnectionPool.new(size: config.pool_size, timeout: config.timeout) {
|
@@ -28,10 +44,11 @@ module RedisCacheable
|
|
28
44
|
}
|
29
45
|
end
|
30
46
|
|
31
|
-
Connectable.redis_connection
|
32
|
-
|
33
|
-
|
34
|
-
|
47
|
+
Connectable.redis_connection
|
48
|
+
end
|
49
|
+
|
50
|
+
def __wrap_namespace__(connection)
|
51
|
+
Redis::Namespace.new(redis_namespace, redis: connection)
|
35
52
|
end
|
36
53
|
end
|
37
54
|
|
data/lib/redis_cacheable.rb
CHANGED
@@ -6,15 +6,44 @@ require 'redis_cacheable/version'
|
|
6
6
|
require 'redis_cacheable/configuration'
|
7
7
|
require 'redis_cacheable/connectable'
|
8
8
|
|
9
|
+
# Usage:
|
10
|
+
#
|
11
|
+
# class CacheableObject
|
12
|
+
# include RedisCacheable
|
13
|
+
#
|
14
|
+
# attr_reader :id, :name
|
15
|
+
#
|
16
|
+
# redis_key :id
|
17
|
+
# redis_attrs :id, :name
|
18
|
+
#
|
19
|
+
# def initialize(attrs)
|
20
|
+
# @id = attrs[:id]
|
21
|
+
# @name = attrs[:name]
|
22
|
+
# end
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# CacheableObject.new(id: 1, name: "object").cache_to_redis
|
26
|
+
# CacheableObject.find_from_redis(1) # => {"id" => 1, "name" => "target"}
|
9
27
|
module RedisCacheable
|
10
28
|
extend ActiveSupport::Concern
|
11
29
|
include Connectable
|
12
30
|
|
13
31
|
module ClassMethods
|
32
|
+
|
33
|
+
# @param [Symbol || Proc] key used by redis.set method
|
34
|
+
# @example If Symbol, param is method name
|
35
|
+
# redis_key :uuid
|
36
|
+
# @example If Proc
|
37
|
+
# redis_key ->(user) {"#{user.id}_{user.email}"}
|
14
38
|
def redis_key(key)
|
15
39
|
@__redis_cache_key__ = key
|
16
40
|
end
|
17
41
|
|
42
|
+
# @param [Array<Symbol> || Proc] attributes
|
43
|
+
# @example If Symbols, param is method name
|
44
|
+
# redis_attrs :uuid, :name, :email
|
45
|
+
# @example If Proc, proc must return JSON serializable object
|
46
|
+
# redis_attrs ->(user) { {id: user.id, name: user.name, email: user.email} }
|
18
47
|
def redis_attrs(*attributes)
|
19
48
|
if attributes.first.is_a?(Proc)
|
20
49
|
@__redis_cache_attrs__ = attributes.first
|
@@ -23,6 +52,8 @@ module RedisCacheable
|
|
23
52
|
end
|
24
53
|
end
|
25
54
|
|
55
|
+
# @param [Object] key JSON
|
56
|
+
# @return [String || Number || Array || Hash]
|
26
57
|
def find_from_redis(key)
|
27
58
|
redis do |conn|
|
28
59
|
json = conn.get(key)
|
data/redis-cacheable.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["kakyoin.hierophant@gmail.com"]
|
11
11
|
spec.summary = %q{Concern style helper of caching object to Redis}
|
12
12
|
spec.description = %q{Concern style helper of caching object to Redis}
|
13
|
-
spec.homepage = ""
|
13
|
+
spec.homepage = "https://github.com/joker1007/redis-cacheable"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis-cacheable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- joker1007
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -204,7 +204,7 @@ files:
|
|
204
204
|
- spec/lib/redis_cacheable/connectable_spec.rb
|
205
205
|
- spec/lib/redis_cacheable_spec.rb
|
206
206
|
- spec/spec_helper.rb
|
207
|
-
homepage:
|
207
|
+
homepage: https://github.com/joker1007/redis-cacheable
|
208
208
|
licenses:
|
209
209
|
- MIT
|
210
210
|
metadata: {}
|
@@ -234,4 +234,3 @@ test_files:
|
|
234
234
|
- spec/lib/redis_cacheable/connectable_spec.rb
|
235
235
|
- spec/lib/redis_cacheable_spec.rb
|
236
236
|
- spec/spec_helper.rb
|
237
|
-
has_rdoc:
|