FreeSql icon indicating copy to clipboard operation
FreeSql copied to clipboard

添加全局缓存工厂

Open hyzx86 opened this issue 1 month ago • 0 comments

为全局static 缓存对象 增加 缓存工厂,

由于缓存太多,可能某些需要缓存的对象未包含在内,目前已处理仓库中所有静态 ConcurrentDictionary 集合 如果有遗漏的其他类型需要缓存可以直接将初始化方式改为如下方式

    internal static ConcurrentDictionary<Guid, CustomAdapter> _dicCustomAdater = Utils.GlobalCacheFactory.CreateCacheItem<ConcurrentDictionary<Guid, CustomAdapter>>();

// 或
    internal static ConcurrentDictionary<Guid, CustomAdapter> _dicCustomAdater = Utils.GlobalCacheFactory.CreateCacheItem(new ConcurrentDictionary<Guid, CustomAdapter>);


为 Utils 类 增加抽象属性 GlobalCacheFactory

    public interface IGlobalCacheFactory
    {
        T CreateCacheItem<T>(T defaultValue = null) where T : class, new();
        T CreateCacheItem<T>() where T : new();
    }

默认实现 DefaultCacheFactory , 逻辑上保留默认初始化方式

    public class DefaultCacheFactory : IGlobalCacheFactory
    {
        public T CreateCacheItem<T>(T defaultValue = null) where T : class, new()
        {
            return defaultValue ?? new T();
        }

        public T CreateCacheItem<T>() where T : new()
        {
            return new T();
        }
    }

hyzx86 avatar Jun 08 '24 12:06 hyzx86