0

I use Memcache on my web server and sometimes I got an error like

java.lang.IllegalArgumentException: Key is too long (maxlen = 250)

I want to find some solution to decrease the length. Is it possible? I've read, that I can do that with sha 1 or something else.

For example in pseudocode

String key = server.getClient();
String value = server.getValue();

String sha1 = hash(key+value);
memcache.add(key,sha1);

Is it possible?

But I think it would cost much more than I wouldn't use cache for request with length more than 250 symbols, wouldn't it?

Dred
  • 1,076
  • 8
  • 24
  • It might cost more if you just change your behavior on cache length. So, yes, you can hash your keys to guarantee they're shorter than a certain limit. SHA-256, for instance, generates a 32-byte hash and you can check how it is done [here](https://stackoverflow.com/q/5531455/4636715) – vahdet May 13 '19 at 06:43
  • @vahdet , Do you mean, it is better to ignore those exceptions? – Dred May 13 '19 at 06:45
  • No, if you hash keys, you will no longer get that kind of exceptions indeed. I object to the notion *'if key is long, I wont cache it'*. – vahdet May 13 '19 at 06:47

0 Answers0