redis-cacheable 0.2.1 → 0.3.0

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: 31bc4cb6af9e7e18b7e2c8d3a30595fdc4895e25
4
- data.tar.gz: b940d81a3321cdecd05711648edd6b29848f1966
3
+ metadata.gz: 98a5a1b98e0a15129aca2cb7a233753616afeaf2
4
+ data.tar.gz: 090bb1ff29ddf917770383b4023e9199d2277429
5
5
  SHA512:
6
- metadata.gz: 4baeafafa7813e8d6f57ea66bb8262889d3d2e5c9fb0ab1a0e680b466199b068e01ff20fde55e551dc905be5acb1dd32f218d132154386cc60e016aae6794311
7
- data.tar.gz: 33291e1c2af0b7e58ed8779ab43e647147dd66676c4487da96c72fa3249ddc29730db60011223b2b230396aa8bc763aef0554dad7957ca781e336d68815b2f96
6
+ metadata.gz: c834765451f6c570044caae6b44d48599a5b9b805f7f84ecacef4e1c7e764aea796d7ea997a92bd74dc84c9c86730d67ac550d48484664b9e73a30a8ffe64814
7
+ data.tar.gz: 36e45970515cb1f874a09c4a7a414fc4e062cb0916a419e5bc5d93de1cb000e1e8246e0ecde220a3fb270c9344c6b8473b51770faac54e6c24c9ac482dadb077
@@ -67,6 +67,12 @@ module RedisCacheable
67
67
  end
68
68
  end
69
69
 
70
+ def del_from_redis(key)
71
+ redis do |conn|
72
+ conn.del(key)
73
+ end
74
+ end
75
+
70
76
  # @private
71
77
  # for internal use
72
78
  def __redis_cache_key__
@@ -80,9 +86,37 @@ module RedisCacheable
80
86
  end
81
87
  end
82
88
 
83
- def cache_to_redis
89
+ def cache_to_redis(expire: nil)
90
+ redis do |conn|
91
+ if expire
92
+ conn.setex(redis_cache_key, expire, MultiJson.dump(redis_cache_data))
93
+ else
94
+ conn.set(redis_cache_key, MultiJson.dump(redis_cache_data))
95
+ end
96
+ end
97
+ end
98
+
99
+ def del_from_redis
100
+ redis do |conn|
101
+ conn.del(redis_cache_key)
102
+ end
103
+ end
104
+
105
+ def expire_redis(sec)
106
+ redis do |conn|
107
+ conn.expire(redis_cache_key, sec)
108
+ end
109
+ end
110
+
111
+ def expireat_redis(unix_time)
112
+ redis do |conn|
113
+ conn.expireat(redis_cache_key, unix_time)
114
+ end
115
+ end
116
+
117
+ def ttl_redis
84
118
  redis do |conn|
85
- conn.set(redis_cache_key, MultiJson.dump(redis_cache_data))
119
+ conn.ttl(redis_cache_key)
86
120
  end
87
121
  end
88
122
 
@@ -1,3 +1,3 @@
1
1
  module RedisCacheable
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "activesupport", ">= 3"
22
- spec.add_dependency "redis"
22
+ spec.add_dependency "redis", ">= 3"
23
23
  spec.add_dependency "redis-namespace"
24
24
  spec.add_dependency "connection_pool"
25
25
  spec.add_dependency "multi_json"
@@ -32,6 +32,10 @@ end
32
32
  class InheritChild < InheritBase; end
33
33
 
34
34
  describe RedisCacheable do
35
+ before do
36
+ Redis.new.flushall
37
+ end
38
+
35
39
  describe "Object including RedisCacheable" do
36
40
  describe "#cache_to_redis" do
37
41
  context "if redis_attrs is symbols" do
@@ -43,6 +47,16 @@ describe RedisCacheable do
43
47
  end
44
48
  end
45
49
 
50
+ context "if given expire option" do
51
+ subject { CacheableObject.new(id: 1, name: "target").cache_to_redis(expire: 1) }
52
+
53
+ it "cache redis_attrs data to redis and expired after given time" do
54
+ subject
55
+ sleep 1
56
+ expect(CacheableObject.find_from_redis(1)).to be_nil
57
+ end
58
+ end
59
+
46
60
  context "if redis_attrs is proc" do
47
61
  subject { ProcCacheableObject.new(id: 1, name: "target").cache_to_redis }
48
62
 
@@ -70,5 +84,49 @@ describe RedisCacheable do
70
84
  end
71
85
  end
72
86
  end
87
+
88
+ describe "#del_from_redis" do
89
+ it "delete cached data from redis" do
90
+ object = CacheableObject.new(id: 1, name: "target")
91
+ object.cache_to_redis
92
+ expect(CacheableObject.find_from_redis(1)).to eq({"id" => 1, "name" => "target"})
93
+ object.del_from_redis
94
+
95
+ expect(CacheableObject.find_from_redis(1)).to be_nil
96
+ end
97
+ end
98
+
99
+ describe "#expire_redis" do
100
+ it "set expire time to cached data" do
101
+ object = CacheableObject.new(id: 1, name: "target")
102
+ object.cache_to_redis
103
+ expect(CacheableObject.find_from_redis(1)).to eq({"id" => 1, "name" => "target"})
104
+ object.expire_redis(2)
105
+ expect(CacheableObject.find_from_redis(1)).to eq({"id" => 1, "name" => "target"})
106
+ sleep 2
107
+ expect(CacheableObject.find_from_redis(1)).to be_nil
108
+ end
109
+ end
110
+
111
+ describe "#expireat_redis" do
112
+ it "set expire time to cached data by unix_time" do
113
+ object = CacheableObject.new(id: 1, name: "target")
114
+ object.cache_to_redis
115
+ expect(CacheableObject.find_from_redis(1)).to eq({"id" => 1, "name" => "target"})
116
+ object.expireat_redis((Time.now + 2).to_i)
117
+ expect(CacheableObject.find_from_redis(1)).to eq({"id" => 1, "name" => "target"})
118
+ sleep 2
119
+ expect(CacheableObject.find_from_redis(1)).to be_nil
120
+ end
121
+ end
122
+
123
+ describe "#ttl_redis" do
124
+ it "returns time to expire as second" do
125
+ object = CacheableObject.new(id: 1, name: "target")
126
+ object.cache_to_redis
127
+ object.expire_redis(2)
128
+ expect((1.0..2.0).cover?(object.ttl_redis)).to be_true
129
+ end
130
+ end
73
131
  end
74
132
  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.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - joker1007
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-20 00:00:00.000000000 Z
11
+ date: 2014-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '3'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: redis-namespace
43
43
  requirement: !ruby/object:Gem::Requirement