pgtk 0.25.0 → 0.26.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 +4 -4
- data/README.md +3 -0
- data/lib/pgtk/stash.rb +59 -40
- data/lib/pgtk/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5b7cf162ce55193a057e281c59e4ade55ba6dccee8e2076e7ebf6cc5ebd8b3d6
|
|
4
|
+
data.tar.gz: f84661c917982464a21be179bccd34e7ae0b8d3f234562dcf026e3f5aebe4793
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 855c4d94c5ff429abe22d4593076f459df95f9cf3612157c149e929243f79e2d49bf9d56183d2c0da074e8abb30e73bff12722dbd1e57e04c3385abd8ca9314c
|
|
7
|
+
data.tar.gz: 7d9f3cfc61dfda380a6bdb63f3451ba8f09cf798796a8786fe6a0ce251e25174faf1cf21d1e4619cfed6bea95041a1072f9c62c9a60e3c825a229795d115cd6b
|
data/README.md
CHANGED
|
@@ -228,6 +228,9 @@ stash = Pgtk::Stash.new(
|
|
|
228
228
|
cap: 10_000, # Maximum cached query results (default: 10,000)
|
|
229
229
|
cap_interval: 60, # Seconds between cache size enforcement (default: 60)
|
|
230
230
|
refill_interval: 16, # Seconds between stale query refilling (default: 16)
|
|
231
|
+
refill_delay: 0.5, # Seconds to wait before refilling the cache (default: 0)
|
|
232
|
+
retire: 60, # Maximum age in seconds to keep a query in cache (default: 15 min)
|
|
233
|
+
retire_interval: 5.5, # How often to retire (default: 60)
|
|
231
234
|
threads: 4, # Worker threads for background refilling (default: 4)
|
|
232
235
|
max_queue_length: 128 # Maximum refilling tasks in queue (default: 128)
|
|
233
236
|
)
|
data/lib/pgtk/stash.rb
CHANGED
|
@@ -41,6 +41,8 @@ class Pgtk::Stash
|
|
|
41
41
|
|
|
42
42
|
# Initialize a new Stash with query caching.
|
|
43
43
|
#
|
|
44
|
+
# Set any of the intervals to nil to disable the cron.
|
|
45
|
+
#
|
|
44
46
|
# @param [Object] pool The underlying connection pool that executes actual database queries
|
|
45
47
|
# @param [Hash] stash Internal cache structure containing queries and tables hashes for sharing state
|
|
46
48
|
# across transactions
|
|
@@ -61,11 +63,11 @@ class Pgtk::Stash
|
|
|
61
63
|
# @param [Loog] loog Logger instance for debugging and monitoring cache operations (default: null logger)
|
|
62
64
|
# @param [Concurrent::ReentrantReadWriteLock] entrance Read-write lock for thread-safe cache access
|
|
63
65
|
# shared across instances
|
|
64
|
-
# @param [Concurrent::AtomicBoolean] launched Atomic boolean flag tracking whether background tasks have
|
|
65
|
-
# been started to prevent multiple launches
|
|
66
66
|
def initialize(
|
|
67
67
|
pool,
|
|
68
68
|
stash: { queries: {}, tables: {} },
|
|
69
|
+
loog: Loog::NULL,
|
|
70
|
+
entrance: Concurrent::ReentrantReadWriteLock.new,
|
|
69
71
|
refill_interval: 16,
|
|
70
72
|
refill_delay: 0,
|
|
71
73
|
max_queue_length: 128,
|
|
@@ -73,14 +75,11 @@ class Pgtk::Stash
|
|
|
73
75
|
cap: 10_000,
|
|
74
76
|
cap_interval: 60,
|
|
75
77
|
retire: 15 * 60,
|
|
76
|
-
retire_interval: 60
|
|
77
|
-
loog: Loog::NULL,
|
|
78
|
-
entrance: Concurrent::ReentrantReadWriteLock.new,
|
|
79
|
-
launched: Concurrent::AtomicBoolean.new(false)
|
|
78
|
+
retire_interval: 60
|
|
80
79
|
)
|
|
81
80
|
@pool = pool
|
|
82
81
|
@stash = stash
|
|
83
|
-
@
|
|
82
|
+
@loog = loog
|
|
84
83
|
@entrance = entrance
|
|
85
84
|
@refill_interval = refill_interval
|
|
86
85
|
@refill_delay = refill_delay
|
|
@@ -90,8 +89,6 @@ class Pgtk::Stash
|
|
|
90
89
|
@cap_interval = cap_interval
|
|
91
90
|
@retire = retire
|
|
92
91
|
@retire_interval = retire_interval
|
|
93
|
-
@loog = loog
|
|
94
|
-
@tpool = Concurrent::FixedThreadPool.new(@threads)
|
|
95
92
|
end
|
|
96
93
|
|
|
97
94
|
# Start the connection pool and launch background cache management tasks.
|
|
@@ -102,8 +99,8 @@ class Pgtk::Stash
|
|
|
102
99
|
#
|
|
103
100
|
# @return [void]
|
|
104
101
|
def start!
|
|
105
|
-
launch!
|
|
106
102
|
@pool.start!
|
|
103
|
+
launch!
|
|
107
104
|
end
|
|
108
105
|
|
|
109
106
|
# Get the PostgreSQL server version.
|
|
@@ -135,19 +132,41 @@ class Pgtk::Stash
|
|
|
135
132
|
[
|
|
136
133
|
'Pgtk::Stash (',
|
|
137
134
|
[
|
|
138
|
-
"refill_interval=#{@refill_interval}s",
|
|
139
|
-
"max_queue_length=#{@max_queue_length}",
|
|
140
135
|
"threads=#{@threads}",
|
|
141
|
-
"
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
136
|
+
"max_queue_length=#{@max_queue_length}",
|
|
137
|
+
if @refill_interval
|
|
138
|
+
[
|
|
139
|
+
"refill_interval=#{@refill_interval}s",
|
|
140
|
+
"refill_delay=#{@refill_delay}s"
|
|
141
|
+
]
|
|
142
|
+
else
|
|
143
|
+
'no refilling'
|
|
144
|
+
end,
|
|
145
|
+
if @cap_interval
|
|
146
|
+
[
|
|
147
|
+
"cap_interval=#{@cap_interval}s",
|
|
148
|
+
"cap=#{@cap}"
|
|
149
|
+
]
|
|
150
|
+
else
|
|
151
|
+
'no capping'
|
|
152
|
+
end,
|
|
153
|
+
if @retire_interval
|
|
154
|
+
[
|
|
155
|
+
"retire_interval=#{@retire_interval}s",
|
|
156
|
+
"retire=#{@retire}"
|
|
157
|
+
]
|
|
158
|
+
else
|
|
159
|
+
'no retirement'
|
|
160
|
+
end
|
|
161
|
+
].flatten.join(', '),
|
|
146
162
|
'):'
|
|
147
163
|
].join,
|
|
148
|
-
|
|
164
|
+
if @tpool
|
|
165
|
+
" #{@tpool.queue_length} tasks in the thread pool"
|
|
166
|
+
else
|
|
167
|
+
' Not launched yet'
|
|
168
|
+
end,
|
|
149
169
|
" #{stash_size} queries cached (#{stash_size > @cap ? 'above' : 'below'} the cap)",
|
|
150
|
-
" #{@tpool.queue_length} tasks in the thread pool",
|
|
151
170
|
" #{@stash[:tables].count} tables in cache",
|
|
152
171
|
" #{qq.sum { |a| a[:s] }} stale queries in cache:",
|
|
153
172
|
qq.select { |a| a[:s].positive? }.sort_by { -_1[:p] }.take(8).map do |a|
|
|
@@ -225,12 +244,8 @@ class Pgtk::Stash
|
|
|
225
244
|
yield Pgtk::Stash.new(
|
|
226
245
|
t,
|
|
227
246
|
stash: @stash,
|
|
228
|
-
refill_interval: @refill_interval,
|
|
229
|
-
max_queue_length: @max_queue_length,
|
|
230
|
-
threads: @threads,
|
|
231
247
|
loog: @loog,
|
|
232
|
-
entrance: @entrance
|
|
233
|
-
launched: @launched
|
|
248
|
+
entrance: @entrance
|
|
234
249
|
)
|
|
235
250
|
end
|
|
236
251
|
end
|
|
@@ -255,28 +270,33 @@ class Pgtk::Stash
|
|
|
255
270
|
# @return [nil]
|
|
256
271
|
# @raise [RuntimeError] if background tasks have already been launched on this cache instance
|
|
257
272
|
def launch!
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
@
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
273
|
+
@tpool = Concurrent::FixedThreadPool.new(@threads)
|
|
274
|
+
if @cap_interval
|
|
275
|
+
Concurrent::TimerTask.execute(execution_interval: @cap_interval, executor: @tpool) do
|
|
276
|
+
loop do
|
|
277
|
+
break if stash_size <= @cap
|
|
278
|
+
@entrance.with_write_lock do
|
|
279
|
+
@stash[:queries].each_key do |q|
|
|
280
|
+
m = @stash[:queries][q].values.map { |h| h[:used] }.min
|
|
281
|
+
next unless m
|
|
282
|
+
@stash[:queries][q].delete_if { |_, h| h[:used] == m }
|
|
283
|
+
@stash[:queries].delete_if { |_, kk| kk.empty? }
|
|
284
|
+
end
|
|
268
285
|
end
|
|
269
286
|
end
|
|
270
287
|
end
|
|
271
288
|
end
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
@
|
|
275
|
-
@stash[:queries]
|
|
276
|
-
|
|
289
|
+
if @retire_interval
|
|
290
|
+
Concurrent::TimerTask.execute(execution_interval: @retire_interval, executor: @tpool) do
|
|
291
|
+
@entrance.with_write_lock do
|
|
292
|
+
@stash[:queries].each_key do |q|
|
|
293
|
+
@stash[:queries][q].delete_if { |_, h| h[:used] < Time.now - @retire }
|
|
294
|
+
@stash[:queries].delete_if { |_, kk| kk.empty? }
|
|
295
|
+
end
|
|
277
296
|
end
|
|
278
297
|
end
|
|
279
298
|
end
|
|
299
|
+
return unless @refill_interval
|
|
280
300
|
Concurrent::TimerTask.execute(execution_interval: @refill_interval, executor: @tpool) do
|
|
281
301
|
@stash[:queries]
|
|
282
302
|
.map { |k, v| [k, v.values.sum { |vv| vv[:popularity] }, v.values.any? { |vv| vv[:stale] }] }
|
|
@@ -300,6 +320,5 @@ class Pgtk::Stash
|
|
|
300
320
|
end
|
|
301
321
|
end
|
|
302
322
|
end
|
|
303
|
-
nil
|
|
304
323
|
end
|
|
305
324
|
end
|
data/lib/pgtk/version.rb
CHANGED