class BBS::RateLimit::Table
The events of a limit in a BBS::SQLite table, by wall-clock time, so they outlast a restart.
Constants
- NAME
-
The table every stored limit keeps its events in.
Public Class Methods
Source
# File lib/bbs/rate_limit.rb, line 141 def initialize(db, name) @db = db @name = name @db.query("CREATE TABLE IF NOT EXISTS #{NAME} (name TEXT NOT NULL, key TEXT NOT NULL, at REAL NOT NULL)") @db.query("CREATE INDEX IF NOT EXISTS #{NAME}_by_key ON #{NAME} (name, key, at)") end
Uses (and creates) the table in db for the limit called name.
Public Instance Methods
Source
# File lib/bbs/rate_limit.rb, line 159 def add(key, time) = @db.insert("INSERT INTO #{NAME} (name, key, at) VALUES (?, ?, ?)", @name, key.to_s, time)
Keeps an event of key at time.
Source
# File lib/bbs/rate_limit.rb, line 149 def now = Time.now.to_f
The current time of this store: Unix seconds.
Source
# File lib/bbs/rate_limit.rb, line 153 def since(key, start) @db.change("DELETE FROM #{NAME} WHERE name = ? AND at <= ?", @name, start) @db.query("SELECT at FROM #{NAME} WHERE name = ? AND key = ? ORDER BY at", @name, key.to_s).map { |row| row['at'] } end
The times of key‘s events after start, oldest first; the older events of the limit are deleted.