Jedis 实现多条件查询

来源:互联网 发布:醉虾 知乎 编辑:程序博客网 时间:2024/06/05 03:34

封装的Redis操作的工具类

这里面存取Redis的key和value全部都先序列化了的

public  class DefaultJedisPoolClient {    private static JedisPool pool = null;    //封装的序列化接口    private ISerializer serializer;    public Set<String> sinter(String... keys) {        Jedis jedis = null;        try{            jedis = pool.getResource();            Set<byte[]> bytesSet = jedis.sinter(serializer.serialize(keys));            String str = null;            Set<String> sinter = new HashSet<>();            for (byte[] bytes : bytesSet) {                str = (String) serializer.unserialize(bytes);                sinter.add(str);            }            return sinter;        }finally {            if(jedis!=null){                jedis.close();            }        }    }    public void sadd(String key, String... macs) {        if(macs==null||macs.length==0)return;        Jedis jedis = null;        try{            jedis = pool.getResource();            jedis.sadd(serializer.serialize(key), convertObjectsToBytes(macs));        }finally {            if(jedis!=null){                jedis.close();            }        }    }    public void hmset(String key, Map<String,Object> values) {        if(values==null||values.size()==0)return;        Jedis jedis = null;        try{            jedis = pool.getResource();            jedis.hmset(serializer.serialize(key), convertObjectsToBytes(values));        }finally {            if(jedis!=null){                jedis.close();            }        }    }    public Map<byte[],byte[]> convertObjectsToBytes(Map<String,Object> objects){        Map<byte[],byte[]> result = new HashMap<>();        for(String key:objects.keySet()){            result.put(serializer.serialize(key),serializer.serialize(objects.get(key)));        }        return result;    }}    

Service中调用方法存取Redis

@Servicepublic class RedisServiceImpl{    @Autowired    private DefaultJedisPoolClient poolClient;    /**     * 存Redis的方法     * 参数list:存有a,b ,c,d参数的HashMap的list集合     */    public void addToRedis(List<Map<String, Object>> list) {        String a;        String b;        String c;        String d;        //查询当前Redis中的数据        Map<String, Object> infos = poolClient.hgetall("list");        for (Map<String, Object> info : list){            //添加数据,遍历完了再重新添加到Redis中            infos.put(info.get("id")+"",info);            a = (String) info.get("a");            if (a!= null && !"".equals(a)){                //以a的值为key,当前id为value,添加到Set集合中,之后多条件查询id用                poolClient.sadd(info.get("a")+"",info.get("id")+"");            }            b= (String) info.get("b");            if (b!= null && !"".equals(b)){                poolClient.sadd(info.get("b")+"",info.get("id")+"");            }            c= (String) info.get("c");            if (c!= null && !"".equals(c)){                poolClient.sadd(info.get("c")+"",info.get("id")+"");            }            d= (String) info.get("d");            if (d!= null && !"".equals(d)){                poolClient.sadd(info.get("d")+"",info.get("id")+"");            }        }        poolClient.hmset("list",infos);    }    /**     * 取Redis数据的方法     * 参数map:存有a,b ,c,d参数的HashMap     */    public List<Map<String, Object>> queryFromRedis(Map<String, String> map) {        StringBuffer sb = new StringBuffer();        String a = map.get("a");        if (a != null && !"".equals(a)){            sb.append(a+",");        }        String b = map.get("b");        if (b != null && !"".equals(b)){            sb.append(b+",");        }        String c = map.get("c");        if (c != null && !"".equals(c)){            sb.append(c+",");        }        String d = map.get("d");        if (d != null && !"".equals(d)){            sb.append(d);        }        String str = sb.toString();        String[] keys = null;        if (str.contains(",")){             keys = str.split(",");        }else {            keys = new String[1];            keys[0] = str;        }        String strs = "";        for (int i=0;i<keys.length;i++){            strs += keys[i];        }        //查询多个参数的id值得交集,得到通过多参数查询的id的Set集合        Set<String> sinter = poolClient.sinter(keys);        List<Map<String, Object>> infos = new ArrayList<>();        if (sinter.size() > 0){            //遍历存有id的Set集合,并查询这些数据            for (String key : sinter){                HashMap<String,Object> info = (HashMap<String,Object>) poolClient.hget("list",key);                infos.add(info);            }        }        return infos;    }}
原创粉丝点击