Skip to content

Instantly share code, notes, and snippets.

@ryanermita
ryanermita / storing_nested_objects_in_redis.py
Created May 17, 2018 14:36
Storing nested objects in redis sample code which you can use to play around in python and redis.
import redis # install redis via pip > `python -m pip install redis`
import json
# Instantiate redis connection with your local redis instance.
redis_connection = redis.StrictRedis(host="localhost", port=int(6379),
decode_responses=True)
"""
Caching simple object (flat and one dimentional object) as redis hash.
"""
@ryanermita
ryanermita / doublequotestrings.md
Last active September 9, 2016 04:17
Alternative way in ruby to enclose strings in double quotes.

%Q

  • Alternative way in ruby to enclose strings in double quotes.

  • Useful when you have more quote characters in a string.You can use this instead of putting backslashes in front of them.

  • Can use in escaping HTML and multiline SQL statements.

Usage

@ryanermita
ryanermita / innodb_lock_wait_timeout.md
Created August 27, 2016 02:11
Updating innodb_lock_wait_timeout

Very useful when you need to update database transaction locking timeout dyanamically.

SET SESSION innodb_lock_wait_timeout = 1; will set the innodb_lock_wait_timeout

SELECT @@innodb_lock_wait_timeout; will output the updated innodb_lock_wait_timeout

@ryanermita
ryanermita / rails_locking.md
Last active February 17, 2024 02:19
Optimistic and Pessimistic Locking in Rails

Optimistic Locking assumes that a database transaction conflict is very rare to happen. It uses a version number of the record to track the changes. It raise an error when other user tries to update the record while it is lock.

usage

Just add a lock_version column to the table you want to place the lock and Rails will automatically check this column before updating the record.

Pessimistic locking assumes that database transaction conflict is very likely to happen. It locks the record until the transaction is done. If the record is currently lock and the other user make a transaction, that second transaction will wait until the lock in first transaction is release.

usage

@ryanermita
ryanermita / raw_sql.rb
Created August 27, 2016 00:05
Running raw SQL in Ruby on Rails
# Running raw SQL in Ruby on Rails
sql = "SELECT * FROM table_name"
result = ActiveRecord::Base.connection.execute(sql)