Using 'incr' with spymemcached client

counter, hitcounter, increment, memcached, spymemcached

Solution

For incr/decr memcached server considers stored values as string representation of integer,

Adding an item like

    memcachedClient.set(clientId, 1800, 1);

adds value as an integer that's why memcached is unable to increment it.

Use

    memcachedClient.set(clientId, 1800, "1");

instead. It will work as you need.

Problem

I am trying set up a very basic hit rate monitor in memcached using the spymemcached-2.8.4 client however the value stored in memcached never actually seems to increment... Is this a bug or am I missing something? ``` 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."); } } ``` I know its possible in memcached: (TELENT output) ``` set clientId_1 0 200 1 1 STORED incr clientId_1 1 2 get clientId_1 VALUE clientId_1 0 1 2 END ```

Original source