cache_store_redis 0.4.2-java → 0.5.4-java
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 +106 -57
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66c8348a23aae76d29cae462e4dac22e9b433059a6c3fcdcdff6c369f77b4f3d
|
4
|
+
data.tar.gz: d448b2da6efb3fd89c5692233a29c5416e49e8741ecb12ee9cab6d35e88e8607
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30df78134211b7e1e92a1af37acf35e8f8ddc169e0124ef7f4d707330f113ac44bb0de8e8cb8657cb46e4ddcd40d07fe67838ddbc6d231507bf0efe5365af3c0
|
7
|
+
data.tar.gz: e21a1afc728d7e1b884135328463d4da7e987e6546383194f49f7e2c536bbaff5467ef8d98f0adb7e9ac3aaa28c076edd00defeb7f295d484030c8cdc939c8f1
|
data/lib/cache_store_redis.rb
CHANGED
@@ -1,110 +1,163 @@
|
|
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
|
+
def decrement_using_stat
|
42
|
+
@mutex.synchronize do
|
43
|
+
@connections_in_use -= 1
|
41
44
|
end
|
42
45
|
end
|
43
46
|
|
44
|
-
#This method is called to configure the connection to the cache store.
|
45
|
-
def configure(host = 'localhost',
|
46
|
-
|
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?
|
47
58
|
@config = {}
|
48
59
|
@config[:url] = url
|
49
60
|
@config[:db] = db
|
50
61
|
else
|
51
|
-
@config = { :
|
62
|
+
@config = { host: host, port: port, db: db }
|
52
63
|
end
|
53
|
-
|
54
|
-
|
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)
|
55
79
|
end
|
56
|
-
|
57
|
-
|
80
|
+
end
|
81
|
+
|
82
|
+
def clean
|
83
|
+
while @queue.length.positive?
|
84
|
+
client = @queue.pop(true)
|
85
|
+
client.close
|
86
|
+
end
|
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}"
|
58
94
|
end
|
59
95
|
end
|
60
96
|
|
61
|
-
|
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
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# This method is called to set a value within this cache store by it's key.
|
62
112
|
#
|
63
113
|
# @param key [String] This is the unique key to reference the value being set within this cache store.
|
64
114
|
# @param value [Object] This is the value to set within this cache store.
|
65
115
|
# @param expires_in [Integer] This is the number of seconds from the current time that this value should expire.
|
66
116
|
def set(key, value, expires_in = 0)
|
67
|
-
v = nil
|
68
117
|
k = build_key(key)
|
69
118
|
|
70
|
-
if value
|
71
|
-
|
72
|
-
|
119
|
+
v = if value.nil? || (value.is_a?(String) && value.strip.empty?)
|
120
|
+
nil
|
121
|
+
else
|
122
|
+
serialize(value)
|
123
|
+
end
|
73
124
|
|
74
125
|
with_client do |client|
|
75
126
|
client.multi do
|
76
127
|
client.set(k, v)
|
77
128
|
|
78
|
-
|
79
|
-
client.expire(k, expires_in)
|
80
|
-
end
|
129
|
+
client.expire(k, expires_in) if expires_in.positive?
|
81
130
|
end
|
82
131
|
end
|
83
|
-
|
84
132
|
end
|
85
133
|
|
86
|
-
#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.
|
87
135
|
#
|
88
136
|
# @param key [String] This is the unique key to reference the value to fetch from within this cache store.
|
89
|
-
# @param expires_in [Integer] This is the number of seconds from the current time that this value should expire.
|
90
|
-
#
|
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.
|
91
141
|
# @return [Object] The value for the specified unique key within the cache store.
|
92
142
|
def get(key, expires_in = 0, &block)
|
93
|
-
|
94
143
|
k = build_key(key)
|
95
144
|
|
96
145
|
value = with_client do |client|
|
97
146
|
client.get(k)
|
98
147
|
end
|
99
148
|
|
100
|
-
value
|
149
|
+
if !value.nil? && value.delete('\"').strip.empty?
|
150
|
+
value = nil
|
151
|
+
else
|
152
|
+
value = deserialize(value) unless value.nil?
|
153
|
+
end
|
101
154
|
|
102
155
|
if value.nil? && block_given?
|
103
156
|
value = yield
|
104
|
-
set(
|
157
|
+
set(key, value, expires_in)
|
105
158
|
end
|
106
159
|
|
107
|
-
|
160
|
+
value
|
108
161
|
end
|
109
162
|
|
110
163
|
# This method is called to remove a value from this cache store by it's unique key.
|
@@ -135,7 +188,6 @@ class RedisCacheStore
|
|
135
188
|
end
|
136
189
|
end
|
137
190
|
|
138
|
-
|
139
191
|
private
|
140
192
|
|
141
193
|
def serialize(object)
|
@@ -155,13 +207,10 @@ class RedisCacheStore
|
|
155
207
|
end
|
156
208
|
|
157
209
|
def build_key(key)
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
k = key.to_s
|
210
|
+
if !@namespace.nil?
|
211
|
+
@namespace + ':' + key.to_s
|
212
|
+
else
|
213
|
+
key.to_s
|
163
214
|
end
|
164
|
-
k
|
165
215
|
end
|
166
|
-
|
167
216
|
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
|
4
|
+
version: 0.5.4
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- vaughanbrittonsage
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,20 +66,6 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
requirement: !ruby/object:Gem::Requirement
|
71
|
-
requirements:
|
72
|
-
- - ">="
|
73
|
-
- !ruby/object:Gem::Version
|
74
|
-
version: '0'
|
75
|
-
name: connection_pool
|
76
|
-
prerelease: false
|
77
|
-
type: :runtime
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
69
|
description: This is the redis cache_store implementation.
|
84
70
|
email:
|
85
71
|
- vaughan.britton@sage.com
|