Skip to content

Instantly share code, notes, and snippets.

@cfc1020
cfc1020 / sessions_and_conversations.markdown
Created September 30, 2018 15:25 — forked from jcasimir/sessions_and_conversations.markdown
Sessions and Conversations in Rails 3

Sessions and Conversations

HTTP is a stateless protocol. Sessions allow us to chain multiple requests together into a conversation between client and server.

Sessions should be an option of last resort. If there's no where else that the data can possibly go to achieve the desired functionality, only then should it be stored in the session. Sessions can be vulnerable to security threats from third parties, malicious users, and can cause scaling problems.

That doesn't mean we can't use sessions, but we should only use them where necessary.

Adding, Accessing, and Removing Data

# Manually secure port 6379
sudo iptables -A INPUT -p tcp --dport 6379 -s xxx.xxx.xxx.xxx -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 6379 -j DROP
sudo iptables -L
# Save current firewall config
sudo iptables-save > /etc/iptables.conf
# Load iptables.conf on startup
sudo nano /etc/rc.local
@cfc1020
cfc1020 / benchmark.rb
Last active June 22, 2020 09:48
Finding the k-th largest element from the array effectively and simply as well
require 'benchmark'
examples = []
res1 = []
res2 = []
def f1(arr, k)
arr.sort[arr.size - k]
end
@cfc1020
cfc1020 / max_set.go
Last active May 13, 2020 09:31
MaxSubNonNegativesArray
// Given an array of integers, A of length N, find out the maximum sum sub-array of non negative numbers from A.
/**
* @input A : Integer array
*
* @Output Integer array.
*/
package main
import "fmt"
@cfc1020
cfc1020 / compare_versions.rb
Last active June 3, 2020 18:51
Compare Versions
class MySolution
# @param a : string
# @param b : string
# @returns an integer
def compare_versions(a, b)
aa = a.split('.')
bb = b.split('.')
i = 0
@cfc1020
cfc1020 / convert_to_title.rb
Created June 5, 2020 17:18
excel-sheet-column-title
# @param {Integer} n
# @return {String}
def convert_to_title(n)
res = ''
begin
rem = n % 26
n /= 26
rem = if rem.zero?
@cfc1020
cfc1020 / binary_gap.rb
Created June 5, 2020 17:25
Return the longest run of 1s for a given integer n's binary representation.
# @param {Integer} n
# @return {Integer}
def binary_gap(n)
res = 0
while n.nonzero? do
n &= n << 1
res += 1
end
@cfc1020
cfc1020 / defang_i_paddr.rb
Last active June 6, 2020 17:41
Defanging an IP Address
# @param {String} address
# @return {String}
def defang_i_paddr(address)
address.split('.').join('[.]')
end
@cfc1020
cfc1020 / level_order.rb
Last active June 8, 2020 16:39
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level)..
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val = 0, left = nil, right = nil)
# @val = val
# @left = left
# @right = right
# end
# end
# @param {TreeNode} root
@cfc1020
cfc1020 / min_add_to_make_valid.rb
Created June 12, 2020 18:22
Minimum Add to Make Parentheses Valid
# @param {String} s
# @return {Integer}
def min_add_to_make_valid(s)
res = 0
balance = 0
s.each_char do |c|
balance += c == '(' ? 1 : -1
if balance < 0