cache_store_redis 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60da65abdb34c0ff1c3f13be3c3adc90eb9749fc
|
4
|
+
data.tar.gz: 0777f322a72712b760cddd07ebe98406defcc739
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7a2d59f6b7b32cdab7f2872386bdd30fe02b237895dcf4f6646d08348df7de3e5040441f12f701bdb94eefec19a003f33dfa3f5e7b7de89ae7bb1a402858b5e
|
7
|
+
data.tar.gz: b7ccfe96ee92fcc13c582c00956e0551906f74203311caec01326905483fc85a81b05d45dbdba1a56bd66643636e1f79dabd7be5543b7cbc6e63d5464a7393ee
|
data/lib/cache_store_redis.rb
CHANGED
@@ -5,5 +5,7 @@ require 'cache_store_redis/version'
|
|
5
5
|
require 'redis'
|
6
6
|
require 'securerandom'
|
7
7
|
|
8
|
+
require_relative 'cache_store_redis/redis_connection'
|
9
|
+
require_relative 'cache_store_redis/connection_pool'
|
8
10
|
require_relative 'cache_store_redis/redis_cache_store'
|
9
11
|
require_relative 'cache_store_redis/optional_redis_cache_store'
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# This class is used to define a pool of re-usable redis connections.
|
2
|
+
class ConnectionPool
|
3
|
+
attr_accessor :config
|
4
|
+
|
5
|
+
def initialize(namespace = nil, config = nil)
|
6
|
+
@namespace = namespace
|
7
|
+
@config = config
|
8
|
+
@queue = Queue.new
|
9
|
+
@connections = []
|
10
|
+
@mutex = Mutex.new
|
11
|
+
@monitor_thread = Thread.new do
|
12
|
+
loop do
|
13
|
+
sleep(1)
|
14
|
+
@mutex.synchronize do
|
15
|
+
connections.select { |con| con.expired? }.each do |con|
|
16
|
+
con.close
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# This method is called to get the namespace for redis keys.
|
24
|
+
def namespace
|
25
|
+
@namespace
|
26
|
+
end
|
27
|
+
|
28
|
+
# This method is called to get the idle connection queue for this pool.
|
29
|
+
def queue
|
30
|
+
@queue
|
31
|
+
end
|
32
|
+
|
33
|
+
# This method is called to fetch a connection from the queue or create a new connection if no idle connections
|
34
|
+
# are available.
|
35
|
+
def fetch_connection
|
36
|
+
queue.pop(true)
|
37
|
+
rescue
|
38
|
+
RedisConnection.new(config)
|
39
|
+
end
|
40
|
+
|
41
|
+
# This method is called to checkout a connection from the pool before use.
|
42
|
+
def check_out
|
43
|
+
connection = nil
|
44
|
+
@mutex.synchronize do
|
45
|
+
connection = fetch_connection
|
46
|
+
connections.delete(connection)
|
47
|
+
connection.open
|
48
|
+
end
|
49
|
+
connection
|
50
|
+
end
|
51
|
+
|
52
|
+
# This method is called to checkin a connection to the pool after use.
|
53
|
+
def check_in(connection)
|
54
|
+
if connection.expired?
|
55
|
+
connection.close
|
56
|
+
end
|
57
|
+
@mutex.synchronize do
|
58
|
+
connections.push(connection)
|
59
|
+
queue.push(connection)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# This method is called to use a connection from this pool.
|
64
|
+
def with_connection
|
65
|
+
connection = check_out
|
66
|
+
return yield connection.client
|
67
|
+
ensure
|
68
|
+
check_in(connection)
|
69
|
+
end
|
70
|
+
|
71
|
+
# This method is called to get an array of idle connections in this pool.
|
72
|
+
def connections
|
73
|
+
@connections
|
74
|
+
end
|
75
|
+
|
76
|
+
def shutdown
|
77
|
+
connections.each do |con|
|
78
|
+
con.client.close
|
79
|
+
end
|
80
|
+
@connections = []
|
81
|
+
@monitor_thread.kill
|
82
|
+
@queue = Queue.new
|
83
|
+
end
|
84
|
+
end
|
@@ -1,13 +1,14 @@
|
|
1
1
|
# This class is used to implement a redis cache store.
|
2
2
|
class RedisCacheStore
|
3
3
|
def initialize(namespace = nil, config = nil)
|
4
|
+
@connection_pool = ConnectionPool.new(config)
|
5
|
+
|
4
6
|
unless RUBY_PLATFORM == 'java'
|
5
7
|
require 'oj'
|
6
8
|
end
|
7
9
|
|
8
10
|
@namespace = namespace
|
9
11
|
@config = config
|
10
|
-
@queue = Queue.new
|
11
12
|
|
12
13
|
@connections_created = 0
|
13
14
|
@connections_in_use = 0
|
@@ -15,26 +16,8 @@ class RedisCacheStore
|
|
15
16
|
@enable_stats = false
|
16
17
|
end
|
17
18
|
|
18
|
-
def
|
19
|
-
@
|
20
|
-
end
|
21
|
-
|
22
|
-
def increment_created_stat
|
23
|
-
@mutex.synchronize do
|
24
|
-
@connections_created += 1
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def increment_using_stat
|
29
|
-
@mutex.synchronize do
|
30
|
-
@connections_in_use += 1
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def decrement_using_stat
|
35
|
-
@mutex.synchronize do
|
36
|
-
@connections_in_use -= 1
|
37
|
-
end
|
19
|
+
def connection_pool
|
20
|
+
@connection_pool
|
38
21
|
end
|
39
22
|
|
40
23
|
# This method is called to configure the connection to the cache store.
|
@@ -61,43 +44,17 @@ class RedisCacheStore
|
|
61
44
|
@config[:connect_timeout] = connect_timeout
|
62
45
|
@config[:read_timeout] = read_timeout
|
63
46
|
@config[:write_timeout] = write_timeout
|
64
|
-
|
65
|
-
|
66
|
-
def fetch_client
|
67
|
-
begin
|
68
|
-
@queue.pop(true)
|
69
|
-
rescue
|
70
|
-
increment_created_stat
|
71
|
-
Redis.new(@config)
|
72
|
-
end
|
47
|
+
connection_pool.config = @config
|
73
48
|
end
|
74
49
|
|
75
50
|
def clean
|
76
|
-
|
77
|
-
client = @queue.pop(true)
|
78
|
-
client.close
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def log_stats
|
83
|
-
return unless @enable_stats == true
|
84
|
-
S1Logging.logger.debug do
|
85
|
-
"[#{self.class}] - REDIS Connection Stats. Process: #{Process.pid} | " \
|
86
|
-
"Created: #{@connections_created} | Pending: #{@queue.length} | In use: #{@connections_in_use}"
|
87
|
-
end
|
51
|
+
connection_pool.shutdown
|
88
52
|
end
|
53
|
+
alias_method :shutdown, :clean
|
89
54
|
|
90
55
|
def with_client
|
91
|
-
|
92
|
-
|
93
|
-
client = fetch_client
|
94
|
-
increment_using_stat
|
95
|
-
log_stats
|
96
|
-
yield client
|
97
|
-
ensure
|
98
|
-
@queue.push(client)
|
99
|
-
decrement_using_stat
|
100
|
-
log_stats
|
56
|
+
connection_pool.with_connection do |connection|
|
57
|
+
yield connection
|
101
58
|
end
|
102
59
|
end
|
103
60
|
|
@@ -106,7 +63,7 @@ class RedisCacheStore
|
|
106
63
|
# @param key [String] This is the unique key to reference the value being set within this cache store.
|
107
64
|
# @param value [Object] This is the value to set within this cache store.
|
108
65
|
# @param expires_in [Integer] This is the number of seconds from the current time that this value should expire.
|
109
|
-
def set(key, value, expires_in =
|
66
|
+
def set(key, value, expires_in = 3_600)
|
110
67
|
k = build_key(key)
|
111
68
|
|
112
69
|
v = if value.nil? || (value.is_a?(String) && value.strip.empty?)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class RedisConnection
|
2
|
+
attr_accessor :created
|
3
|
+
attr_accessor :client
|
4
|
+
|
5
|
+
def initialize(config)
|
6
|
+
self.client = Redis.new(config)
|
7
|
+
self.created = Time.now
|
8
|
+
end
|
9
|
+
|
10
|
+
# This method is called to determine if this connection has been open for longer than the keep alive timeout or not.
|
11
|
+
def expired?
|
12
|
+
return false if self.created.nil?
|
13
|
+
Time.now >= (self.created + keep_alive_timeout)
|
14
|
+
end
|
15
|
+
|
16
|
+
def open
|
17
|
+
self.created = Time.now if self.created.nil?
|
18
|
+
end
|
19
|
+
|
20
|
+
def close
|
21
|
+
self.client.close
|
22
|
+
self.created = nil
|
23
|
+
end
|
24
|
+
|
25
|
+
# This method is called to get the keep alive timeout value to use for this connection.
|
26
|
+
def keep_alive_timeout
|
27
|
+
Float(ENV['REDIS_KEEP_ALIVE_TIMEOUT'] || 30)
|
28
|
+
end
|
29
|
+
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.7.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-
|
11
|
+
date: 2018-03-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -90,8 +90,10 @@ files:
|
|
90
90
|
- bin/console
|
91
91
|
- bin/setup
|
92
92
|
- lib/cache_store_redis.rb
|
93
|
+
- lib/cache_store_redis/connection_pool.rb
|
93
94
|
- lib/cache_store_redis/optional_redis_cache_store.rb
|
94
95
|
- lib/cache_store_redis/redis_cache_store.rb
|
96
|
+
- lib/cache_store_redis/redis_connection.rb
|
95
97
|
- lib/cache_store_redis/version.rb
|
96
98
|
homepage: https://github.com/vaughanbrittonsage/cache_store
|
97
99
|
licenses:
|
@@ -113,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
115
|
version: '0'
|
114
116
|
requirements: []
|
115
117
|
rubyforge_project:
|
116
|
-
rubygems_version: 2.5.
|
118
|
+
rubygems_version: 2.5.1
|
117
119
|
signing_key:
|
118
120
|
specification_version: 4
|
119
121
|
summary: This is the redis cache_store implementation.
|