Example #1
0
        /// <summary>把该对象持久化到数据库,添加/更新实体缓存和单对象缓存,增加总计数</summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public virtual Int32 Insert(IEntity entity)
        {
            var rs = persistence.Insert(entity);

            // 标记来自数据库
            var e = entity as TEntity;

            e.MarkDb(true);

            // 加入实体缓存
            var ec = _cache;

            if (ec != null)
            {
                ec.Add(e);
            }

            // 加入单对象缓存
            _singleCache?.Add(e);

            // 增加计数
            if (_Count >= 0)
            {
                Interlocked.Increment(ref _Count);
            }

            // 事务回滚时执行逆向操作
            var tr = GetTran();

            if (tr != null)
            {
                tr.Completed += (s, se) =>
                {
                    if (!se.Success && se.Executes > 0)
                    {
                        if (ec != null)
                        {
                            ec.Remove(e);
                        }
                        _singleCache?.Remove(e);
                        if (_Count >= 0)
                        {
                            Interlocked.Decrement(ref _Count);
                        }
                    }
                }
            }
            ;

            return(rs);
        }
Example #2
0
        /// <summary>更新数据库,同时更新实体缓存</summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public virtual Int32 Update(IEntity entity)
        {
            var rs = Factory.Persistence.Update(this, entity);

            var e = entity as TEntity;

            // 更新缓存
            _cache?.Update(e);

            // 干掉缓存项,让它重新获取
            _singleCache?.Remove(e);

            return(rs);
        }
Example #3
0
        /// <summary>从数据库中删除该对象,同时从实体缓存和单对象缓存中删除,扣减总数量</summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public virtual Int32 Delete(IEntity entity)
        {
            var rs = persistence.Delete(entity);

            // 如果当前在事务中,并使用了缓存,则尝试更新缓存
            if (HoldCache || UsingTrans)
            {
                if (_cache != null)
                {
                    var fi = Operate.Unique;
                    if (fi != null)
                    {
                        var v = entity[fi.Name];
                        Cache.Entities.RemoveAll(e => Object.Equals(e[fi.Name], v));
                    }
                }
                // 自动加入单对象缓存
                if (_singleCache != null)
                {
                    _singleCache.Remove(entity as TEntity, false);
                }
            }

            if (_Count >= 0)
            {
                Interlocked.Decrement(ref _Count);
            }

            return(rs);
        }
Example #4
0
        /// <summary>更新数据库,同时更新实体缓存</summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public virtual Int32 Update(IEntity entity)
        {
            var rs = Factory.Persistence.Update(entity);

            var e = entity as TEntity;

            // 更新缓存
            TEntity old = null;
            var     ec  = _cache;

            if (ec != null)
            {
                old = ec.Update(e);
            }

            // 干掉缓存项,让它重新获取
            _singleCache?.Remove(e);

            return(rs);
        }