支持两种hash方法: crc32 fnv
配置如下:
; Hash function {crc32, fnv}
memcache.hash_function=fnv
set key脚本:
<?php $cluster = array( array(‘host‘ => ‘127.0.0.1‘, ‘port‘ => ‘11211‘), array(‘host‘ => ‘127.0.0.1‘, ‘port‘ => ‘11212‘), array(‘host‘ => ‘127.0.0.1‘, ‘port‘ => ‘11213‘), array(‘host‘ => ‘127.0.0.1‘, ‘port‘ => ‘11214‘), array(‘host‘ => ‘127.0.0.1‘, ‘port‘ => ‘11215‘), ); $memcache = new Memcache; foreach ($cluster as $mc) { $host = $mc[‘host‘]; $port = $mc[‘port‘]; if ($memcache->addServer($host, $port)) { echo "Add $host:$port to cluster\n"; } else { echo "Failed to add $host:$port to cluster\n"; } } $ok = 0; for ($n=1; $n <= 200000 ; $n++) { $key = "k".$n; if (! $memcache->set($key, 1)) { echo "Failed to set $key\n"; } else { $ok ++; } if ($n % 100 == 0) { echo "Completed set $n key to cache\n"; } } echo "Set OK: $ok\n"; $memcache->close(); ?>
启动cache脚本:
#!/bin/bash pkill memcached if [ $? == 0 ];then echo "kill all memcached" else echo "failed to kill cached" exit 1 fi memcached -d -p 11211 -u memcached -m 64 -c 1024 & memcached -d -p 11212 -u memcached -m 64 -c 1024 & memcached -d -p 11213 -u memcached -m 64 -c 1024 & memcached -d -p 11214 -u memcached -m 64 -c 1024 & memcached -d -p 11215 -u memcached -m 64 -c 1024 & sleep 1 ps aux | grep memcached
结果检查脚本:
#!/bin/bash T11211=`echo stats | nc 127.0.0.1 11211 | grep cmd_set | awk ‘{print $3}‘` T11212=`echo stats | nc 127.0.0.1 11212 | grep cmd_set | awk ‘{print $3}‘` T11213=`echo stats | nc 127.0.0.1 11213 | grep cmd_set | awk ‘{print $3}‘` T11214=`echo stats | nc 127.0.0.1 11214 | grep cmd_set | awk ‘{print $3}‘` T11215=`echo stats | nc 127.0.0.1 11215 | grep cmd_set | awk ‘{print $3}‘` echo "11211 = $T11211" echo "11212 = $T11212" echo "11213 = $T11213" echo "11214 = $T11214" echo "11215 = $T11215"
最终结果对比:
crc32:
11211 = 51180
11212 = 47519
11213 = 26179
11214 = 29082
11215 = 46040
fnv:
11211 = 47085
11212 = 35358
11213 = 49721
11214 = 18787
11215 = 49049
时间: 2024-10-05 21:09:59