How to store the key size more than 250 characters in memcached using java code?

3,075 views
Skip to first unread message

sekhar mekala

unread,
Dec 30, 2014, 9:33:03 AM12/30/14
to memc...@googlegroups.com
HI Team,


 I need to store the key size more than 250 characters in memcached server.

I am tried with my code:

code:
 MemcachedClient client = new MemcachedClient((new InetSocketAddress("192.168.7.134", 11211)));
 String key="292FEC76-5F1C-486F-85A5-09D88096F098_VirtualizationObjectNamesInfo_CustomerManagementObject#292FEC76-5F1C-486F-85A5-09D88096F098#"+ "(TENANT_PARENTOBJECTID='FFCEF160-BAEE-4876-9C45-8D179DC90481'$OR$TENANT_PARENTOBJECTID='B813170B-3390-4B51-9E6C-81EF3D44BB94'$"+ "OR$TENANT_PARENTOBJECTID='CD8C669F-A8AA-4987-9B71-ABFBF5207014'$OR$TENANT_PARENTOBJECTID='373F4223-BD1E-4B4C-AE86-F0B09F3D7FBE'$"+ "OR$TENANT_PARENTOBJECTID='1DE854CA-2E51-4B6B-A3F2-2EC1A9626D1D'$OR$TENANT_PARENTOBJECTID='32C4B07F-804B-45E1-A594-03472945FC1E'$OR"+ "$TENANT_PARENTOBJECTID='608E4495-8FF9-49DB-8476-CAFCFF84A5B1'$OR"+ "$TENANT_PARENTOBJECTID='ACFEB0D5-586E-45C1-A3B4-24781C1D07A5')#(TENANT_CHILDOBJECTID='FFCEF160-BAEE-4876-9C45-8D179DC90481'$OR"+ "$TENANT_CHILDOBJECTID='B813170B-3390-4B51-9E6C-81EF3D44BB94'$OR$TENANT_CHILDOBJECTID='CD8C669F-A8AA-4987-9B71-ABFBF5207014'$OR"+ "$TENANT_CHILDOBJECTID='373F4223-BD1E-4B4C-AE86-F0B09F3D7FBE'$OR$TENANT_CHILDOBJECTID='1DE854CA-2E51-4B6B-A3F2-2EC1A9626D1D'$OR"+ "$TENANT_CHILDOBJECTID='32C4B07F-804B-45E1-A594-03472945FC1E'$OR$TENANT_CHILDOBJECTID='608E4495-8FF9-49DB-8476-CAFCFF84A5B1'$OR"+ "$TENANT_CHILDOBJECTID='ACFEB0D5-586E-45C1-A3B4-24781C1D07A5')#978754F1-AE6D-4201-85B3-0008BD3EE190_v1.0";
  
System.out.println("Key size:" + key.length());
client.set(key, 3600, "teststse");
System.out.println(client.get(key));

Error:
2014-12-30 19:20:57.944 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=/192.168.7.134:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
Key size:1184
2014-12-30 19:20:57.947 INFO net.spy.memcached.MemcachedConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@6c6455ae
Exception in thread "main" java.lang.IllegalArgumentException: Key is too long (maxlen = 250)
at net.spy.memcached.util.StringUtils.validateKey(StringUtils.java:69)
at net.spy.memcached.MemcachedConnection.enqueueOperation(MemcachedConnection.java:745)
at net.spy.memcached.MemcachedClient.asyncStore(MemcachedClient.java:310)
at net.spy.memcached.MemcachedClient.set(MemcachedClient.java:844)
at MemcachedConnectionPooling.ClearedMemcachedServer.main(ClearedMemcachedServer.java:42)



Client: spymemcached
Server: memcached

plz help me.
Thanks in advace

Sekhar.



Zhiwei Chan

unread,
Dec 30, 2014, 10:22:51 AM12/30/14
to memc...@googlegroups.com
It is a limitation of the memcached server, so you have to change the 'KEY_MAX_LENGTH' value and re-compile your memcached server.
But I don't think it is necessary to do this, it waste a lot of memory by the long key.

--

---
You received this message because you are subscribed to the Google Groups "memcached" group.
To unsubscribe from this group and stop receiving emails from it, send an email to memcached+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ryan McElroy

unread,
Dec 30, 2014, 12:22:24 PM12/30/14
to memc...@googlegroups.com
You can't; its a on-purpose limitation of memcached. What you can do is use md5 or sha1 to hash your string and use that as a key.

Long keys are usually a sign of misuse of memcached, though, so it often helps to take a step back and evaluate what you actually need to put into your key.

~Ryan (mobile)

sekhar mekala

unread,
Jan 6, 2015, 9:01:28 AM1/6/15
to memc...@googlegroups.com
HI Ryan,

Thanks for your valuable suggestion. But using md5 or sha1 to Hash your string, This key is collision or not. 

Please help me.

Thanks
Sekhar
Message has been deleted

Zhiwei Chan

unread,
Jan 7, 2015, 3:10:50 AM1/7/15
to memc...@googlegroups.com
In fact, it is almost impossible to meet a conflict. I think it is safe enough.  see this link for more info: http://en.wikipedia.org/wiki/SHA-1

2015-01-07 14:11 GMT+08:00 sekhar mekala <msekh...@gmail.com>:
HI Ryan,

Thanks for your valuable suggestion. But using md5 or sha1 to Hash your string, This key is collision or not. 

Please help me.

Thanks
Sekhar

On Tuesday, December 30, 2014 10:52:24 PM UTC+5:30, Ryan McElroy wrote:
Reply all
Reply to author
Forward
0 new messages