python判断redis中key是否存在_Redis中关于Key的模糊查找最近使⽤Redis优化项⽬功能,其中有⼀部分为模糊查询,找了很多帖⼦,也没有找到很好的解决⽅案和思路,最终皇天不负有⼼⼈啊,终于让我找到了
可以通过Redis中keys命令进⾏获取key值,具体命令格式:keys pattern
姜汁红糖⽂中提到redis中允许模糊查询的有3个通配符,分别是:*,?,[]
其中:
*:通配任意多个字符
:通配单个字符
[]:通配括号内的某⼀个字符
=========================================================================
在实际项⽬中有可能会使⽤spring集成redis的RedisTemplate进⾏操作,这样在注⼊模板时可能会出现模糊查询不好⽤的情况,是因为keys⽅法是存在于StringRedisTemplate⼦类中(⽗类-RedisTemplate)的
在spring配置时,⼀定要注意
=========================================================================
实际操作中具体的思路:
1. 将要查询的条件当做key进⾏ZSet存储
2. 在获取时,调⽤StringRedisTemplate.keys(pattern),例:
1
2
3
4
精品工厂
5
publicSet keys(String pattern){
returnstringRedisTemplate.keys("*"+ pattern + "*");
// return stringRedisTemplate.keys("?" + pattern);
// return stringRedisTemplate.keys("[" + pattern + "]");
圆明园的毁灭读后感}
ps:模糊查找适⽤于 String数据结构,对redis⽀持的其他结构(List、t等),没有 验证是否⽀持。
实践代码:
/**
* redis缓存操作类
*/
@Service
publicclassRedisCacheServiceimplementsInitializingBean{
privateValueOperations valueOperations;
@Autowired
privateStringRedisTemplate redisTemplate;
@SuppressWarnings("unchecked")
@Override
publicvoidafterPropertiesSet()throwsException{
RedisSerializer redisSerializer =newStringRedisSerializer();
valueOperations = redisTemplate.opsForValue();
}
/**
* 从缓存中获取资源信息
*@paramkey
*@return
*/
张北坝上草原publicListgetCacheResource(String key){
Set keys = redisTemplate.keys(key);
if(CollectionUtils.isEmpty(keys)) {
returnnewArrayList<>();
}
List resourceCacheBOList =newArrayList<>();
for(String accurateKey : keys) {
String cacheValue = (accurateKey);
List sub = JSONArray.parArray(cacheValue, ResourceCacheBO.class);
resourceCacheBOList.addAll(sub);
}
returnresourceCacheBOList;
}
遇到问题:存在key:“A_091_JPFX”,但是⽤ 模糊key:“A_*_JPFX”匹配时,却匹配不到;有资料说是编码问题,按下⾯⽅式解决:在使⽤redisTemplate.keys查找keys时,发现明明相应的key是存在的,模糊查询就是查找不出来;原因有⼆:
1.确定你的查询字符串是否正确
2.就是的你key值有可能是乱码了就是遇到\xca\xed加上你key之类的乱码!例如:这⾥写图⽚描述
你需要重新定义key
@Bean
publicRedisTemplateredisTemplate(RedisConnectionFactory factory){
RedisTemplate redisTemplate =newRedisTemplate();
redisTemplate.tConnectionFactory(factory);
//key序列化⽅式;(不然会出现乱码;),但是如果⽅法上有Long等⾮String类型的话,会报类型转换错误;
//所以在没有⾃⼰定义key⽣成策略的时候,以下这个代码建议不要这么写,可以不配置或者⾃⼰实现ObjectRedisSerializer //或者JdkSerializationRedisSerializer序列化⽅式;
RedisSerializer redisSerializer =newStringRedisSerializer();//Long类型不可以会出现异常信息;
redisTemplate.tKeySerializer(redisSerializer);
redisTemplate.tHashKeySerializer(redisSerializer);
returnredisTemplate;
}
完整代码:
scarf复数word.analysis.rvice;
importcom.alibaba.fastjson.JSONArray;
importorg.springframework.beans.factory.InitializingBean;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.StringRedisTemplate;
importorg.ValueOperations;
取钱英语
importorg.dis.rializer.RedisSerializer;
importorg.dis.rializer.StringRedisSerializer;
importorg.springframework.stereotype.Service;
importorg.springframework.util.CollectionUtils;
importorg.springframework.util.StringUtils;
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.List;
importjava.util.Set;
/**
* redis缓存操作类
右眼皮跳代表什么
*/
@Service
publicclassRedisCacheServiceimplementsInitializingBean{
privateValueOperations valueOperations;
@Autowired
privateStringRedisTemplate redisTemplate;
@SuppressWarnings("unchecked")
@Override
publicvoidafterPropertiesSet()throwsException{
RedisSerializer redisSerializer =newStringRedisSerializer();//Long类型不可以会出现异常信息; redisTemplate.tKeySerializer(redisSerializer);
redisTemplate.tHashKeySerializer(redisSerializer);
valueOperations = redisTemplate.opsForValue();
}
/**
* 缓存资源
*@paramkey
*@paramresourceCacheBOList
*/
publicvoidcacheResource(String key, List resourceCacheBOList){
// 参数校验
if(CollectionUtils.isEmpty(resourceCacheBOList)) {
thrownewBizException(1,"参数有误");
}
// 缓存
String resourceCacheValue = JacksonJsonUtil.objectToString(resourceCacheBOList); valueOperations.t(key, resourceCacheValue);
}
/**
* 从缓存中获取资源信息
*@paramkey
*@return
*/
// TODO 待测试
publicListgetCacheResource(String key){
Set keys = redisTemplate.keys(key);
if(CollectionUtils.isEmpty(keys)) {
returnnewArrayList<>();
}
List resourceCacheBOList =newArrayList<>();
for(String accurateKey : keys) {
String cacheValue = (accurateKey);
List sub = JSONArray.parArray(cacheValue, ResourceCacheBO.class); resourceCacheBOList.addAll(sub);
}
returnresourceCacheBOList;
}
/**
* 缓存 关键字的编码
*@paramkey
*@return
*/
publicvoidcacheKeyWordCode(String key, String code){
// 参数校验
if(StringUtils.isEmpty(key) || StringUtils.isEmpty(code)) { thrownewBizException(1,"参数有误");
}
// 缓存
valueOperations.t(key, code);
}留学个人陈述
/**
* 获取 关键字的编码
*@paramkey
*@return
*/
publicStringgetKeyWordCode(String key){
String keyWordCode = (key);
if(StringUtils.isEmpty(keyWordCode)) {
returnnull;
}el{