int main()
{
int server = getServerid("test",3);
printf("%d",server);
}
int getServerid(char *key,int serverTotal)
{
printf("%d \n",*key);
int c,hash = 0;
while(c = *key++)
{
hash +=c;
}
printf("%d \n",c);
printf("%d \n",hash);
return hash % serverTotal;
}
输出结果为:
116
0
448
1Press any key to continue
此段代码是memcached在客户端执行的代码,根据一个key来获得这个key和value应该存放的memcached实例号码,然后每次对于这个key的读取和写入等操作,都去那个实力上进行操作。
如例子:key为test,那么部分数据会存在memcached列表的第一个实例上。