cache_store_redis 0.4.4 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cache_store_redis/version.rb +1 -1
- data/lib/cache_store_redis.rb +101 -63
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f09f961f03ff2ebfa552c67f2cd8e68ca90d4d7
|
4
|
+
data.tar.gz: edbcce0a3fb317ae7a1e3659d6edd5c524141cf4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84bd996123e42b669a1c2851d8cc4e83e8181b285e10217ce15a560ac8cc65a5c015a40b58b2a63161e8b6e98c27ff499043f3943c5bcb49b1e3d30d769d6096
|
7
|
+
data.tar.gz: 1a5ddbd79f7da39d5249b1a298414ee5d0330b0e8110400d4b4d65f3d52b5b0576c67226c8b4c80c3f9a360ea83e869e4d59cedf102caf5cf85ab4ed3f9fa5f9
|
data/lib/cache_store_redis.rb
CHANGED
@@ -1,117 +1,159 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'thread'
|
1
4
|
require 'cache_store_redis/version'
|
2
5
|
require 'redis'
|
3
6
|
require 'securerandom'
|
4
|
-
require 'connection_pool'
|
5
7
|
|
6
|
-
#This class is used to implement a redis cache store.
|
7
|
-
#This class is used for interacting with a redis based cache store.
|
8
|
+
# This class is used to implement a redis cache store.
|
8
9
|
class RedisCacheStore
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
if config == nil
|
13
|
-
Redis.new
|
14
|
-
else
|
15
|
-
Redis.new(config)
|
16
|
-
end
|
10
|
+
def initialize(namespace = nil, config = nil)
|
11
|
+
unless RUBY_PLATFORM == 'java'
|
12
|
+
require 'oj'
|
17
13
|
end
|
18
|
-
end
|
19
14
|
|
20
|
-
|
21
|
-
@
|
22
|
-
|
15
|
+
@namespace = namespace
|
16
|
+
@config = config
|
17
|
+
@queue = Queue.new
|
23
18
|
|
24
|
-
|
25
|
-
@
|
19
|
+
@connections_created = 0
|
20
|
+
@connections_in_use = 0
|
21
|
+
@mutex = Mutex.new
|
22
|
+
@enable_stats = false
|
26
23
|
end
|
27
24
|
|
28
|
-
def
|
25
|
+
def enable_stats=(value)
|
26
|
+
@enable_stats = value
|
27
|
+
end
|
29
28
|
|
30
|
-
|
31
|
-
|
29
|
+
def increment_created_stat
|
30
|
+
@mutex.synchronize do
|
31
|
+
@connections_created += 1
|
32
32
|
end
|
33
|
+
end
|
33
34
|
|
34
|
-
|
35
|
-
@
|
35
|
+
def increment_using_stat
|
36
|
+
@mutex.synchronize do
|
37
|
+
@connections_in_use += 1
|
38
|
+
end
|
36
39
|
end
|
37
40
|
|
38
|
-
def
|
39
|
-
|
40
|
-
|
41
|
-
raise 'nil redis client returned from pool' if client.nil?
|
42
|
-
yield client
|
43
|
-
end
|
44
|
-
else
|
45
|
-
# allow single client to be used when pool size is zero
|
46
|
-
@client ||= Redis.new(@config)
|
47
|
-
yield @client
|
41
|
+
def decrement_using_stat
|
42
|
+
@mutex.synchronize do
|
43
|
+
@connections_in_use -= 1
|
48
44
|
end
|
49
45
|
end
|
50
46
|
|
51
|
-
#This method is called to configure the connection to the cache store.
|
52
|
-
def configure(host = 'localhost',
|
53
|
-
|
47
|
+
# This method is called to configure the connection to the cache store.
|
48
|
+
def configure(host = 'localhost',
|
49
|
+
port = 6379,
|
50
|
+
db = 'default',
|
51
|
+
password = nil,
|
52
|
+
driver: nil,
|
53
|
+
url: nil,
|
54
|
+
connect_timeout: 0.5,
|
55
|
+
read_timeout: 1,
|
56
|
+
write_timeout: 0.5)
|
57
|
+
if !url.nil?
|
54
58
|
@config = {}
|
55
59
|
@config[:url] = url
|
56
60
|
@config[:db] = db
|
57
61
|
else
|
58
|
-
@config = { :
|
62
|
+
@config = { host: host, port: port, db: db }
|
59
63
|
end
|
60
|
-
|
61
|
-
|
64
|
+
|
65
|
+
@config[:password] = password unless password .nil?
|
66
|
+
@config[:driver] = driver unless driver.nil?
|
67
|
+
|
68
|
+
@config[:connect_timeout] = connect_timeout
|
69
|
+
@config[:read_timeout] = read_timeout
|
70
|
+
@config[:write_timeout] = write_timeout
|
71
|
+
end
|
72
|
+
|
73
|
+
def fetch_client
|
74
|
+
begin
|
75
|
+
@queue.pop(true)
|
76
|
+
rescue
|
77
|
+
increment_created_stat
|
78
|
+
Redis.new(@config)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def clean
|
83
|
+
while @queue.length.positive?
|
84
|
+
client = @queue.pop(true)
|
85
|
+
client.close
|
62
86
|
end
|
63
|
-
|
64
|
-
|
87
|
+
end
|
88
|
+
|
89
|
+
def log_stats
|
90
|
+
return unless @enable_stats == true
|
91
|
+
S1Logging.logger.debug do
|
92
|
+
"[#{self.class}] - REDIS Connection Stats. Process: #{Process.pid} | " \
|
93
|
+
"Created: #{@connections_created} | Pending: #{@queue.length} | In use: #{@connections_in_use}"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def with_client
|
98
|
+
log_stats
|
99
|
+
begin
|
100
|
+
client = fetch_client
|
101
|
+
increment_using_stat
|
102
|
+
log_stats
|
103
|
+
yield client
|
104
|
+
ensure
|
105
|
+
@queue.push(client)
|
106
|
+
decrement_using_stat
|
107
|
+
log_stats
|
65
108
|
end
|
66
109
|
end
|
67
110
|
|
68
|
-
#This method is called to set a value within this cache store by it's key.
|
111
|
+
# This method is called to set a value within this cache store by it's key.
|
69
112
|
#
|
70
113
|
# @param key [String] This is the unique key to reference the value being set within this cache store.
|
71
114
|
# @param value [Object] This is the value to set within this cache store.
|
72
115
|
# @param expires_in [Integer] This is the number of seconds from the current time that this value should expire.
|
73
116
|
def set(key, value, expires_in = 0)
|
74
|
-
v = nil
|
75
117
|
k = build_key(key)
|
76
118
|
|
77
|
-
if value
|
78
|
-
|
79
|
-
|
119
|
+
v = if value.nil?
|
120
|
+
nil
|
121
|
+
else
|
122
|
+
serialize(value)
|
123
|
+
end
|
80
124
|
|
81
125
|
with_client do |client|
|
82
126
|
client.multi do
|
83
127
|
client.set(k, v)
|
84
128
|
|
85
|
-
|
86
|
-
client.expire(k, expires_in)
|
87
|
-
end
|
129
|
+
client.expire(k, expires_in) if expires_in.positive?
|
88
130
|
end
|
89
131
|
end
|
90
|
-
|
91
132
|
end
|
92
133
|
|
93
|
-
#This method is called to get a value from this cache store by it's unique key.
|
134
|
+
# This method is called to get a value from this cache store by it's unique key.
|
94
135
|
#
|
95
136
|
# @param key [String] This is the unique key to reference the value to fetch from within this cache store.
|
96
|
-
# @param expires_in [Integer] This is the number of seconds from the current time that this value should expire.
|
97
|
-
#
|
137
|
+
# @param expires_in [Integer] This is the number of seconds from the current time that this value should expire.
|
138
|
+
# (This is used in conjunction with the block to hydrate the cache key if it is empty.)
|
139
|
+
# @param &block [Block] This block is provided to hydrate this cache store with the value for the request key
|
140
|
+
# when it is not found.
|
98
141
|
# @return [Object] The value for the specified unique key within the cache store.
|
99
142
|
def get(key, expires_in = 0, &block)
|
100
|
-
|
101
143
|
k = build_key(key)
|
102
144
|
|
103
145
|
value = with_client do |client|
|
104
146
|
client.get(k)
|
105
147
|
end
|
106
148
|
|
107
|
-
value = deserialize(value) unless value
|
149
|
+
value = deserialize(value) unless value.nil?
|
108
150
|
|
109
151
|
if value.nil? && block_given?
|
110
152
|
value = yield
|
111
153
|
set(k, value, expires_in)
|
112
154
|
end
|
113
155
|
|
114
|
-
|
156
|
+
value
|
115
157
|
end
|
116
158
|
|
117
159
|
# This method is called to remove a value from this cache store by it's unique key.
|
@@ -142,7 +184,6 @@ class RedisCacheStore
|
|
142
184
|
end
|
143
185
|
end
|
144
186
|
|
145
|
-
|
146
187
|
private
|
147
188
|
|
148
189
|
def serialize(object)
|
@@ -162,13 +203,10 @@ class RedisCacheStore
|
|
162
203
|
end
|
163
204
|
|
164
205
|
def build_key(key)
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
k = key.to_s
|
206
|
+
if !@namespace.nil?
|
207
|
+
@namespace + ':' + key.to_s
|
208
|
+
else
|
209
|
+
key.to_s
|
170
210
|
end
|
171
|
-
k
|
172
211
|
end
|
173
|
-
|
174
212
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache_store_redis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- vaughanbrittonsage
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-01-
|
11
|
+
date: 2018-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,20 +66,6 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: connection_pool
|
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
69
|
- !ruby/object:Gem::Dependency
|
84
70
|
name: oj
|
85
71
|
requirement: !ruby/object:Gem::Requirement
|