开发搜索功能的时候,需要搜索词分词并且标红。因为搜索词标红是一个费时的操作,所以对于搜素词的分词结果做了五分钟的缓存。并且以搜索词为key,开发完毕。某次搜索之后,发现了异常的log,
文本如下:
java.lang.IllegalArgumentException: Key contains invalid characters: ``mem_search_segment_pattern_str_思维 曲线 ^&''
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
这才想起来memcache key不能有空格,翻看了memcache /doc/protocal.txt对key的介绍如下
Keys
----
Data stored by memcached is identified with the help of a key. A key
is a text string which should uniquely identify the data for clients
that are interested in storing and retrieving it. Currently the
length limit of a key is set at 250 characters (of course, normally
clients wouldn't need to use such long keys); the key must not include
control characters or whitespace.
----
Data stored by memcached is identified with the help of a key. A key
is a text string which should uniquely identify the data for clients
that are interested in storing and retrieving it. Currently the
length limit of a key is set at 250 characters (of course, normally
clients wouldn't need to use such long keys); the key must not include
control characters or whitespace.
第一感觉使用正则表达式
String newKey = key.replaceAll("\\p{Cntrl}]|\\p{Space}", "_");
另外长度要控制在250个字符。。