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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b7648636075a74c0e4bd9c6f7ae51605c0b57184b1298ca64d53bee697b3e515
4
- data.tar.gz: c560d50aa53e1dd6d9d067c38698d2d6c44d0684a5eeb1c99fd2a0592dd48358
3
+ metadata.gz: 66c8348a23aae76d29cae462e4dac22e9b433059a6c3fcdcdff6c369f77b4f3d
4
+ data.tar.gz: d448b2da6efb3fd89c5692233a29c5416e49e8741ecb12ee9cab6d35e88e8607
5
5
  SHA512:
6
- metadata.gz: 2bfe8206e8dea8747f6a1042778a85e579327680d7f620cdd666188932b975af0fd9810ea255aa456fe65d161e694c2a17ab10cd6457eda7ea657b35914016b3
7
- data.tar.gz: 01d3871337057dd60c949a5922d01c4919b0844deaa373e13dd0b9c4140c86962ab3e2e691c9e64be226911bb5db79c3859e19955eb67f4978d3fe9e7c912176
6
+ metadata.gz: 30df78134211b7e1e92a1af37acf35e8f8ddc169e0124ef7f4d707330f113ac44bb0de8e8cb8657cb46e4ddcd40d07fe67838ddbc6d231507bf0efe5365af3c0
7
+ data.tar.gz: e21a1afc728d7e1b884135328463d4da7e987e6546383194f49f7e2c536bbaff5467ef8d98f0adb7e9ac3aaa28c076edd00defeb7f295d484030c8cdc939c8f1
@@ -1,3 +1,3 @@
1
1
  module CacheStoreRedis
2
- VERSION = '0.4.2'
2
+ VERSION = '0.5.4'
3
3
  end
@@ -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
- def self.pool(config)
11
- @pool ||= ConnectionPool.new(size: pool_size, timeout: pool_timeout) do
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
- def self.pool_size
21
- @pool_size ||= ENV['CACHE_STORE_POOL_SIZE'] || 10
22
- end
15
+ @namespace = namespace
16
+ @config = config
17
+ @queue = Queue.new
23
18
 
24
- def self.pool_timeout
25
- @pool_size ||= ENV['CACHE_STORE_POOL_TIMEOUT'] || 1
19
+ @connections_created = 0
20
+ @connections_in_use = 0
21
+ @mutex = Mutex.new
22
+ @enable_stats = false
26
23
  end
27
24
 
28
- def initialize(namespace = nil, config = nil)
25
+ def enable_stats=(value)
26
+ @enable_stats = value
27
+ end
29
28
 
30
- if RUBY_PLATFORM != 'java'
31
- require 'oj'
29
+ def increment_created_stat
30
+ @mutex.synchronize do
31
+ @connections_created += 1
32
32
  end
33
+ end
33
34
 
34
- @namespace = namespace
35
- @config = config
35
+ def increment_using_stat
36
+ @mutex.synchronize do
37
+ @connections_in_use += 1
38
+ end
36
39
  end
37
40
 
38
- def with_client
39
- self.class.pool(@config).with do |client|
40
- yield client
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', port = 6379, db = 'default', password = nil, driver: nil, url: nil)
46
- if url != nil
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 = { :host => host, :port => port, :db => db }
62
+ @config = { host: host, port: port, db: db }
52
63
  end
53
- if password != nil
54
- @config[:password] = password
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
- if driver != nil
57
- @config[:driver] = driver
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
- #This method is called to set a value within this cache store by it's key.
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 != nil
71
- v = serialize(value)
72
- end
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
- if expires_in > 0
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. (This is used in conjunction with the block to hydrate the cache key if it is empty.)
90
- # @param &block [Block] This block is provided to hydrate this cache store with the value for the request key when it is not found.
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 = deserialize(value) unless value == nil
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(k, value, expires_in)
157
+ set(key, value, expires_in)
105
158
  end
106
159
 
107
- return value
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
- k = ''
159
- if @namespace != nil
160
- k = @namespace + ':' + key.to_s
161
- elsif
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.2
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: 2017-07-28 00:00:00.000000000 Z
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