Class: Pgtk::Stash
- Inherits:
-
Object
- Object
- Pgtk::Stash
- Defined in:
- lib/pgtk/stash.rb
Overview
Database query cache implementation.
Provides a caching layer for PostgreSQL queries, automatically invalidating the cache when tables are modified. Read queries are cached while write queries bypass the cache and invalidate related cached entries.
Thread-safe with read-write locking.
The implementation is very naive! Use it at your own risk.
- Author
Yegor Bugayenko ([email protected])
- Copyright
Copyright (c) 2019-2026 Yegor Bugayenko
- License
MIT
Instance Method Summary collapse
-
#dump ⇒ String
Convert internal state into text.
-
#exec(query, params = [], result = 0) ⇒ PG::Result
Execute a SQL query with optional caching.
-
#initialize(pool, stash: { queries: {}, tables: {}, table_mod: {}, table_inflight: {} }, loog: Loog::NULL, entrance: Concurrent::ReentrantReadWriteLock.new, refill: 16, delay: 0, maxqueue: 128, threads: 4, cap: 10_000, capping: 60, retire: 15 * 60, retirement: 60) ⇒ Stash
constructor
Initialize a new Stash with query caching.
-
#session {|Pgtk::Stash| ... } ⇒ Object
Run statements on a single connection, without a transaction.
-
#start! ⇒ void
Start the connection pool and launch background cache management tasks.
-
#transaction {|Pgtk::Stash| ... } ⇒ Object
Execute a database transaction.
-
#version ⇒ String
Get the PostgreSQL server version.
Constructor Details
#initialize(pool, stash: { queries: {}, tables: {}, table_mod: {}, table_inflight: {} }, loog: Loog::NULL, entrance: Concurrent::ReentrantReadWriteLock.new, refill: 16, delay: 0, maxqueue: 128, threads: 4, cap: 10_000, capping: 60, retire: 15 * 60, retirement: 60) ⇒ Stash
Initialize a new Stash with query caching.
Set any of the intervals to nil to disable the cron.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/pgtk/stash.rb', line 70 def initialize( pool, stash: { queries: {}, tables: {}, table_mod: {}, table_inflight: {} }, loog: Loog::NULL, entrance: Concurrent::ReentrantReadWriteLock.new, refill: 16, delay: 0, maxqueue: 128, threads: 4, cap: 10_000, capping: 60, retire: 15 * 60, retirement: 60 ) @pool = pool @stash = stash @stash[:table_mod] ||= {} unless @stash[:table_inflight].default_proc @stash[:table_inflight] = Hash.new { |h, k| h[k] = Concurrent::AtomicFixnum.new(0) } end @loog = loog @entrance = entrance @refill = refill @delay = delay @maxqueue = maxqueue @threads = threads @cap = cap @capping = capping @retire = retire @retirement = retirement end |
Instance Method Details
#dump ⇒ String
Convert internal state into text.
120 121 122 123 124 |
# File 'lib/pgtk/stash.rb', line 120 def dump @entrance.with_read_lock do body(queries) end end |
#exec(query, params = [], result = 0) ⇒ PG::Result
Execute a SQL query with optional caching.
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/pgtk/stash.rb', line 132 def exec(query, params = [], result = 0) pure = (query.is_a?(Array) ? query.join(' ') : query).gsub(/\s+/, ' ').strip if MODS_RE.match?(pure) || (WITH_RE.match?(pure) && ALTS_RE.match?(pure)) modify(pure, params, result) elsif /(^|\s)pg_[a-z_]+\(/.match?(pure) @pool.exec(pure, params, result) else select(pure, params, result) end end |
#session {|Pgtk::Stash| ... } ⇒ Object
Run statements on a single connection, without a transaction.
157 158 159 160 161 |
# File 'lib/pgtk/stash.rb', line 157 def session @pool.session do |t| yield(Pgtk::Stash.new(t, stash: @stash, loog: @loog, entrance: @entrance)) end end |
#start! ⇒ void
This method returns an undefined value.
Start the connection pool and launch background cache management tasks.
105 106 107 108 109 |
# File 'lib/pgtk/stash.rb', line 105 def start! @pool.start! cascade! launch! end |
#transaction {|Pgtk::Stash| ... } ⇒ Object
Execute a database transaction.
147 148 149 150 151 |
# File 'lib/pgtk/stash.rb', line 147 def transaction @pool.transaction do |t| yield(Pgtk::Stash.new(t, stash: @stash, loog: @loog, entrance: @entrance)) end end |
#version ⇒ String
Get the PostgreSQL server version.
113 114 115 |
# File 'lib/pgtk/stash.rb', line 113 def version @pool.version end |