Search in sources :

Example 1 with StaticMemoryCacheManager

use of com.diboot.core.cache.StaticMemoryCacheManager in project diboot by dibo-software.

the class IamCacheManager method initApiPermissionCache.

/**
 * 初始化
 */
private static synchronized void initApiPermissionCache() {
    StaticMemoryCacheManager cacheManager = getCacheManager();
    if (cacheManager.isUninitializedCache(CACHE_NAME_CONTROLLER_API) == false) {
        return;
    }
    List<ApiPermissionWrapper> permissions = ApiPermissionExtractor.extractAllApiPermissions();
    if (V.notEmpty(permissions)) {
        for (ApiPermissionWrapper wrapper : permissions) {
            if (wrapper.getChildren() != null) {
                for (ApiPermission apiPermission : wrapper.getChildren()) {
                    // 缓存url-permission
                    URL_PERMISSIONCODE_CACHE.put(apiPermission.getApiMethod().toUpperCase() + ":" + apiPermission.getApiUri(), apiPermission.getPermissionCode());
                    // 缓存class-api
                    cacheManager.putCacheObj(CACHE_NAME_CONTROLLER_API, wrapper.getClassName(), wrapper);
                }
            }
        }
    }
}
Also used : StaticMemoryCacheManager(com.diboot.core.cache.StaticMemoryCacheManager) ApiPermission(com.diboot.iam.annotation.process.ApiPermission) ApiPermissionWrapper(com.diboot.iam.annotation.process.ApiPermissionWrapper)

Example 2 with StaticMemoryCacheManager

use of com.diboot.core.cache.StaticMemoryCacheManager in project diboot by dibo-software.

the class BindingCacheManager method initEntityInfoCache.

/**
 * 初始化
 */
private static void initEntityInfoCache() {
    StaticMemoryCacheManager cacheManager = getCacheManager();
    if (cacheManager.isUninitializedCache(CACHE_NAME_CLASS_ENTITY) == false) {
        return;
    }
    // 初始化有service的entity缓存
    Map<String, IService> serviceMap = ContextHelper.getApplicationContext().getBeansOfType(IService.class);
    Set<String> uniqueEntitySet = new HashSet<>();
    if (V.notEmpty(serviceMap)) {
        for (Map.Entry<String, IService> entry : serviceMap.entrySet()) {
            Class entityClass = BeanUtils.getGenericityClass(entry.getValue(), 1);
            if (entityClass != null) {
                IService entityIService = entry.getValue();
                if (uniqueEntitySet.contains(entityClass.getName())) {
                    if (entityIService.getClass().getAnnotation(Primary.class) != null) {
                        EntityInfoCache entityInfoCache = cacheManager.getCacheObj(CACHE_NAME_CLASS_ENTITY, entityClass.getName(), EntityInfoCache.class);
                        if (entityInfoCache != null) {
                            entityInfoCache.setService(entityIService);
                        }
                    } else {
                        log.warn("Entity: {} 存在多个service实现类,可能导致调用实例与预期不一致!", entityClass.getName());
                    }
                } else {
                    EntityInfoCache entityInfoCache = new EntityInfoCache(entityClass, entityIService);
                    cacheManager.putCacheObj(CACHE_NAME_CLASS_ENTITY, entityClass.getName(), entityInfoCache);
                    cacheManager.putCacheObj(CACHE_NAME_TABLE_ENTITY, entityInfoCache.getTableName(), entityInfoCache);
                    cacheManager.putCacheObj(CACHE_NAME_ENTITYNAME_CLASS, entityClass.getSimpleName(), entityClass);
                    uniqueEntitySet.add(entityClass.getName());
                }
            }
        }
    } else {
        log.debug("未获取到任何有效@Service.");
    }
    // 初始化没有service的table-mapper缓存
    SqlSessionFactory sqlSessionFactory = ContextHelper.getBean(SqlSessionFactory.class);
    Collection<Class<?>> mappers = sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers();
    if (V.notEmpty(mappers)) {
        for (Class<?> mapperClass : mappers) {
            Type[] types = mapperClass.getGenericInterfaces();
            try {
                if (types != null && types.length > 0 && types[0] != null) {
                    ParameterizedType genericType = (ParameterizedType) types[0];
                    Type[] superTypes = genericType.getActualTypeArguments();
                    if (superTypes != null && superTypes.length > 0 && superTypes[0] != null) {
                        String entityClassName = superTypes[0].getTypeName();
                        if (!uniqueEntitySet.contains(entityClassName) && entityClassName.length() > 1) {
                            Class<?> entityClass = Class.forName(entityClassName);
                            BaseMapper mapper = (BaseMapper) ContextHelper.getBean(mapperClass);
                            EntityInfoCache entityInfoCache = new EntityInfoCache(entityClass, null);
                            entityInfoCache.setBaseMapper(mapper);
                            cacheManager.putCacheObj(CACHE_NAME_CLASS_ENTITY, entityClass.getName(), entityInfoCache);
                            cacheManager.putCacheObj(CACHE_NAME_TABLE_ENTITY, entityInfoCache.getTableName(), entityInfoCache);
                            cacheManager.putCacheObj(CACHE_NAME_ENTITYNAME_CLASS, entityClass.getSimpleName(), entityClass);
                            uniqueEntitySet.add(entityClass.getName());
                        }
                    }
                }
            } catch (Exception e) {
                log.warn("解析mapper异常", e);
            }
        }
    }
    uniqueEntitySet = null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Primary(org.springframework.context.annotation.Primary) StaticMemoryCacheManager(com.diboot.core.cache.StaticMemoryCacheManager) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) BaseMapper(com.baomidou.mybatisplus.core.mapper.BaseMapper) EntityInfoCache(com.diboot.core.binding.parser.EntityInfoCache) IService(com.baomidou.mybatisplus.extension.service.IService)

Example 3 with StaticMemoryCacheManager

use of com.diboot.core.cache.StaticMemoryCacheManager in project diboot by dibo-software.

the class IamCacheManager method getCacheManager.

private static StaticMemoryCacheManager getCacheManager() {
    if (iamMemoryCacheManager == null) {
        iamMemoryCacheManager = new StaticMemoryCacheManager(CACHE_NAME_CONTROLLER_API);
        URL_PERMISSIONCODE_CACHE.clear();
    }
    return iamMemoryCacheManager;
}
Also used : StaticMemoryCacheManager(com.diboot.core.cache.StaticMemoryCacheManager)

Aggregations

StaticMemoryCacheManager (com.diboot.core.cache.StaticMemoryCacheManager)3 BaseMapper (com.baomidou.mybatisplus.core.mapper.BaseMapper)1 IService (com.baomidou.mybatisplus.extension.service.IService)1 EntityInfoCache (com.diboot.core.binding.parser.EntityInfoCache)1 ApiPermission (com.diboot.iam.annotation.process.ApiPermission)1 ApiPermissionWrapper (com.diboot.iam.annotation.process.ApiPermissionWrapper)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)1 Primary (org.springframework.context.annotation.Primary)1