Skip to content

Instantly share code, notes, and snippets.

View dchentech's full-sized avatar
🎯
Focusing

David Chen dchentech

🎯
Focusing
View GitHub Profile
@dchentech
dchentech / sphinx.txt
Created November 20, 2012 06:33
undefined method `to_crc32' for 1:Fixnum
预备知识:如果对条件选择做索引的话,sphinx采用的数据结构均为非字符类型,碰到字符字段,在ThinkingSphinx都被默认经CRC32转换成标识唯一整数。
解决过程:ThinkingSphinx需要对表名做一个CRC32转化,具体在lib/thinking_sphinx/source/sql.rb的crc_column方法。该方法里为了对继承表进行兼容,里面会判断Rails里约定的inheritance_column是否也在表字段里(代码为 @model.column_names.include?(@model.inheritance_column)),如果有就把type字段也作为CRC32的参数。可是如果type为整数类型,那么它没有to_crc32方法,所以就报了undefined method `to_crc32' for 1:Fixnum 错误。
解决方案:给有整数type字段的model加一句 self.inheritance_column = :_type
@dchentech
dchentech / ActiveRecord_without_Rails.rb
Created November 20, 2012 06:57
在Rails外面使用ActiveRecord,并直接声明全部model
ActiveRecord::Base.establish_connection(YAML.load_file('./config/database.yml')['production'])
ActiveRecord::Base.connection.execute("show tables;").to_a.flatten.map do |table_name|
eval "class #{table_name.camelcase} < ActiveRecord::Base; self.table_name = :#{table_name}; end"
end
@dchentech
dchentech / pythonbrew.txt
Created November 26, 2012 07:21
CentOS安装Pygments.rb
系统默认的Python是2.4.3, 使用时报MentosError: Failed to get header错误。
看了下官方文档, pygments.rb需要2.5以上, 因此选择了和ruby的rvm类似的可以管理多版本python的https://github.com/utahta/pythonbrew。
运行默认推荐的 curl -kL http://xrl.us/pythonbrewinstall | bash , 终端提示如下错误:
$ CURLOPT_SSL_VERIFYPEER=false curl -kL http://xrl.us/pythonbrewinstall | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 229 100 229 0 0 299 0 --:--:-- --:--:-- --:--:-- 0
curl: (60) SSL certificate problem, verify that the CA cert is OK. Details:
@dchentech
dchentech / bootstrap.css
Created November 30, 2012 05:46
pygments.rb在bootstrap样式下自动换行
pre {
white-space: pre; /* fix bootstrap conflict with pygments */
}
/* 代码框自动下滑到底部(footer)的原因是table标签未关闭,见: https://github.com/mvj3/pygments.rb/commit/e7ad93a8db7a4b6be94e8fc03661b88797c903e8#lib/pygments/popen.rb */
@dchentech
dchentech / php.markdown
Created November 30, 2012 07:15
osx搭建think php解决方案

10.6 osx 安装php

  1. 配置apache虚拟主机信息, 往 /etc/apache2/extra/httpd-vhosts.conf 写入项目目录配置 <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "/Users/mvj3/eoemobile/code/eoe" ServerName edu.eoe.lo ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log" CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
2. 配置apache, 编辑 /etc/apache2/httpd.conf
@dchentech
dchentech / etag.rb
Created November 30, 2012 09:12
Rails动态文件添加ETag缓存
# 项目提供动态的文件下载,由于文件系统里的位置因加了自定义的目录分级,而与和URL有偏差,所以需要经过控制器来重写文件路径来读取,在此也保证了当前请求静态文件在磁盘上只有一份以节约空间。
# 为了避免单个客户端多次请求同一个文件,在该控制器方法里加上HTTP头部的ETag来进行304缓存。
def file
response.headers['Etag'] = Digest::SHA1.hexdigest(@your_file_instance)
send_file @your_file_instance.path
end
@dchentech
dchentech / css.markdown
Created December 5, 2012 09:24
css hack技巧

div height in percent, and unwanted scrollbar http://stackoverflow.com/a/10607261/595618

if you want to use % as height you have to put that in every parent element:

maybe you could set the container absolute, but that not really a proper coding style.

页面加载时整个页面瞬间闪动

把外引js加载放到页面最下方即可

@dchentech
dchentech / html5shiv.haml
Created December 10, 2012 02:17
HAML引用html5shiv.js
/[if lt IE 9]
%script{:src=>"html5shiv/dist/html5shiv.js"}
@dchentech
dchentech / cache_control.markdown
Created December 13, 2012 10:08
Unicorn和Nginx部署Rails应用的css, js, img等静态资源缓存使用总结

接上文 Rails动态文件添加ETag缓存 来自动生成304 Not Modified告诉客户端来直接使用本地缓存,但是在这种情况下会出现图片闪动(重新加载渲染),影响用户体验。除了Etag这种通过和服务器进行HTTP头部比对来实现缓存的策略外,还可以采用Cache-control来通知浏览器在一段时间内不必重新请求服务器而直接本地缓存。配置如下:

nginx.conf

使用root用户来避免文件权限问题而导致403错误

user root nginx;

在server { } 里加入Cache-Control声明

location ~ ^/(assets)/ {
gzip_static on;
expires 24h;
add_header Cache-Control public;

@dchentech
dchentech / trie_vs_hash_performance.rb
Last active December 21, 2020 15:55
Trie和Hash察看单个字符串性能比较
%w[benchmark algorithms open-uri nokogiri].map &method(:require)
# 摘自wikipedia的trie词条
# 在计算机科学中,trie,又称前缀树,是一种有序树,用于保存关联数组,其中的键通常是字符串。与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定。一个节点的所有子孙都有相同的前缀,也就是这个节点对应的字符串,而根节点对应空字符串。一般情况下,不是所有的节点都有对应的值,只有叶子节点和部分内部节点所对应的键才有相关的值。
# 摘自wikipedia的Hash词条
# 散列函数(或散列算法,英语:Hash Function)是一种从任何一种数据中创建小的数字“指纹”的方法。散列函数把消息或数据压缩成摘要,使得数据量变小,将数据的格式固定下来。该函数将数据打乱混合,重新创建一个叫做散列值的指纹。散列值通常用来代表一个短的随机字母和数字组成的字符串。好的散列函数在输入域中很少出现散列冲突。在散列表和数据处理中,不抑制冲突来区别数据,会使得数据库记录更难找到。
# 用同样的字符串数组来各自构造一万以上元素的Hash和Trie
string_array_for_insert = Nokogiri(open('http://www.douban.com').read).text.split("\n").map(&:strip).uniq
string_array_for_insert = (string_array_for_insert*100).map {|i| i.split(//).shuffle.join }