【发布时间】:2012-10-16 03:39:20
【问题描述】:
我正在尝试使用 spymemcached-2.8.4 客户端在 memcached 中设置一个非常基本的命中率监视器,但是存储在 memcached 中的值实际上似乎从未增加...这是一个错误还是我遗漏了什么?
public static void checkHitRate(String clientId) throws FatalException, ForbiddenException {
MemcachedClient memcachedClient;
try {
memcachedClient = new MemcachedClient(new InetSocketAddress("localhost", 11211));
Integer hitRate = (Integer) memcachedClient.get(clientId);
if (hitRate == null) {
memcachedClient.set(clientId, 1800, 1);
} else if (hitRate > 500) {
throw new ForbiddenException("Hit rate too high. Try again later");
} else if (hitRate <= 500) {
memcachedClient.incr(clientId, 1);
}
} catch (IOException e) {
throw new FatalException("Could not read hit rate from Memcached.");
}
}
我知道它在 memcached 中是可能的:
(TELENT 输出)
set clientId_1 0 200 1
1
STORED
incr clientId_1 1
2
get clientId_1
VALUE clientId_1 0 1
2
END
【问题讨论】:
-
您是否尝试过使用 telnet 连接到 memcached 服务器并手动执行此操作?它有效吗?查看 memcached 命令:javaworld.com/javaworld/jw-04-2012/…
-
是的,这是可能的(见编辑)...
-
尝试为 memcached 服务器启用详细模式(例如,在命令行中运行 memcached -vv)以在 incr 操作期间查看服务器上的错误(它抱怨“CLIENT_ERROR 无法增加或减少非数字值” ) 同样在您的 Java 应用程序中,您应该得到异常...
-
我建议您检查一下 java 代码中的 set 和 incr 函数返回什么。这样您就可以查看某个操作是否由于某种原因而失败。
标签: memcached counter increment spymemcached hitcounter