Exemplo n.º 1
0
        /// <summary>
        /// Get Json JObject From Json File
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static JObject Json_GetJObjectFromJsonFile(string filePath)
        {
            JObject jObject = new JObject();

            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("the file not found in the filePath");
            }
            string key = filePath;

            if (key.Contains("/") || key.Contains("\\"))
            {
                key.Replace("\\", "/");
                string[] array = key.Split('/');
                key = array[array.Length - 1];
            }
            object jobjectInCache = HttpRuntimeCache_Helper_DG.Cache_Get(key);

            if (jobjectInCache != null)
            {
                jObject = (JObject)jobjectInCache;
            }
            else
            {
                jObject = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(ReadFileStringContent(filePath));
                HttpRuntimeCache_Helper_DG.Cache_Add(key, jObject);
            }
            return(jObject);
        }
Exemplo n.º 2
0
 /// <summary>
 /// edit data cache must update
 /// </summary>
 public static void CacheChanges <T>()
 {
     if (QX_Frame_Helper_DG_Config.Cache_IsCache)
     {
         HttpRuntimeCache_Helper_DG.Cache_Delete($"{typeof(Db).Name}_{typeof(T).Name}");
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// query cache
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static IQueryable <T> GetIQuerybleByCache <T>() where T : class
 {
     if (QX_Frame_Helper_DG_Config.Cache_IsCache)
     {
         IQueryable <T> iqueryable = HttpRuntimeCache_Helper_DG.Cache_Get($"{typeof(Db).Name}_{typeof(T).Name}") as IQueryable <T>;
         if (iqueryable == null)
         {
             iqueryable = GetCurrentDbContext().Set <T>().AsExpandable();
             HttpRuntimeCache_Helper_DG.Cache_Add($"{typeof(Db).Name}_{typeof(T).Name}", iqueryable, QX_Frame_Helper_DG_Config.Cache_CacheExpirationTimeSpan_Minutes);
         }
         return(iqueryable);
     }
     else
     {
         return(GetCurrentDbContext().Set <T>().AsExpandable());
     }
 }
Exemplo n.º 4
0
        /*the singleton Db */
        //private volatile static Db db = null;   //volatile find Db in memory not in cache

        #region The Singleton to new DBEntity_DG

        //private static readonly object lockHelper = new object();
        //static EF_Helper_DG()
        //{
        //    if (db == null)
        //    {
        //        lock (lockHelper)
        //        {
        //            if (db == null)
        //                db = System.Activator.CreateInstance<Db>();
        //        }
        //    }

        //    //close the Validate of EF OnSaveEnabled
        //    db.Configuration.ValidateOnSaveEnabled = false;
        //}

        #endregion

        #region get current dbContext
        public static DbContext GetCurrentDbContext()
        {
            //method 1 : CallContext 该方法有有时候第一次访问不到的bug
            //CallContext:是线程内部唯一的独用的数据槽(一块内存空间)
            //Db dbContext = CallContext.GetData("DbContext") as Db;
            //if (dbContext == null)  //线程在内存中没有此上下文
            //{
            //    //create a dbContext to memory if dbContext has not exist
            //    dbContext = System.Activator.CreateInstance<Db>();
            //    CallContext.SetData("DbContext", dbContext);
            //}

            //method 2 :
            Db dbContext = HttpRuntimeCache_Helper_DG.Cache_Get("dbContext") as Db;

            if (dbContext == null)
            {
                //create a dbContext to memory if dbContext has not exist
                dbContext = System.Activator.CreateInstance <Db>();
                HttpRuntimeCache_Helper_DG.Cache_Add("dbContext", dbContext);
            }
            return(dbContext);
        }