一个好的文件存储结构不但使用、维护方便,关键是调用方便。
废话少说,直接上菜,厨子开始干活 :)
假设用户上传一个头像,系统为其生成大小不同的三种缩略图,并以用户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
这样的做法在系统规模较小的时候是没什么问题的,但随着用户数量的增加,这样的存储结构肯定是个隐患。下面介绍一种可容纳大规模存储的解决方案,当然这不一定是最好,也不一定适合你 :)
所谓可容纳大规模存储就是将文件存储进行分级,以便不会在单一目录下存储过多的图片。
这里的方案是按用户ID进行分级,方法很简单,对用户ID进行100的取模运算(具体的方法就不写了,相信你比我聪明),例如:
raecoo的ID是86799,那运算后的结果就是 8/67/99,这里还需要将用户数量的目标规模估计在内,假设系统目标用户容量为千万级,那结构就应该是这样了 public/images/users/00/08/67/99/raecoo.gif。采用这样的存储结构上面的例子也就变为:
public/images/users/00/08/67/99/raecoo.gif (原图)
public/images/users/00/08/67/99/raecoo-big.gif
public/images/users/00/08/67/99/raecoo-medium.gif
public/images/users/00/08/67/99/raecoo-thumb.gif
上述目录中的0是在用户ID不达千万级的数字时进行补齐使用的。当然文件名还可以用用户ID进行命名,这样就能够在仅知道用户ID时直接显示用户头像(public/images/users/00/08/67/99/86799.gif)。












Leave a comment
Fields in bold are required. Email addresses are never published or distributed.
Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>URLs must be fully qualified (eg: http://www.raecoo.com),and all tags must be properly closed.
Line breaks and paragraphs are automatically converted.
Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.