예제 #1
0
        /// <summary>
        /// 注意:更新方法的调用要确保model不是缓存得到的,否则缓存数据会覆盖掉数据库数据,另外有事务的问题,如果ado有开事务,更新缓存之后如果事务回滚,缓存数据就有问题。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="ado"></param>
        /// <param name="IsUseCache"></param>
        /// <returns></returns>
        public static bool Update <T>(T model, AdoHelper ado, bool IsUseCache = false) where T : EntityBase, new()
        {
            EntityQuery <T> q     = new EntityQuery <T>(ado);
            int             count = q.Update(model);

            if (RedisHelper.IsUseCache && IsUseCache && (model is ICacheEvent))
            {
                ICacheEvent im = model as ICacheEvent;

                string[] cks  = im.GetCacheKeys();
                string   rule = im.GetCacheKeyRule();

                MemoryCache mc = MemoryCache.Default;

                CacheItemPolicy cip = new CacheItemPolicy();
                cip.AbsoluteExpiration = DateTimeOffset.Now.AddHours(6);

                foreach (var item in cks)
                {
                    string key = string.Format(rule, item, model[item]);

                    mc.Set(key, model, cip);

                    RedisHelper.SetStringKey <T>(key, model, DateTime.Now.AddDays(7) - DateTime.Now);
                }
            }

            return(count > 0);
        }
예제 #2
0
        /// <summary>
        /// 注意:更新方法的调用要确保model不是缓存得到的,否则缓存数据会覆盖掉数据库数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="IsUseCache"></param>
        /// <returns></returns>
        public static bool Update <T>(T model, bool IsUseCache = false) where T : EntityBase, new()
        {
            System.Reflection.PropertyInfo[] ps = model.GetType().GetProperties();
            foreach (System.Reflection.PropertyInfo pi in ps)
            {
                //Name 为属性名称,GetValue 得到属性值(参数this 为对象本身,null)
                string name = pi.Name.ToLower();
                if (name == "TenantId".ToLower())
                {
                    pi.SetValue(model, TenantId, null);
                }
                else if (name == "UpdateId".ToLower())
                {
                    pi.SetValue(model, AdminId, null);
                }
                else if (name == "UpdateUser".ToLower())
                {
                    pi.SetValue(model, AdminName, null);
                }
                else if (name == "UpdateIP".ToLower())
                {
                    pi.SetValue(model, Util.GetLocalIP(), null);
                }
                else if (name == "UpdateTime".ToLower())
                {
                    pi.SetValue(model, DateTime.Now, null);
                }
            }
            EntityQuery <T> q     = new EntityQuery <T>();
            int             count = q.Update(model);

            if (RedisHelper.IsUseCache && IsUseCache && (model is ICacheEvent))
            {
                ICacheEvent im = model as ICacheEvent;

                string[] cks  = im.GetCacheKeys();
                string   rule = im.GetCacheKeyRule();

                MemoryCache mc = MemoryCache.Default;

                CacheItemPolicy cip = new CacheItemPolicy();
                cip.AbsoluteExpiration = DateTimeOffset.Now.AddHours(6);

                foreach (var item in cks)
                {
                    string key = string.Format(rule, item, model[item]);

                    mc.Set(key, model, cip);

                    RedisHelper.SetStringKey <T>(key, model, DateTime.Now.AddDays(7) - DateTime.Now);
                }
            }

            return(count > 0);
        }