redis 解决key的乱码问题,并清理详解
key乱码问题
因redis默认使用jdkserializationredisserializer来进行序列化,造成key是乱码,如下:
keys '*!report:flag:phon*'
1) "\xac\xed\x00\x05t\x00!report:flag:phone_156464"
2) "\xac\xed\x00\x05t\x00!report:flag:phone_198946"
3) "\xac\xed\x00\x05t\x00!report:flag:phone_183302"
解决key乱码
private redistemplate redistemplate; @autowired(required = false) public void setredistemplate(redistemplate redistemplate) { redisserializer stringserializer = new stringredisserializer(); redistemplate.setkeyserializer(stringserializer); redistemplate.setvalueserializer(stringserializer); redistemplate.sethashkeyserializer(stringserializer); redistemplate.sethashvalueserializer(stringserializer); this.redistemplate = redistemplate; }
清理乱码key
官方不支持 del '*keys'方式模糊/批量删除key。但是keys命令支持模糊匹配,所以采取以下方式:
方式1:可删除正常key,无法删除乱码key
redis-cli -h 192.168.1.21 -a password -n 2 --scan --pattern '*!report:flag:phon*' | xargs redis-cli -h 192.168.1.21 -a password -n 2 del
方式2:成功删除
del "\xac\xed\x00\x05t\x00!report:flag:phone_183302" "\xac\xed\x00\x05t\x00!report:flag:phone_198946"
补充知识:redis key和value的乱码问题解决,含日期转化格式问题
在项目中,遇到的问题是redis的key和value出现的乱码问题:在这里插入图片描述
而原本的内容为下:
{ "status":"success", "data":{ "id":3, "title":"花林", "price":99, "stock":81, "description":"美女一只", "sales":17, "imgurl":"https://xiaolei1996.oss-cn-shanghai.aliyuncs.com/blog/title/we1.jpg", "promostatus":2, "promoprice":50, "promoid":1, "startdate":"2020-03-23 21:50:59" } }
原因: 是因为和redis内部的编码协议出现了问题,所以需要改进。spring提供了一个优化方案。springboot的redistemplate改进。
@component @enableredishttpsession(maxinactiveintervalinseconds = 3600) public class redisconfig { @bean public redistemplate redistemplate(redisconnectionfactory factory){ redistemplate redistemplate = new redistemplate(); redistemplate.setconnectionfactory(factory); //首先解决key的序列化问题 stringredisserializer stringredisserializer = new stringredisserializer(); redistemplate.setkeyserializer(stringredisserializer); //解决value的序列化问题 jackson2jsonredisserializer jackson2jsonredisserializer = new jackson2jsonredisserializer(object.class); redistemplate.setvalueserializer(jackson2jsonredisserializer); return redistemplate; } }
比之前好了,但是还有点小问题,json的数据比以前多了,这是因为日期的转化出现问题,这块的知识触及盲区,就先把解决方案写下面,以后有时间在研究。
public class jodadatetimejsonserializer extends jsonserializer<datetime> { @override public void serialize(datetime value, jsongenerator gen, serializerprovider serializers) throws ioexception { gen.writestring(value.tostring("yyyy-mm-dd hh:mm:ss")); } }
public class jodadatetimejsondeserializer extends jsondeserializer<datetime> { @override public datetime deserialize(jsonparser p, deserializationcontext ctxt ) throws ioexception, jsonprocessingexception { string datestring= p.readvalueas(string.class); datetimeformatter datetimeformatter = datetimeformat.forpattern("yyyy-mm-dd hh:mm:ss"); return datetime.parse(datestring,datetimeformatter);//转成 } }
@component @enableredishttpsession(maxinactiveintervalinseconds = 3600) public class redisconfig { @bean public redistemplate redistemplate(redisconnectionfactory factory){ redistemplate redistemplate = new redistemplate(); redistemplate.setconnectionfactory(factory); //首先解决key的序列化问题 stringredisserializer stringredisserializer = new stringredisserializer(); redistemplate.setkeyserializer(stringredisserializer); //解决value的序列化问题 jackson2jsonredisserializer jackson2jsonredisserializer = new jackson2jsonredisserializer(object.class); redistemplate.setvalueserializer(jackson2jsonredisserializer); //改进日期转化问题 objectmapper objectmapper = new objectmapper(); simplemodule simplemodule = new simplemodule(); simplemodule.addserializer(datetime.class,new jodadatetimejsonserializer()); simplemodule.adddeserializer(datetime.class,new jodadatetimejsondeserializer()); //解决反序列化问题 objectmapper.enabledefaulttyping(objectmapper.defaulttyping.non_final); objectmapper.registermodule(simplemodule); jackson2jsonredisserializer.setobjectmapper(objectmapper); redistemplate.setvalueserializer(jackson2jsonredisserializer); return redistemplate; } }
最后终于出现了预期的效果
以上这篇redis 解决key的乱码问题,并清理详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。