今天在做一个mysql查询结果返回时,发现用查询的sql语句做memcache的key有问题,后来想了下有可能是因为key中不能包含空格吧,在网上查了一下,果然如此。所以用正则转换了一下key的内容,另外别忘了key有长度限制,最多250个字符。
代码如下:
$key = preg_replace('/\s+/','',$key);
附memcahce关于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.