Exemplo n.º 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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 根据id获取实体对象
        /// </summary>
        /// <param name="ID">对象id</param>
        /// <returns></returns>
        public static T GetModel <T>(int ID, bool IsUseCache = false) where T : EntityBase, ILongID, new()
        {
            T model = new T();

            model.ID = ID;

            //UpdateCacheEventSource es = new UpdateCacheEventSource();

            //model.Subscribe(es);

            //es.RaiseEvent("GetModel");

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

                string rule = im.GetCacheKeyRule();

                string          key = string.Format(rule, model.GetIdentityName(), model.ID);
                CacheItemPolicy cip = new CacheItemPolicy();
                cip.AbsoluteExpiration = DateTimeOffset.Now.AddHours(6);

                var cache = mc.Get(key);

                if (cache != null)
                {
                    model = cache as T;
                }
                else
                {
                    model = RedisHelper.GetStringKey <T>(key);

                    if (model == null)
                    {
                        model = OQL.FromObject <T>().Select().Where((cmp, m) => cmp.Comparer(m.ID, "=", ID)).END.ToObject();

                        if (model != null)
                        {
                            RedisHelper.SetStringKey <T>(key, model, DateTime.Now.AddDays(7) - DateTime.Now);
                        }
                    }
                    if (model != null)
                    {
                        //更新本地缓存
                        mc.Set(key, model, cip);
                    }
                }
            }
            else
            {
                model = OQL.FromObject <T>().Select().Where((cmp, m) => cmp.Comparer(m.ID, "=", ID)).END.ToObject();
            }

            return(model);
        }
Exemplo n.º 3
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);
        }