Class | Sequel::Postgres::StatementCache::StatementCache
In: | lib/sequel/extensions/pg_statement_cache.rb
Parent: | Object

The backbone of the block, a modified LRU (least recently used) cache mapping SQL query strings to Statement objects.

### Methods

clear each fetch new size

### Included Modules

Enumerable

### Public Class methods

new(opts={}, &block)

Set the options for the statement cache. These are generally set at the database level using the :statement_cache_opts Database option.

:max_size : | The maximum size (high water mark) for the cache. If an entry is added when the current size of the cache is equal to the maximum size, the cache is cleaned up to reduce the number of entries to the :min_size. Defaults to 1000.
:min_size : | The minimum size (low water mark) for the cache. On cleanup, the size of the cache is reduced to this number. Note that there could be fewer than this number of entries in the cache. Defaults to :max_size/2.
:prepare_after : | The number of executions to wait for before preparing

                 the query server-side.  If set to 1, prepares all executed
                 queries server-side.  If set to 5, does not attempt to
                 prepare the query until the 5th execution.  Defaults to 2.

:sorter :

A callable object that takes two arguments, the current time and the related Statement instance, and should return some Comparable (usually a numeric) such that the lowest values returned are the first to be removed when it comes time to clean the pool. The default is basically:

      lambda{|t, stmt| (stmt.last_seen - t)/stmt.num_executes}

so that it doesn‘t remove statements that have been executed many times just because many less-frequently executed statements have been executed recently.

The block passed is called with the Statement object‘s name, only for statements that have been prepared, and should be used to deallocate the statements.

[Source]

         # File lib/sequel/extensions/pg_statement_cache.rb, line 107
    107:         def initialize(opts={}, &block)
    108:           @cleanup_proc = block
    109:           @prepare_after = opts.fetch(:prepare_after, 2)
    110:           @max_size = opts.fetch(:max_size, 1000)
    111:           @min_size = opts.fetch(:min_size, @max_size/2)
    112:           @sorter = opts.fetch(:sorter){method(:default_sorter)}
    113:           @ids = (1..@max_size).to_a.reverse
    114:           @hash = {}
    115:           #
    116:           # We add one so that when we clean the cache, the entry
    117:           # about to be added brings us to the min_size.
    118:           @size_diff = @max_size - @min_size + 1
    119:         end

### Public Instance methods

clear()

Completely clear the statement cache, deallocating on the server side all statements that have been prepared.

[Source]

         # File lib/sequel/extensions/pg_statement_cache.rb, line 123
    123:         def clear
    124:           @hash.keys.each{|k| remove(k)}
    125:         end

each(&block)

Yield each SQL string and Statement instance in the cache to the block.

[Source]

         # File lib/sequel/extensions/pg_statement_cache.rb, line 129
    129:         def each(&block)
    130:           @hash.each(&block)
    131:         end

fetch(sql) {|stmt.name| ...}

Get the related statement name from the cache. If the entry is already in the cache, just bump it‘s last seen time and the number of executions. Otherwise, add it to the cache. If the cache is already full, clean it up before adding it.

If the num of executions has passed the threshhold, yield the statement name to the block, which should be used to prepare the statement on the server side.

This method should return the prepared statment name if the statement has been prepared, and nil if the query has not been prepared and the statement should be executed normally.

[Source]

         # File lib/sequel/extensions/pg_statement_cache.rb, line 147
    147:         def fetch(sql)
    148:           unless stmt = @hash[sql]
    149:             # Get the next id from the id pool.
    150:             unless id = @ids.pop
    151:               # No id left, cache must be full, so cleanup and then
    152:               # get the next id from the id pool.
    153:               cleanup
    154:               id = @ids.pop
    155:             end
    156:             @hash[sql] = stmt = Statement.new(id)
    157:           end
    158:
    159:           stmt.last_seen = Time.now
    160:           stmt.num_executes += 1
    161:
    162:           if stmt.num_executes >= @prepare_after
    163:             if stmt.num_executes == @prepare_after
    164:               begin
    165:                 yield(stmt.name)
    166:               rescue PGError
    167:                 # An error occurred while preparing the statement,
    168:                 # execute it normally (which will probably raise
    169:                 # the error again elsewhere), but decrement the
    170:                 # number of executions so we don't think we've
    171:                 # prepared the statement when we haven't.
    172:                 stmt.num_executes -= 1
    173:                 return nil
    174:               end
    175:             end
    176:             stmt.name
    177:           end
    178:         end

size()

The current size of the statement cache.

[Source]

         # File lib/sequel/extensions/pg_statement_cache.rb, line 181
    181:         def size
    182:           @hash.length
    183:         end

[Validate]