redis-cacheable 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c1551b9cda86647b91fc64934f9bc5d3db043a4
4
- data.tar.gz: 27fde316e23c41e16ea71dda78164a28ed552047
3
+ metadata.gz: d39f90df0af7cdab3b63487d59fdd1dae095cdd5
4
+ data.tar.gz: f34c8c7764c257b3a9a361b420b317a4b90a68e8
5
5
  SHA512:
6
- metadata.gz: a7bb129cab54e4436221fcb8e70f24cb8df39b584d1304f8ae96f82404bcab961eb7a8ce62044242af646597bbf15d6bc08862164ce24f53f460fc3295cb4928
7
- data.tar.gz: b51ef2783bde32b7bef3001abb99ca9c9129f0063b49dae98183d74536e76af6006075dfd65e4b6d22ccac20389f5559b06e770823ea146424ea4719d67cdb3c
6
+ metadata.gz: c537829ce3c8b5c26e0b27fbf3d55180b6e7bcfadebb9c4b4910bc0bd77b98ecbabe6d0736e9d7930867f701d258f77023dd3fe0f195e89cd13174d30f691d50
7
+ data.tar.gz: 1dda563700067ef3f3fa5b4b64bebed28b10c1d7936aa265e203c37e267b59366c5a39b425f9ff5e4d57e2fac02b6e3274ae24732caa053e89c86d6aa3823f2b
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # redis-cacheable
2
+ [![Gem Version](https://badge.fury.io/rb/redis-cacheable.png)](http://badge.fury.io/rb/redis-cacheable)
2
3
  [![Build Status](https://travis-ci.org/joker1007/redis-cacheable.png?branch=master)](https://travis-ci.org/joker1007/redis-cacheable)
3
4
  [![Coverage Status](https://coveralls.io/repos/joker1007/redis-cacheable/badge.png?branch=master)](https://coveralls.io/r/joker1007/redis-cacheable?branch=master)
4
5
  [![Code Climate](https://codeclimate.com/github/joker1007/redis-cacheable.png)](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
- def cache_all
16
- find_each do |record|
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.with do |conn|
32
- namespaced_redis = Redis::Namespace.new(redis_namespace, redis: conn)
33
- blk.call(namespaced_redis)
34
- end
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
 
@@ -1,3 +1,3 @@
1
1
  module RedisCacheable
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -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)
@@ -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($/)
@@ -1,5 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class ConnectableObject
4
+ include RedisCacheable::Connectable
5
+ end
6
+
3
7
  describe RedisCacheable::Connectable do
4
8
  describe "Class including RedisCacheable::Connectable" do
5
9
  describe ".redis" do
data/spec/spec_helper.rb CHANGED
@@ -18,7 +18,3 @@ RSpec.configure do |config|
18
18
  Redis.stub(:new).with(any_args) { MockRedis.new }
19
19
  end
20
20
  end
21
-
22
- class ConnectableObject
23
- include RedisCacheable::Connectable
24
- end
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.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-05 00:00:00.000000000 Z
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: