seata optimize concurrent processing of the cache layer

k5hmc34c  于 2个月前  发布在  其他
关注(0)|答案(1)|浏览(28)

Why you need it?

Is your feature request related to a problem? Please describe in details
优化缓存层的读写
optimize read and write of the cache layer

How it could be?

A clear and concise description of what you want to happen. You can explain more about input of the feature, and output of it.
    public Configuration proxy(Configuration originalConfiguration) {
        return (Configuration)Enhancer.create(Configuration.class,
            (MethodInterceptor)(proxy, method, args, methodProxy) -> {
                if (method.getName().startsWith(METHOD_PREFIX)
                        && !method.getName().equalsIgnoreCase(METHOD_LATEST_CONFIG)) {
                    String rawDataId = (String)args[0];
                    ObjectWrapper wrapper = CONFIG_CACHE.get(rawDataId);
                    String type = method.getName().substring(METHOD_PREFIX.length());
                    if (!ObjectWrapper.supportType(type)) {
                        type = null;
                    }
                    if (null == wrapper) {
                        Object result = method.invoke(originalConfiguration, args);
                        // The wrapper.data only exists in the cache when it is not null.
                        if (result != null) {
                            wrapper = new ObjectWrapper(result, type);
                            CONFIG_CACHE.put(rawDataId, wrapper);
                        }
                    }
                    return wrapper == null ? null : wrapper.convertData(type);
                }
                return method.invoke(originalConfiguration, args);
            });
    }

可以看到以上代码,如果并发获取值时,值不存在,会同时去配置中心读数据并写入缓存,应该加一个双检锁来处理
As you can see from the above code, when the value is fetched concurrently, if the value does not exist, the data is read from the configuration center and written to the cache at the same time.A double-checked lock should be added to handle this problem

Add any other context or screenshots about the feature request here.

相关问题