Example #1
0
        /// <summary>
        /// 更新数据到缓存(Add)
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="dbContext"></param>
        /// <param name="entity"></param>
        internal void AddCache <TEntity>(TEntity entity)
        {
            var tableName = TableAttribute.GetName(typeof(TEntity));

            //如果存在表级别缓存,则更新数据到缓存
            if (CacheStorageManager.IsExist(GetTableCacheKey(tableName), out List <TEntity> entities))
            {
                if (TableCachingAttribute.IsExistTaleCaching(typeof(TEntity), out TimeSpan tableCacheTimeSpan))
                {
                    entities.Add(entity);
                    //如果过期时间为0,则取上下文的过期时间
                    CacheStorageManager.Put(GetTableCacheKey(tableName), entities, tableCacheTimeSpan == TimeSpan.Zero ? CacheOptions.TableCacheExpiredTimeSpan : tableCacheTimeSpan);
                }
            }
        }
Example #2
0
        /// <summary>
        /// 更新数据到缓存(Add)
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="dbContext"></param>
        /// <param name="values"></param>
        internal static void AddCache <TEntity>(DbContext dbContext, IEnumerable <TEntity> values)
        {
            if (dbContext.OpenTableCache)
            {
                //如果存在表级别缓存,则更新数据到缓存
                if (CacheStorageManager.IsExist(dbContext, GetTableCacheKey(dbContext), out List <TEntity> entities))
                {
                    if (TableCachingAttribute.IsExistTaleCaching(typeof(TEntity), out TimeSpan tableCacheTimeSpan))
                    {
                        //如果过期时间为0,则取上下文的过期时间
                        TimeSpan timeSpan = tableCacheTimeSpan == TimeSpan.Zero ? dbContext.TableCacheExpiredTimeSpan : tableCacheTimeSpan;

                        entities.AddRange(values);
                        CacheStorageManager.Put(dbContext, GetTableCacheKey(dbContext), entities, tableCacheTimeSpan);
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// 从缓存中获取数据,如果没有,则后台执行扫描表任务
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="dbContext"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        internal List <TEntity> GetEntitiesFromCache <TEntity>(Expression <Func <TEntity, bool> > filter) where TEntity : class
        {
            Ensure.ArgumentNotNullOrEmpty(filter, nameof(filter));

            //1.如果TableCache里面有该缓存键,则直接获取
            if (CacheStorageManager.IsExist(GetTableCacheKey(TableAttribute.GetName(typeof(TEntity))), out List <TEntity> entities))
            {
                DbContext.IsFromCache = true;
                return(entities.Where(filter.Compile()).ToList());
            }

            //2.则判断是否需要对该表进行扫描(含有TableCachingAttribute的标记的类才可以有扫描全表的权限)
            if (TableCachingAttribute.IsExistTaleCaching(typeof(TEntity), out TimeSpan tableCacheTimeSpan))
            {
                //执行扫描全表数据任务
                ScanTableBackground <TEntity>(tableCacheTimeSpan);
            }

            return(null);
        }
Example #4
0
        /// <summary>
        /// 更新数据到缓存(Delete)
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="dbContext"></param>
        /// <param name="entity"></param>
        internal void DeleteCache <TEntity>(TEntity entity)
        {
            var tableName = TableAttribute.GetName(typeof(TEntity));

            //如果存在表级别缓存,则更新数据到缓存
            if (CacheStorageManager.IsExist(GetTableCacheKey(tableName), out List <TEntity> entities))
            {
                if (TableCachingAttribute.IsExistTaleCaching(typeof(TEntity), out TimeSpan tableCacheTimeSpan))
                {
                    //如果过期时间为0,则取上下文的过期时间
                    TimeSpan timeSpan = tableCacheTimeSpan == TimeSpan.Zero ? CacheOptions.TableCacheExpiredTimeSpan : tableCacheTimeSpan;
                    //从缓存集合中寻找该记录,如果找到,则更新该记录
                    var val = entities.Find(t => t.Equals(entity));
                    if (val != null)
                    {
                        entities.Remove(val);
                        CacheStorageManager.Put(GetTableCacheKey(tableName), entities, tableCacheTimeSpan);
                    }
                }
            }
        }
Example #5
0
 /// <summary>
 /// 更新数据到缓存(Delete)
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="dbContext"></param>
 /// <param name="filter"></param>
 internal void DeleteCache <TEntity>(Expression <Func <TEntity, bool> > filter)
 {
     if (DbContext.OpenTableCache)
     {
         var tableName = TableAttribute.GetName(typeof(TEntity));
         //如果存在表级别缓存,则更新数据到缓存
         if (CacheStorageManager.IsExist(GetTableCacheKey(tableName), out List <TEntity> entities))
         {
             if (TableCachingAttribute.IsExistTaleCaching(typeof(TEntity), out TimeSpan tableCacheTimeSpan))
             {
                 //从缓存集合中寻找该记录,如果找到,则更新该记录
                 var list = entities.Where(filter.Compile()).ToList();
                 if (list != null && list.Any())
                 {
                     entities.RemoveAll(t => list.Contains(t));
                     //如果过期时间为0,则取上下文的过期时间
                     CacheStorageManager.Put(GetTableCacheKey(tableName), entities, tableCacheTimeSpan == TimeSpan.Zero ? DbContext.TableCacheExpiredTimeSpan : tableCacheTimeSpan);
                 }
             }
         }
     }
 }
Example #6
0
 /// <summary>
 /// 更新数据到缓存(Delete)
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="dbContext"></param>
 /// <param name="filter"></param>
 internal static void DeleteCache <TEntity>(DbContext dbContext, Expression <Func <TEntity, bool> > filter)
 {
     if (dbContext.OpenTableCache)
     {
         //如果存在表级别缓存,则更新数据到缓存
         if (CacheStorageManager.IsExist(dbContext, GetTableCacheKey(dbContext), out List <TEntity> entities))
         {
             if (TableCachingAttribute.IsExistTaleCaching(typeof(TEntity), out TimeSpan tableCacheTimeSpan))
             {
                 //如果过期时间为0,则取上下文的过期时间
                 TimeSpan timeSpan = tableCacheTimeSpan == TimeSpan.Zero ? dbContext.TableCacheExpiredTimeSpan : tableCacheTimeSpan;
                 //从缓存集合中寻找该记录,如果找到,则更新该记录
                 var val = entities.Where(filter.Compile()).FirstOrDefault();
                 if (val != null)
                 {
                     entities.Remove(val);
                     CacheStorageManager.Put(dbContext, GetTableCacheKey(dbContext), entities, tableCacheTimeSpan);
                 }
             }
         }
     }
 }
Example #7
0
        /// <summary>
        /// 更新数据到缓存(Update)
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="dbContext"></param>
        /// <param name="entity"></param>
        /// <param name="filter"></param>
        internal void UpdateCache <TEntity>(TEntity entity, Expression <Func <TEntity, bool> > filter, IEnumerable <string> updateCloumns = null)
        {
            if (DbContext.OpenTableCache)
            {
                var tableName = TableAttribute.GetName(typeof(TEntity));
                //如果存在表级别缓存,则更新数据到缓存
                if (CacheStorageManager.IsExist(GetTableCacheKey(tableName), out List <TEntity> oldEntities))
                {
                    if (TableCachingAttribute.IsExistTaleCaching(typeof(TEntity), out TimeSpan tableCacheTimeSpan))
                    {
                        //如果过期时间为0,则取上下文的过期时间
                        TimeSpan timeSpan = tableCacheTimeSpan == TimeSpan.Zero ? DbContext.TableCacheExpiredTimeSpan : tableCacheTimeSpan;
                        //从缓存集合中寻找该记录,如果找到,则更新该记录
                        var list = oldEntities.Where(filter.Compile()).ToList();
                        if (list != null && list.Any())
                        {
                            List <TEntity> newEntities;

                            oldEntities.RemoveAll(t => list.Contains(t));

                            if (null == updateCloumns)
                            {
                                //改变了多条,更新对应字段
                                newEntities = UpdateEntitiesField(oldEntities, entity, updateCloumns);
                            }
                            else
                            {
                                //只改变了传入的唯一一条
                                newEntities = oldEntities;
                                newEntities.Add(entity);
                            }

                            CacheStorageManager.Put(GetTableCacheKey(tableName), newEntities, timeSpan);
                        }
                    }
                }
            }
        }
 /// <summary>
 /// 更新数据到缓存(Update)
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="dbContext"></param>
 /// <param name="entity"></param>
 /// <param name="filter"></param>
 internal void UpdateCache <TEntity>(TEntity entity, Expression <Func <TEntity, bool> > filter)
 {
     if (DbContext.OpenTableCache)
     {
         var tableName = TableAttribute.GetName(typeof(TEntity));
         //如果存在表级别缓存,则更新数据到缓存
         if (CacheStorageManager.IsExist(GetTableCacheKey(tableName), out List <TEntity> entities))
         {
             if (TableCachingAttribute.IsExistTaleCaching(typeof(TEntity), out TimeSpan tableCacheTimeSpan))
             {
                 //如果过期时间为0,则取上下文的过期时间
                 TimeSpan timeSpan = tableCacheTimeSpan == TimeSpan.Zero ? DbContext.TableCacheExpiredTimeSpan : tableCacheTimeSpan;
                 //从缓存集合中寻找该记录,如果找到,则更新该记录
                 var val = entities.Where(filter.Compile()).FirstOrDefault();
                 if (val != null)
                 {
                     val = entity;
                     CacheStorageManager.Put(GetTableCacheKey(tableName), entities, timeSpan);
                 }
             }
         }
     }
 }
Example #9
0
        /// <summary>
        /// 从缓存中获取数据,如果没有,则后台执行扫描表任务
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="dbContext"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        internal static List <TEntity> GetEntitiesFromCache <TEntity>(DbContext dbContext, Expression <Func <TEntity, bool> > filter) where TEntity : class
        {
            //1.检查是否开启了Table缓存
            if (!dbContext.OpenTableCache)
            {
                return(null);
            }

            //2.如果TableCache里面有该缓存键,则直接获取
            if (CacheStorageManager.IsExist(dbContext, GetTableCacheKey(dbContext), out List <TEntity> entities))
            {
                dbContext.IsFromCache = true;
                return(entities.Where(filter.Compile()).ToList());
            }

            //3.则判断是否需要对该表进行扫描(含有TableCachingAttribute的标记的类才可以有扫描全表的权限)
            if (TableCachingAttribute.IsExistTaleCaching(typeof(TEntity), out TimeSpan tableCacheTimeSpan))
            {
                //执行扫描全表数据任务
                ScanTableBackground <TEntity>(dbContext, tableCacheTimeSpan);
            }

            return(null);
        }