mongodb 实现数据缓存

mongodb缓存结果

private MongoOperations mongoTemplate;

public Object methodCacheHold(ProceedingJoinPoint joinPoint) throws Throwable {
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();
//        Class<?> returnType = joinPoint.getClass().getMethod(methodName).getReturnType();
        String cacheKey = getCacheKey(targetName, methodName, arguments);  
        CacheEntity cacheEntity = mongoTemplate.findById(cacheKey, CacheEntity.class);
        Object result = null;
        if (cacheEntity == null) {
            result = joinPoint.proceed();
            // if (result != null) {
            cacheEntity = new CacheEntity();
            cacheEntity.setId(cacheKey);//key为类名,方法名和参数组成字符串
            cacheEntity.setEntity(result);
            mongoTemplate.insert(cacheEntity);
            // }
        } else {
            result = cacheEntity.getEntity();
        }
        return result;
    }

    private String getCacheKey(String targetName, String methodName, Object[] arguments) {
        StringBuffer sb = new StringBuffer();
        sb.append(targetName).append(".").append(methodName);
        if ((arguments != null) && (arguments.length != 0)) {
            for (int i = 0; i < arguments.length; i++) {
                if (arguments[i] instanceof Date) {
                    sb.append(".").append(((Date) arguments[i]).getTime());
                } else {
                    sb.append(".").append(arguments[i]);
                }
            }
        }
        return sb.toString();
    }

清楚mongodb缓存结果

private MongoOperations mongoTemplate;
public Object removeCacheHold(ProceedingJoinPoint joinPoint) throws Throwable {
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();
//        Class<?> returnType = joinPoint.getClass().getMethod(methodName).getReturnType();
        String cacheKey = getCacheKey(targetName, methodName, arguments);  
        CacheEntity cacheEntity = new CacheEntity();
        cacheEntity.setId(cacheKey);
        mongoTemplate.remove(cacheEntity);
        Object result = null;
        result = joinPoint.proceed();
        return result;
    }

    private String getCacheKey(String targetName, String methodName, Object[] arguments) {
        StringBuffer sb = new StringBuffer();
        methodName = methodName.replace("CacheRemove", "Cache");//清楚的方法名规则为添加缓存的方法名后加上Remove
        sb.append(targetName).append(".").append(methodName);
        if ((arguments != null) && (arguments.length != 0)) {
            for (int i = 0; i < arguments.length; i++) {
                if (arguments[i] instanceof Date) {
                    sb.append(".").append(((Date) arguments[i]).getTime());
                } else {
                    sb.append(".").append(arguments[i]);
                }
            }
        }
        return sb.toString();
    }
通过添加在方法上的annotation触发执行缓存的保存和更新

相关推荐