Archive for October, 2008

31
Oct

how to store user picture in the file system

一个好的文件存储结构不但使用、维护方便,关键是调用方便。
废话少说,直接上菜,厨子开始干活 :)
假设用户上传一个头像,系统为其生成大小不同的三种缩略图,并以用户ID做为目录名称,格式如下:
public/images/users/86799/raecoo.gif (原图)
public/images/users/86799/raecoo-big.gif
public/images/users/86799/raecoo-medium.gif
public/images/users/86799/raecoo-thumb.gif
这样的做法在系统规模较小的时候是没什么问题的,但随着用户数量的增加,这样的存储结构肯定是个隐患。下面介绍一种可容纳大规模存储的解决方案,当然这不一定是最好,也不一定适合你 :)

31
Oct

ruby email validation regex expression

this Regex Expression can handle ‘-’ character in Ruby email validation
^\w+((-\w+)|(\-)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$
Ruby Regex Expression test tool : http://www.rubular.com/
BTW:only test in ruby.
感谢ashchan 提供了可以支持Gmail 标签的正则表达式,经测试效果也不错,如下:
/^([^@\s!#\$%\^&\(\)]+)@((?:[-a-z0-9A-Z]+\.)+[a-zA-Z]{2,})$/

25
Oct

rails store format in memcache

在Rails开发过程中,经常需要查看Memcache的存储内容,原来一直通过console方式,但速度太慢,效果不好,今天试图用Telnet去查看。说干就干
telnet 127.0.0.1 11211
get ‘user_login’
ERROR
奇怪,不应该呀,这个Key明显是存到Cache里了,重新打开console模式测试,可以正常取出。这里有点怀疑Rails写入Memcache的机制,是不是加了类似保存Session时的前缀啊。
怎么直接查看Memcache已保存的所有Key,Google之发现使用 stats cachedump slab_id limit_num可以直接查看Memcache某个slab中的前limit_num个key列表,显示格式如下
ITEM key_name [ value_length b; expire_time|access_time s]
嗯,很爽,果不出所料,Rails确实在存入Memcache前做了点手脚,加了一个前缀。现在将前缀加上再取,成功!
stats cachedump 1 10
ITEM active_record:friends:86799 [1 b; 1224840079 s]
ITEM p1_cn-production:ticker_key [4 b; 1224839825 s]
ITEM p1_cn-production:timer_key [4 b; 1224839825 s]
get p1_cn-production:active_record:friends:86799
VALUE p1_cn-production:active_record:friends:86799 0 75
[iP?i恑a?i}?i劽i惷i旅i?i竺i0?iK?i i~igit?i睹
可以看出前缀是根据Rails项目名称和运行模式生成的,还挺聪明

09
Oct

how to use ruby invoke remote url

调用远程URL接口的通用方法,临时封装的,还不完善,仅供参考,相信封装完善后将是一个不错的Libary,其实RESTFul的远程也类似这样,但Rails自己封装的肯定比这个要成熟的多,抽空也Copy一个出来,记录在此

require ‘net/http’
require ‘net/https’
require “singleton”
require “erb”
def invoke_remote
RemoteHttp.instance
end
class RemoteHttp
include Singleton
def call(host,url,request_type,params={})
http = Net::HTTP.new(host)
response, data = http.get(host+url, nil)
puts ‘—————————————————-’
puts response
puts ‘—————————————————-’
puts data
puts ‘—————————————————-’
end
end
invoke_remote.call(’http://www.p1.cn’,'/raecoo’,”,{})

    About

    Tag Cloud