Class: RateLimit::MySQL
Instance Method Summary collapse
-
#get(name) ⇒ Object
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. -
#initialize(dbh, table, fields = ["name","current","max","min","cost","refill_amount","refill_epoch","last_refill","total_used"]) ⇒ MySQL
constructor
A new instance of MySQL.
-
#set(bucket) ⇒ Object
saves a bucket into the storage * Args : -
bucket
-> the Bucket to set. -
#update(bucket) ⇒ Object
updates the key fields that need updating into the storage this is often cheaper for certain types of storage than using set() * Args : -
bucket
-> the Bucket to update.
Constructor Details
#initialize(dbh, table, fields = ["name","current","max","min","cost","refill_amount","refill_epoch","last_refill","total_used"]) ⇒ MySQL
Returns a new instance of MySQL.
116 117 118 119 120 121 122 |
# 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 |
Instance Method Details
#get(name) ⇒ Object
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
-
131 132 133 134 135 136 137 138 |
# 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) ⇒ Object
saves a bucket into the storage
-
Args :
-
bucket
-> the Bucket to set. Thename
field in the Bucket option will be used as a key.
-
-
Returns :
-
an empty result set
-
-
Raises :
-
Mysql::Error
-> any issue with the connection to the database or the SQL statements
-
147 148 149 |
# 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) ⇒ Object
updates the key fields that need updating into the storage this is often cheaper for certain types of storage than using set()
-
Args :
-
bucket
-> the Bucket to update. Thename
field in the Bucket option will be used as a key.
-
-
Returns :
-
an empty result set
-
-
Raises :
-
Mysql::Error
-> any issue with the connection to the database or the SQL statements
-
159 160 161 |
# File 'lib/ratelimit/bucketbased.rb', line 159 def update(bucket) @queries['update'].execute(bucket.current, bucket.last_refill, bucket.total_used, bucket.name) end |