Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
I may be slow to respond.

Aleksey Leshchuk alekseyl

💭
I may be slow to respond.
View GitHub Profile
@alekseyl
alekseyl / ar_example.rb
Last active Mar 31, 2021
Active record structure
View ar_example.rb
# Rails style guided+
#-------------------- 1. includes and extend --------------
#-------------------- 2. default scope --------------------
#-------------------- 3. inner classes --------------------
#-------------------- 4. constants ------------------------
#-------------------- 5. attr related macros --------------
#-------------------- 6. enums ----------------------------
#-------------------- 7. scopes ---------------------------
#-------------------- 8. has and belongs ------------------
#-------------------- 9. accept nested macros ------------
@alekseyl
alekseyl / model.rb
Last active Mar 31, 2021
Active record model template upgraded.
View model.rb
<% module_namespacing do -%>
class <%= class_name %> < <%= parent_class_name.classify %>
# Rails style guided+
#-------------------- 1. includes and extend --------------
#-------------------- 2. default scope --------------------
#-------------------- 3. inner classes --------------------
#-------------------- 5. attr related macros --------------
#-------------------- 6. enums ----------------------------
#-------------------- 7. scopes ---------------------------
View comments_controoler.rb
class CommentsController < ApplicationController
# I assumed the usual naming for models and foreign columns
def users_comments
# I don't like where( post_id: Post.all ) but if table comments
# contains deleted posts comments or even NULLs than we need to keep it this way
@user_comments = Comment.where( post_id: Post.all )
.where(author_id: Author.where( username: params[:username]) )
end
end
@alekseyl
alekseyl / comparision
Last active May 28, 2018
comparision of redis memory optimization, for https://medium.com/@leshchuk/zipem-all-61076c7da4c
View comparision
# Columns:
# redis - clear memory usage of 100K values in Mb,
# gz - gz compressed value, smaz = smaz compressed value,
# key zip = zipping key crumbs to cingle char,
# me_ - memory_efficient_ hash optimization
#integers, html pieces, plain english text pieces
+----------+--------+--------+--------+---------+--------+----------+------------+
| type | redis | gz | smaz | key zip | me_* | kz+me+gz | kz+me+smaz |
+----------+--------+--------+--------+---------+--------+----------+------------+
@alekseyl
alekseyl / calc_performance
Last active May 18, 2018
compare redis performance for ruby hmethods VS methods
View calc_performance
def calc_me_redis_perfomance_hash_zipping( zip_max = 128, sample_size = 10000 )
Redis.include(MeRedis)
redis = Redis.new
Redis.configure do |c|
c.zip_crumbs = :user
c.integers_to_base62 = true
c.hash_max_ziplist_entries = zip_max
end
@alekseyl
alekseyl / me_redis_configure.rb
Last active May 18, 2018
MeRedis confiugure examples
View me_redis_configure.rb
Redis.include( MeRedis )
# zip key crumbs user, card, card_preview, zips integer crumbs to base62,
# for keys starting with card_preview prefix compress values with Zlib
# for keys starting with user or card compress values with ActiveRecordJSONCompressor
Redis.configure(
hash_max_ziplist_entries: 256,
zip_crumbs: %i[user card card_preview], # -> { user: :u, card: :c, card_preview: :c0 }
integers_to_base62: true,
compress_namespaces: {
:card_preview => MeRedis::ZipValues::ZlibCompressor,
@alekseyl
alekseyl / json_compressor.rb
Last active May 17, 2018
JSON compressor example for me-redis gem
View json_compressor.rb
module RSmazCompressor
def self.compress(value); RSmaz.compress(value) end
def self.decompress(value); RSmaz.decompress(value) end
end
module ZLibJSONCompressor
def self.compress(value); Zlib.deflate(value.to_json) end
def self.decompress(value); JSON.load( Zlib.inflate( value ) )end
end
@alekseyl
alekseyl / redis_memory_test.rb
Last active May 14, 2018
redis_memory_test example for key length comparision
View redis_memory_test.rb
def redis_memory_test
results = {}
redis = Redis.new
redis.flushdb
results[:clear] = redis.info(:memory)['used_memory'].to_i
(2..128).each do |i|
redis.pipelined do
10000.times { redis.incr( SecureRandom.urlsafe_base64( i ) ) }
View gist:76c37965c347b9e253580bde33fcd79e
# def to_base62
# Base62.encode(to_s)
# end
2.4.1 :001 > redis = Redis.new
=> #<Redis client v4.0.1 for redis://127.0.0.1:6379/0>
2.4.1 :002 > redis.flushdb
2.4.1 :003 > redis.info(:memory)['used_memory_human']
=> "670.55K"
2.4.1 :004 > redis.pipelined { 100000.times{|i| redis.incr( "user:#{i}" ) }; }; :done
@alekseyl
alekseyl / nginx_cache_full_example.conf
Last active Mar 28, 2018
nginx/rails cache config example
View nginx_cache_full_example.conf
proxy_cache_pass /path/to/cache levels= keys_zone=rails_cache:10m max_size=10g use_temp_path=off inactive=120m;
location / {
try_files $uri @rails;
}
location @rails {
# lets skip authorized user.
proxy_no_cache $cookie_remember_user_token;
proxy_cache_bypass $cookie_remember_user_token;