Skip to content

Instantly share code, notes, and snippets.

View xifengzhu's full-sized avatar

leif Yi xifengzhu

View GitHub Profile
@xifengzhu
xifengzhu / Rails 实现读写分离(MySQL).md
Last active April 10, 2022 07:41
Rails 实现读写分离(MySQL)

常用的实现读写分离的方式有如下两种

  • 第三方中间件,比较成熟的有Mysql官方提供的 MySQL Router以及 360公司基于MySQL-Proxy 0.8.2开发的开源 Atlas

  • 其次就是在应用中做解决,在 ActiveRecord中做做处理, 如:octopus

本次实践的是用 gem octopus来实现读写分离。

为了模拟读写分离,将 MySQL 分别安装在了本地 Mac 和阿里云 Linux 系统上,MacMySQL 作为写入数据库,而Linux的作为读取数据库。

require 'jwt'
class JsonWebToken
# Encodes and signs JWT Payload with expiration
def self.encode(payload)
payload.reverse_merge!(meta)
JWT.encode(payload, Rails.application.secrets.secret_key_base)
end
# Decodes the JWT with the signed secret
@xifengzhu
xifengzhu / decrypt_wechat_refund_callback_data.rb
Last active September 14, 2017 08:53
微信退款成功回调通知数据解密
def refund_notify
encrypt_result = Hash.from_xml(request.body.read)["xml"]
decrypt_result = Hash.from_xml(decrypt_data(encrypt_result['req_info']))['root']
# other logic code (decrypt_result['out_refund_no'])
render :xml => {return_code: "SUCCESS"}.to_xml(root: 'xml', dasherize: false)
end
private
@xifengzhu
xifengzhu / Unicorn-auto-start-after-server-boot.sh
Last active August 26, 2017 08:53
Setting up Unicorn to run via init.d
#!/bin/sh
# File: /etc/init.d/${project_name}_unicorn
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
@xifengzhu
xifengzhu / ssl_puma.sh
Created November 23, 2016 09:04 — forked from tadast/ssl_puma.sh
localhost SSL with puma
# 1) Create your private key (any password will do, we remove it below)
$ cd ~/.ssh
$ openssl genrsa -des3 -out server.orig.key 2048
# 2) Remove the password
$ openssl rsa -in server.orig.key -out server.key
@xifengzhu
xifengzhu / common-shell.md
Last active November 7, 2017 06:35
程序员开发常用shell 命令

Launch Sublime Text 2 from the Mac OS X Terminal

ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/sublime

Check process with name

ps aux | grep sidekiq
(function() {
// store a list of open refs
var openItems = [];
app.config(function($provide) {
// when creating a $firebaseArray, record the ref so we can destroy it later
$provide.decorator("$firebaseArray", function($delegate) {
return function(ref) {
var list = $delegate(ref);
openItems.push(list);
@xifengzhu
xifengzhu / spellbook_trello.js
Created November 2, 2015 04:38
spellbook script for trello
Trello-filter: no label
javascript: (function () {$(".list-card").each(function(){ if ($(this).find(".list-card-labels").children().length) { $(this).toggle() } }) })();
Trello-filter: no points
javascript: !function(){$(".list-card").each(function(){""!==$(this).find(".list-card-details .badges .badge-points.point-count").text()&&$(this).toggle()})}();
Trello-filter: no assignees
javascript: !function(){var cards=$(".list-card").has(".list-card-details .list-card-members .member");cards.toggle()}();
type the follow in your brower url, you will get a online note panel
1. data:text/html, <style type="text/css">#e{position:absolute;top:0;right:0;bottom:0;left:0;}</style><div id="e"></div><script src="http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script><script>var e=ace.edit("e");e.setTheme("ace/theme/monokai");e.getSession().setMode("ace/mode/ruby");</script>
2. data:text/html, <style>html,body{margin: 0; padding: 0;}</style><textarea style="font-size: 1.5em; line-height: 1.5em; background: %23000; color: %233a3; width: 100%; height: 100%; border: none; outline: none; margin: 0; padding: 90px;" autofocus placeholder="wake up Neo..." />
# coding: utf-8
require 'active_support/callbacks'
class Record
include ActiveSupport::Callbacks
define_callbacks :save
# define_callbacks :kill
def save