class RateLimit::MySQL
Public Class Methods
new(dbh, table, fields=["name","current","max","min","cost","refill_amount","refill_epoch","last_refill","total_used"])
click to toggle source
# File lib/ratelimit/bucketbased.rb, line 116 def initialize(dbh, table, fields=["name","current","max","min","cost","refill_amount","refill_epoch","last_refill","total_used"]) @queries = { 'get' => dbh.prepare("SELECT `#{fields.join('`, `')}` FROM `#{table}` WHERE `#{fields[0]}` = ? LIMIT 1"), 'update' => dbh.prepare("UPDATE `#{table}` SET `#{fields[1]}` = ?, `#{fields[7]}` = ?, `#{fields[8]}` = ? WHERE `#{fields[0]}` = ?"), 'set' => dbh.prepare("REPLACE INTO `#{table}` (`#{fields.join('`, `')}`) VALUES (?,?,?,?,?,?,?,?,?)") } end
Public Instance Methods
get(name)
click to toggle source
retrieves a named bucket
-
Args :
-
name
-> the name of the bucket to be retrieved
-
-
Returns :
-
the bucket matching the name if found, nil otherwise
-
-
Raises :
-
Mysql::Error
-> any issue with the connection to the database or the SQL statements
-
# File lib/ratelimit/bucketbased.rb, line 131 def get(name) rs = @queries['get'].execute(name) bucket = nil rs.each do |row| bucket = Bucket.new(row[0],*row[1,8].map{|x| x.to_f}) end bucket end
set(bucket)
click to toggle source
saves a bucket into the storage
-
Args :
-
Returns :
-
an empty result set
-
-
Raises :
-
Mysql::Error
-> any issue with the connection to the database or the SQL statements
-
# File lib/ratelimit/bucketbased.rb, line 147 def set(bucket) @queries['set'].execute(bucket.name, bucket.current, bucket.max, bucket.min, bucket.cost, bucket.refill_amount, bucket.refill_epoch, bucket.last_refill, bucket.total_used) end
update(bucket)
click to toggle source
updates the key fields that need updating into the storage this is often cheaper for certain types of storage than using set()
-
Args :
-
Returns :
-
an empty result set
-
-
Raises :
-
Mysql::Error
-> any issue with the connection to the database or the SQL statements
-
# File lib/ratelimit/bucketbased.rb, line 159 def update(bucket) @queries['update'].execute(bucket.current, bucket.last_refill, bucket.total_used, bucket.name) end