我在多线程 Java SE 应用程序中使用 couchbase 和 xmemcached 客户端。我经常观察到 couchbase 无法保存最后几个键,而传统的 memcached 可以很好地设置所有键。在这两种情况下,我都使用完全相同的 xmemcachedclient 配置。
弹簧配置
<bean name="memcachedClient" class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean" destroy-method="shutdown">
<property name="configuration">
<bean class="com.google.code.yanf4j.config.Configuration">
<property name="handleReadWriteConcurrently" value="true" />
</bean>
</property>
<property name="servers">
<value>127.0.0.1:11211</value>
</property>
<!-- server's weights -->
<property name="weights">
<list>
<value>2</value>
</list>
</property>
<!-- nio connection pool size -->
<!-- <property name="connectionPoolSize" value="4"></property> -->
<!-- Use binary protocol,default is TextCommandFactory -->
<property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory" />
</property>
<!-- Distributed strategy -->
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator" />
</property>
<!-- Serializing transcoder -->
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>
<!-- ByteBuffer allocator -->
<property name="bufferAllocator">
<bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator" />
</property>
</bean>
下面是我检查丢失键的测试代码
@Test
public void test100000HitMiss()
{
ArrayList<Integer> hits = new ArrayList<Integer>();
ArrayList<Integer> misses = new ArrayList<Integer>();
for(Integer i=0; i < 100000; i++)
{
try
{
Object val = memcachedClient.get("2/C/TALAL" + i.toString());
if(null == val)
misses.add(i);
else
hits.add(i);
}
catch (Exception ex)
{
logger.error(ex);
}
}
logger.info("Hits: " + hits.size() + " - Misses: " + misses.size());
}
每次运行此测试时,我都会在未命中数组中获得 3 或 4 个键