use of com.mendmix.mybatis.plugin.cache.annotation.Cache in project jeesuite-libs by vakinge.
the class CacheHandler method generateQueryMethodCacheByMethod.
/**
* 按查询方法生成缓存key前缀
* @param entityClassName
* @param method
* @return
*/
private QueryCacheMethodMetadata generateQueryMethodCacheByMethod(MapperMetadata mapperMeta, MapperMethod mapperMethod) {
Method method = mapperMethod.getMethod();
Cache cacheAnnotation = method.getAnnotation(Cache.class);
String[] evictOnMethods = cacheAnnotation.evictOnMethods();
Class<?> mapperClass = mapperMeta.getMapperClass();
Class<?> entityClass = mapperMeta.getEntityClass();
QueryCacheMethodMetadata methodCache = new QueryCacheMethodMetadata();
methodCache.methodName = mapperClass.getName() + InvocationVals.DOT + method.getName();
methodCache.concurrency = cacheAnnotation.concurrency();
methodCache.uniqueIndex = cacheAnnotation.uniqueIndex();
methodCache.cacheGroupKey = entityClass.getSimpleName() + GROUPKEY_SUFFIX;
if (cacheAnnotation.userScope()) {
methodCache.contextParam = CURRENT_USER_CONTEXT_NAME;
} else if (cacheAnnotation.scopeContext().length > 0) {
methodCache.contextParam = cacheAnnotation.scopeContext()[0];
}
if (cacheAnnotation.refKey().length > 0) {
methodCache.refKey = cacheAnnotation.refKey()[0];
}
if (methodCache.contextParam != null && evictOnMethods.length == 0) {
evictOnMethods = new String[] { "*" };
}
methodCache.checkExpired = evictOnMethods.length > 0;
if (cacheAnnotation.expire() > 0) {
methodCache.expire = cacheAnnotation.expire();
} else if (cacheAnnotation.userScope()) {
methodCache.expire = IN_1MINS * 10 < defaultCacheExpire ? IN_1MINS * 10 : defaultCacheExpire;
}
if (methodCache.uniqueIndex && method.getReturnType() != entityClass) {
throw new MybatisHanlerInitException("@Cache with[uniqueIndex = true] but ReturnType not Match [" + entityClass.getName() + "]");
}
methodCache.collectionResult = method.getReturnType() == List.class || method.getReturnType() == Set.class;
if (methodCache.collectionResult) {
methodCache.groupRalated = true;
} else {
// count等统计查询
methodCache.groupRalated = method.getReturnType().isAnnotationPresent(Table.class) == false;
}
methodCache.fieldNames = new String[method.getParameterTypes().length];
Annotation[][] annotations = method.getParameterAnnotations();
boolean uniqueQuery = method.getReturnType().isAnnotationPresent(Table.class);
for (int i = 0; i < annotations.length; i++) {
Annotation[] aa = annotations[i];
if (aa.length > 0) {
String fieldName = null;
inner: for (Annotation annotation : aa) {
if (annotation.toString().contains(Param.class.getName())) {
fieldName = ((Param) annotation).value();
break inner;
}
}
if (uniqueQuery && mapperMeta.getEntityMetadata().getProp2ColumnMappings().containsKey(fieldName)) {
methodCache.fieldNames[i] = fieldName;
}
}
//
}
methodCache.keyPattern = new StringBuilder(entityClass.getSimpleName()).append(InvocationVals.DOT).append(method.getName()).append(":%s").toString();
if (uniqueQuery) {
for (String name : methodCache.fieldNames) {
if (StringUtils.isBlank(name)) {
methodCache.fieldNames = null;
break;
}
}
}
//
buildEvictOnMethods(mapperClass.getName(), mapperMethod, evictOnMethods);
return methodCache;
}
Aggregations