显示文章列表时同时显示作者的处理技巧
无论是论坛还是社区,经常会有这样的问题, 即在显示文章列表的时候同时显示这个发帖人的名字. 一般可以用联表查询的方式来获取, 因为在文章表里面通常只存一下用户的ID,但是老是经常这样联总是觉得不爽, 而且在网站用户规模扩大的时候你甚至需要将用户分别存入多个表 ,这个时候就比较麻烦了. 如果我们只取文章表的内容,需要人的名字的时候通过其它的手段获取会怎么样呢?
首先当然想到的是缓存, 如果全部只是人的名字的话占用的空间应该不大, 就使用 id/名字 这样的结构来存储. 这样就至少有两个选择, HashTable, Dictionary<KT,VT>,这两个数据结构都可以达到我们的要求(这里ID显然是不重复的). 那么这样做性能如何呢, 可以写一个小小的测试. 下面是测试代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Collections.Generic;
![](/Images/OutliningIndicators/None.gif)
namespace HashTableTest
{
public class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
for (int i = 0; i < 1000000; i++)
{
ht.Add(i, i.ToString());
}
![](/Images/OutliningIndicators/InBlock.gif)
DateTime start = DateTime.Now;
for (int k = 0; k < 1000000; k++)
{
string str = (string)ht[k];
}
![](/Images/OutliningIndicators/InBlock.gif)
TimeSpan time1 = (TimeSpan)(DateTime.Now - start);
Console.WriteLine("HashTable花费时间 : " + time1 .TotalSeconds.ToString() + "秒");
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
Dictionary<long, string> dic = new Dictionary<long, string>();
![](/Images/OutliningIndicators/InBlock.gif)
for (long ii = 0; ii < 1000000; ii++)
{
dic.Add(ii, ii.ToString());
}
![](/Images/OutliningIndicators/InBlock.gif)
DateTime start2 = DateTime.Now;
![](/Images/OutliningIndicators/InBlock.gif)
for (long kk = 0; kk < 1000000; kk++)
{
string str2 = dic[kk];
}
![](/Images/OutliningIndicators/InBlock.gif)
TimeSpan time2 = (TimeSpan)(DateTime.Now - start2);
Console.WriteLine("Dictionary<>花费时间 : " + time2.TotalSeconds.ToString() + "秒");
![](/Images/OutliningIndicators/InBlock.gif)
Console.ReadLine();
}
}
}
在沙加的神舟电脑上测试结果 HashTable的时间是 0.140, 泛型字典的时间 : 0.0625, 可以看出泛型还是占有绝对优势的. 速度也非常快. 如果用户还不多的话完全可以一次性加载所有的数据. 这样少一次联表查询很多时候就会很方便了.
首先当然想到的是缓存, 如果全部只是人的名字的话占用的空间应该不大, 就使用 id/名字 这样的结构来存储. 这样就至少有两个选择, HashTable, Dictionary<KT,VT>,这两个数据结构都可以达到我们的要求(这里ID显然是不重复的). 那么这样做性能如何呢, 可以写一个小小的测试. 下面是测试代码:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
在沙加的神舟电脑上测试结果 HashTable的时间是 0.140, 泛型字典的时间 : 0.0625, 可以看出泛型还是占有绝对优势的. 速度也非常快. 如果用户还不多的话完全可以一次性加载所有的数据. 这样少一次联表查询很多时候就会很方便了.