redis批量先查缓存再查数据库

 

 /**
     *  批量查询缓存,若是缓存没有的数据再调用对应的方法查询数据,查询之后放入缓存
     * @param prefix 缓存前缀
     * @param params 缓存参数
     * @param column 缓存参数对应字段列名
     * @param dataBaseFunction 数据库查询方法
     * @return
     * @param  查询参数
     * @param  返回类型
     */
    public   List batchGetCacheData(String prefix, Collection params, String column,
                                            Function,List> dataBaseFunction, Class clazz){
        // TODO 未大规模测试 谨慎使用
        //       先查redis
        Set keys = params.stream().map(m -> {return prefix + m;}).collect(Collectors.toSet());
        List cacheResultList = new ArrayList<>();
        List> splitList = ListUtil.split((Collection) keys, 500);
        for (List ts : splitList) {
            cacheResultList.addAll(redisTemplate.executePipelined(new RedisCallback() {
                @Override
                public String doInRedis(RedisConnection connection) throws DataAccessException {
                    for (T key : ts) {
                        connection.get(key.toString().getBytes());
                    }
                    return null;
                }
            }));
        }
        //过滤出没有获取到缓存的数据,去执行对应数据库查询
        Tuple2, List> tuple2 = analysisCacheVal(params, cacheResultList, clazz);
        Set queryKeys = tuple2.getFirst();
        List result = tuple2.getSecond();
        if(!CollectionUtils.isEmpty(result) && result.size() == params.size()){
            return result;
        }
        List dataBaseResult = getDataBaseFunction(prefix, queryKeys, dataBaseFunction, column);
        result.addAll(dataBaseResult);
        return result;
    }

    private static  Tuple2, List> analysisCacheVal (Collection params, List cacheResultList, Class clazz) {
        Set queryKeys = new HashSet<>();
        List result = new ArrayList<>();
        T[] paramsObj = (T[]) params.toArray();
        for (int i = 0; i< params.size(); i++) {
            String cacheR = cacheResultList.get(i);
            if (Objects.isNull(cacheR)) {
                queryKeys.add(paramsObj[i]);
            } else {
                result.add(JSON.parseObject(cacheR, clazz));
            }
        }
        return new Tuple2<>(queryKeys, result);
    }

    private  List getDataBaseFunction (String prefix, Set queryKeys, Function,List> dataBaseFunction, String column) {
        List dataBaseResult = dataBaseFunction.apply(queryKeys);
        if(!CollectionUtils.isEmpty(dataBaseResult)){
            for (R r : dataBaseResult) {
                Object obj = getProperty(r, column);
                if (Objects.nonNull(obj)) {
                    redisTemplate.opsForValue().set(prefix + obj.toString(), JSON.toJSONString(r), 1, TimeUnit.MINUTES);
                }
            }
        }
        return dataBaseResult;
    }


    public static  Object getProperty(R object, String column) {
        Class clazz = object.getClass();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            if (field.getName().equals(column)) {
                try {
                    field.setAccessible(true);
                    return field.get(object);
                } catch (IllegalAccessException e) {
                    log.error("RedisUtil getProperty error {}", e);
                    return null;
                }
            }
        }
        return null;
    }

使用方式

redisUtil.batchGetCacheData("country:", ids, "id", v -> baseMapper.getByIds(ids), 查询对象.class);

public class ListUtil {
    public static final int DEFAULT_LIMIT = 500;
    public static final int LIMIT1000 = 1000;

    public static  List> split(Collection collection) {
        return split(collection, DEFAULT_LIMIT);
    }

    public static  List> split(Collection collection, int size) {
        if (collection == null || collection.isEmpty()) {
            return Collections.emptyList();
        }
        List list = new ArrayList<>(collection);
        final int listSize = list.size();
        final List> result = new ArrayList<>(listSize / size + 1);
        int offset = 0;
        for (int toIdx = size; toIdx <= listSize; offset = toIdx, toIdx += size) {
            result.add(list.subList(offset, toIdx));
        }
        if (offset < listSize) {
            result.add(list.subList(offset, listSize));
        }
        return result;
    }

}

你可能感兴趣的:(缓存,数据库,redis,java)