Class: Rack::State::Store::PreparedPostgres
- Inherits:
-
Object
- Object
- Rack::State::Store::PreparedPostgres
- Defined in:
- lib/rack/state.rb
Overview
Postgres table-based state storage adapter using prepared statements. Because accessing state is a highly repetitive activity, prepared statements may offer some optimization.
See Postgres for details.
Constant Summary collapse
- BINARY =
1
Instance Method Summary collapse
- #create(token, object) ⇒ Object
- #delete(token) ⇒ Object
-
#initialize(connection, table = 'state') ⇒ PreparedPostgres
constructor
A new instance of PreparedPostgres.
- #read(token) ⇒ Object
- #update(token, object) ⇒ Object
Constructor Details
#initialize(connection, table = 'state') ⇒ PreparedPostgres
Returns a new instance of PreparedPostgres.
353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/rack/state.rb', line 353 def initialize(connection, table = 'state') @db = connection begin @db.prepare 'create_state', "INSERT INTO #{table} (token,object) VALUES ($1,$2)" @db.prepare 'read_state', "SELECT object FROM #{table} WHERE token=$1" @db.prepare 'update_state', "UPDATE #{table} SET object=$2, mtime=NOW() WHERE token=$1" @db.prepare 'delete_state', "DELETE FROM #{table} WHERE token=$1" rescue PG::DuplicatePstatement end end |
Instance Method Details
#create(token, object) ⇒ Object
368 369 370 371 372 |
# File 'lib/rack/state.rb', line 368 def create(token, object) @db.exec_prepared('create_state', params(token, object)).clear rescue PG::UniqueViolation raise KeyError end |
#delete(token) ⇒ Object
386 387 388 |
# File 'lib/rack/state.rb', line 386 def delete(token) @db.exec_prepared('delete_state', params(token)).clear end |
#read(token) ⇒ Object
374 375 376 377 378 |
# File 'lib/rack/state.rb', line 374 def read(token) @db.exec_prepared('read_state', params(token), BINARY) do |result| ::Marshal.load result.getvalue(0,0) end rescue nil end |
#update(token, object) ⇒ Object
380 381 382 383 384 |
# File 'lib/rack/state.rb', line 380 def update(token, object) @db.exec_prepared('update_state', params(token, object)) do |result| raise KeyError if result.cmd_tuples == 0 end end |