コード例 #1
0
        internal override bool AddInternal(object key, ExpirationHint expirationHint, OperationContext operationContext)
        {
            if (_cacheStore == null)
            {
                throw new InvalidOperationException();
            }
            CacheEntry item = (CacheEntry)_cacheStore.Get(key);

            if (item == null)
            {
                return(false);
            }
            if (item.ExpirationHint == null)
            {
                item.ExpirationHint = expirationHint;
            }
            else if (item.ExpirationHint is AggregateExpirationHint)
            {
                ((AggregateExpirationHint)item.ExpirationHint).Add(expirationHint);
            }
            else
            {
                AggregateExpirationHint hint = new AggregateExpirationHint();
                hint.Add(item.ExpirationHint);
                hint.Add(expirationHint);
                item.ExpirationHint = hint;
            }
            _cacheStore.Insert(key, item);
            item.LastModifiedTime = DateTime.Now;
            return(true);
        }
コード例 #2
0
        internal override bool RemoveInternal(object key, ExpirationHint eh)
        {
            if (_cacheStore == null)
            {
                throw new InvalidOperationException();
            }
            CacheEntry item = (CacheEntry)_cacheStore.Get(key);

            if ((item == null) || (item.ExpirationHint == null))
            {
                return(false);
            }
            if (item.ExpirationHint is AggregateExpirationHint)
            {
                AggregateExpirationHint hint           = new AggregateExpirationHint();
                AggregateExpirationHint expirationHint = (AggregateExpirationHint)item.ExpirationHint;
                foreach (ExpirationHint hint3 in expirationHint)
                {
                    if (!hint3.Equals(eh))
                    {
                        hint.Add(hint3);
                    }
                }
                item.ExpirationHint = hint;
            }
            else if (item.ExpirationHint.Equals(eh))
            {
                item.ExpirationHint = null;
            }
            _cacheStore.Insert(key, item);
            item.LastModifiedTime = DateTime.Now;
            return(true);
        }
コード例 #3
0
        public static Dependency GetDependencyObj(ExpirationHint internalHint)
        {
            if (internalHint == null)
            {
                return(null);
            }

            Dependency dependency = new Dependency();

            if (internalHint is AggregateExpirationHint)
            {
                AggregateExpirationHint hint = (AggregateExpirationHint)internalHint;
                for (int i = 0; i < hint.Hints.Count; i++)
                {
                    AddHintToDependency(dependency, hint.Hints[i]);
                }
            }
            else
            {
                AddHintToDependency(dependency, internalHint);
            }

            return(dependency);
        }
コード例 #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <param name="eh"></param>
        /// <returns></returns>
        internal override bool AddInternal(object key, ExpirationHint eh, OperationContext operationContext)
        {
            CacheEntry e = null;

            try
            {
                if (_cacheStore == null)
                {
                    throw new InvalidOperationException();
                }

                e = (CacheEntry)_cacheStore.Get(key);

                if (e == null)
                {
                    return(false);
                }

                //We only allow either idle expiration or Fixed expiration both cannot be set at the same time
                if ((e.ExpirationHint is IdleExpiration && eh is FixedExpiration) ||
                    (e.ExpirationHint is FixedExpiration && eh is IdleExpiration))
                {
                    return(false);
                }

                if (e.ExpirationHint == null)
                {
                    e.ExpirationHint = eh;
                }
                else
                {
                    if (e.ExpirationHint is AggregateExpirationHint)
                    {
                        ((AggregateExpirationHint)e.ExpirationHint).Add(eh);
                    }
                    else
                    {
                        AggregateExpirationHint aeh = AggregateExpirationHint.Create(Context.StorePoolManager);
                        aeh.Add(e.ExpirationHint);
                        aeh.Add(eh);
                        e.ExpirationHint = aeh;
                    }
                }
                e.MarkFree(NCModulesConstants.Global);
                e.MarkInUse(NCModulesConstants.LocalCache);

                //Our store may not be an in memory store

                _cacheStore.Insert(key, e, true);
                e.LastModifiedTime = System.DateTime.Now;

                if (_context.PerfStatsColl != null)
                {
                    if (_evictionPolicy != null)
                    {
                        _context.PerfStatsColl.SetEvictionIndexSize((long)_evictionPolicy.IndexInMemorySize);
                    }

                    if (_context.ExpiryMgr != null)
                    {
                        _context.PerfStatsColl.SetExpirationIndexSize((long)_context.ExpiryMgr.IndexInMemorySize);
                    }
                }
                return(true);
            }
            finally
            {
                if (e != null)
                {
                    e.MarkFree(NCModulesConstants.LocalCache);
                }
            }
        }
コード例 #5
0
ファイル: LocalCache.cs プロジェクト: wangchengqun/NCache
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <param name="eh"></param>
        /// <returns></returns>
        internal override bool RemoveInternal(object key, ExpirationHint eh)
        {
            if (ServerMonitor.MonitorActivity)
            {
                ServerMonitor.LogClientActivity("LocalCache.Remove", "");
            }

            if (_cacheStore == null)
            {
                throw new InvalidOperationException();
            }

            CacheEntry e = (CacheEntry)_cacheStore.Get(key);

            if (e == null || e.ExpirationHint == null)
            {
                return(false);
            }
            else
            {
                if (e.ExpirationHint is AggregateExpirationHint)
                {
                    AggregateExpirationHint AggHint      = new AggregateExpirationHint();
                    AggregateExpirationHint entryExpHint = (AggregateExpirationHint)e.ExpirationHint;

                    foreach (ExpirationHint exp in entryExpHint)
                    {
                        if (!exp.Equals(eh))
                        {
                            AggHint.Add(exp);
                        }
                    }
                    e.ExpirationHint = AggHint;
                }
                else if (e.ExpirationHint.Equals(eh))
                {
                    e.ExpirationHint = null;
                }
            }

            if (_notifyCacheFull)
            {
                _notifyCacheFull = false;
            }
            _cacheStore.Insert(key, e, true);
            e.LastModifiedTime = System.DateTime.Now;

            if (_context.PerfStatsColl != null)
            {
                if (_evictionPolicy != null)
                {
                    _context.PerfStatsColl.SetEvictionIndexSize((long)_evictionPolicy.IndexInMemorySize);
                }

                if (_context.ExpiryMgr != null)
                {
                    _context.PerfStatsColl.SetExpirationIndexSize((long)_context.ExpiryMgr.IndexInMemorySize);
                }
            }

            return(true);
        }
コード例 #6
0
ファイル: LocalCache.cs プロジェクト: wangchengqun/NCache
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <param name="eh"></param>
        /// <returns></returns>
        internal override bool AddInternal(object key, ExpirationHint eh, OperationContext operationContext)
        {
            if (ServerMonitor.MonitorActivity)
            {
                ServerMonitor.LogClientActivity("LocalCache.Add_2", "");
            }

            if (_cacheStore == null)
            {
                throw new InvalidOperationException();
            }

            CacheEntry e = (CacheEntry)_cacheStore.Get(key);

            if (e == null)
            {
                return(false);
            }

            //We only allow either idle expiration or Fixed expiration both cannot be set at the same time
            if ((e.ExpirationHint is IdleExpiration && eh is FixedExpiration) ||
                (e.ExpirationHint is FixedExpiration && eh is IdleExpiration))
            {
                return(false);
            }

            if (e.ExpirationHint == null)
            {
                e.ExpirationHint = eh;
            }
            else
            {
                if (e.ExpirationHint is AggregateExpirationHint)
                {
                    ((AggregateExpirationHint)e.ExpirationHint).Add(eh);
                }
                else
                {
                    AggregateExpirationHint aeh = new AggregateExpirationHint();
                    aeh.Add(e.ExpirationHint);
                    aeh.Add(eh);
                    e.ExpirationHint = aeh;
                }
            }

            _cacheStore.Insert(key, e, true);
            e.LastModifiedTime = System.DateTime.Now;

            if (_context.PerfStatsColl != null)
            {
                if (_evictionPolicy != null)
                {
                    _context.PerfStatsColl.SetEvictionIndexSize((long)_evictionPolicy.IndexInMemorySize);
                }

                if (_context.ExpiryMgr != null)
                {
                    _context.PerfStatsColl.SetExpirationIndexSize((long)_context.ExpiryMgr.IndexInMemorySize);
                }
            }
            return(true);
        }
コード例 #7
0
        public static ExpirationHint GetExpirationHintObj(Alachisoft.NCache.Common.Protobuf.Dependency dependency, bool resyncOnExpiration, string serializationContext)
        {
            AggregateExpirationHint hints = new AggregateExpirationHint();

            if (dependency != null && dependency.keyDep != null && dependency.keyDep.Count > 0)
            {
                for (int i = 0; i < dependency.keyDep.Count; i++)
                {
                    Alachisoft.NCache.Caching.AutoExpiration.KeyDependency keyDep =
                        new Alachisoft.NCache.Caching.AutoExpiration.KeyDependency(dependency.keyDep[i].keys.ToArray(),
                                                                                   new DateTime(dependency.keyDep[i].startAfter));

                    hints.Add(keyDep);
                }
            }

            if (dependency != null && dependency.fileDep != null && dependency.fileDep.Count > 0)
            {
                for (int i = 0; i < dependency.fileDep.Count; i++)
                {
                    Alachisoft.NCache.Caching.AutoExpiration.FileDependency fileDep =
                        new Alachisoft.NCache.Caching.AutoExpiration.FileDependency(dependency.fileDep[i].filePaths.ToArray(),
                                                                                    new DateTime(dependency.fileDep[i].startAfter));

                    hints.Add(fileDep);
                }
            }

            if (dependency != null && dependency.oleDbDep != null && dependency.oleDbDep.Count > 0)
            {
                for (int i = 0; i < dependency.oleDbDep.Count; i++)
                {
                    OleDbCacheDependency oleDb = new OleDbCacheDependency(dependency.oleDbDep[i].connectionString,
                                                                          dependency.oleDbDep[i].dbCacheKey);

                    hints.Add(oleDb);
                }
            }



            if (dependency != null && dependency.sql7Dep != null && dependency.sql7Dep.Count > 0)
            {
                for (int i = 0; i < dependency.sql7Dep.Count; i++)
                {
                    Sql7CacheDependency sql7Dep = new Sql7CacheDependency(dependency.sql7Dep[i].connectionString,
                                                                          dependency.sql7Dep[i].dbCacheKey);

                    hints.Add(sql7Dep);
                }
            }


            if (dependency != null && dependency.xtDep != null && dependency.xtDep.Count > 0)
            {
                try
                {
                    for (int i = 0; i < dependency.xtDep.Count; i++)
                    {
                        IFormatter formater = new BinaryFormatter();
                        byte[]     buffer   = dependency.xtDep[i].data;
                        object     obj      = null;

                        using (MemoryStream stream = new MemoryStream(buffer))
                        {
                            obj = formater.Deserialize(stream);
                        }


                        Alachisoft.NCache.Caching.AutoExpiration.ExtensibleDependency xtDep =
                            new Alachisoft.NCache.Caching.AutoExpiration.ExtensibleDependency(
                                (Runtime.Dependencies.ExtensibleDependency)obj);


                        hints.Add(xtDep);
                    }
                }
                catch (SerializationException ex)
                {
                    throw new OperationFailedException(ex.Message);
                }
            }

            if (dependency != null && dependency.oracleDep != null && dependency.oracleDep.Count > 0)
            {
                for (int i = 0; i < dependency.oracleDep.Count; i++)
                {
                    Hashtable parameters = new Hashtable();
                    for (int pc = 0; pc < dependency.oracleDep[i].param.Count; pc++)
                    {
                        OracleCommandParams commandParam = new OracleCommandParams((Runtime.Dependencies.OracleCmdParamsType)dependency.oracleDep[i].param[pc].cmdParam.dbType,
                                                                                   dependency.oracleDep[i].param[pc].cmdParam.value,
                                                                                   (Runtime.Dependencies.OracleParameterDirection)dependency.oracleDep[i].param[pc].cmdParam.direction);
                        parameters.Add(dependency.oracleDep[i].param[pc].key, commandParam);
                    }

                    OracleCacheDependency oraDep = new OracleCacheDependency(dependency.oracleDep[i].connectionString,
                                                                             dependency.oracleDep[i].query,
                                                                             (CommandType)dependency.oracleDep[i].commandType,
                                                                             parameters);

                    hints.Add(oraDep);
                }
            }



            if (dependency != null && dependency.yukonDep != null && dependency.yukonDep.Count > 0)
            {
                for (int i = 0; i < dependency.yukonDep.Count; i++)
                {
                    Hashtable parameters = new Hashtable();
                    for (int pc = 0; pc < dependency.yukonDep[i].param.Count; pc++)
                    {
                        YukonCommandParam yukonParam   = dependency.yukonDep[i].param[pc].cmdParam;
                        SqlCmdParams      commandParam = new SqlCmdParams((SqlDbType)yukonParam.dbType, yukonParam.value);

                        commandParam.CmpInfo                 = (System.Data.SqlTypes.SqlCompareOptions)yukonParam.cmpOptions;
                        commandParam.Direction               = (ParameterDirection)yukonParam.direction;
                        commandParam.IsNullable              = yukonParam.isNullable;
                        commandParam.LocaleID                = yukonParam.localeId;
                        commandParam.Offset                  = yukonParam.offset;
                        commandParam.Precision               = (byte)yukonParam.precision;
                        commandParam.Scale                   = (byte)yukonParam.scale;
                        commandParam.ParamSize               = yukonParam.size;
                        commandParam.SourceColumn            = yukonParam.sourceColumn;
                        commandParam.SourceColumnNullMapping = yukonParam.sourceColumnNull;
                        commandParam.SqlValue                = yukonParam.sqlValue;
                        commandParam.SrcVersion              = (DataRowVersion)yukonParam.version;
                        commandParam.DbType                  = (SqlDbType)yukonParam.typeId;
                        commandParam.TypeName                = yukonParam.typeName;
                        commandParam.UdtName                 = yukonParam.udtTypeName;

                        if (!yukonParam.nullValueProvided)
                        {
                            string val = yukonParam.value as string;
                            if (val != null)
                            {
                                if (commandParam.DbType == SqlDbType.Binary || commandParam.DbType == SqlDbType.VarBinary || commandParam.DbType == SqlDbType.Image || commandParam.DbType == SqlDbType.Timestamp)
                                {
                                    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                                    commandParam.Value = encoding.GetBytes(val);
                                }
                                else if (commandParam.DbType == SqlDbType.DateTime || commandParam.DbType == SqlDbType.DateTime2 || commandParam.DbType == SqlDbType.Date || commandParam.DbType == SqlDbType.SmallDateTime)
                                {
                                    commandParam.Value = new DateTime(Convert.ToInt64(val));
                                }
                                else if (commandParam.DbType == SqlDbType.Time)
                                {
                                    commandParam.Value = new TimeSpan(Convert.ToInt64(val));
                                }
                                else if (commandParam.DbType == SqlDbType.DateTimeOffset)
                                {
                                    string[] dateOffset = val.Split(new char[] { ',' });
                                    commandParam.Value = new DateTimeOffset(new DateTime(Convert.ToInt64(dateOffset[0])), TimeSpan.FromMinutes(Convert.ToInt32(dateOffset[1])));
                                }
                                else if (commandParam.DbType == SqlDbType.Money || commandParam.DbType == SqlDbType.SmallMoney || commandParam.DbType == SqlDbType.Decimal)
                                {
                                    commandParam.Value = Convert.ToDecimal(val);
                                }
                                else if (commandParam.DbType == SqlDbType.Int)
                                {
                                    commandParam.Value = Convert.ToInt32(val);
                                }
                                else if (commandParam.DbType == SqlDbType.BigInt)
                                {
                                    commandParam.Value = Convert.ToInt64(val);
                                }
                                else if (commandParam.DbType == SqlDbType.SmallInt)
                                {
                                    commandParam.Value = Convert.ToInt16(val);
                                }
                                else if (commandParam.DbType == SqlDbType.Real)
                                {
                                    commandParam.Value = Convert.ToSingle(val);
                                }
                                else if (commandParam.DbType == SqlDbType.UniqueIdentifier)
                                {
                                    commandParam.Value = System.Data.SqlTypes.SqlGuid.Parse(val);
                                }
                                else if (commandParam.DbType == SqlDbType.TinyInt)
                                {
                                    commandParam.Value = Convert.ToByte(val);
                                }
                                else if (commandParam.DbType == SqlDbType.Float)
                                {
                                    commandParam.Value = Convert.ToDouble(val);
                                }
                                else if (commandParam.DbType == SqlDbType.Bit)
                                {
                                    commandParam.Value = Convert.ToBoolean(val);
                                }
                                else
                                {
                                    commandParam.Value = val;
                                }
                            }
                            else
                            {
                                commandParam.Value = DBNull.Value;
                            }
                        }
                        else
                        {
                            commandParam.Value = DBNull.Value;
                        }

                        parameters.Add(dependency.yukonDep[i].param[pc].key, commandParam);
                    }

                    SqlYukonCacheDependency yukonDep = new SqlYukonCacheDependency(dependency.yukonDep[i].connectionString,
                                                                                   dependency.yukonDep[i].query,
                                                                                   (CommandType)dependency.yukonDep[i].commandType,
                                                                                   parameters);

                    hints.Add(yukonDep);
                }
            }
            if (dependency != null && dependency.NosDep != null && dependency.NosDep.Count > 0)
            {
                for (int i = 0; i < dependency.NosDep.Count; i++)
                {
                    Hashtable parameters = new Hashtable();
                    for (int pc = 0; pc < dependency.NosDep[i].param.Count; pc++)
                    {
                        parameters.Add(dependency.NosDep[i].param[pc].key, dependency.NosDep[i].param[pc].value);
                    }
                    NosDBCacheDependency oraDep = new NosDBCacheDependency(dependency.NosDep[i].connectionString,
                                                                           dependency.NosDep[i].query,
                                                                           dependency.NosDep[i].timeout,
                                                                           parameters);

                    hints.Add(oraDep);
                }
            }


            if (resyncOnExpiration)
            {
                hints.SetBit(ExpirationHint.NEEDS_RESYNC);
            }

            IList <ExpirationHint> expHints = hints.Hints;

            if (expHints.Count == 0)
            {
                return(null);
            }

            if (expHints.Count == 1)
            {
                return(expHints[0]);
            }

            return(hints);
        }
コード例 #8
0
        public static ExpirationHint GetExpirationHintObj(Alachisoft.NCache.Config.Dom.ExpirationPolicy policy, Alachisoft.NCache.Common.Protobuf.Dependency dependency, long absoluteExpiration, long slidingExpiration, bool resyncOnExpiration, string serializationContext)
        {
            ConvHelper.ValidateExpiration(policy);
            ExpirationHint hint  = null;
            const long     TICKS = 10000000;

            if ((absoluteExpiration == 1 || absoluteExpiration == 2) || (slidingExpiration == 1 || slidingExpiration == 2))
            {
                if (policy.SlidingExpiration.LongerEnabled || policy.SlidingExpiration.DefaultEnabled || policy.AbsoluteExpiration.LongerEnabled || policy.AbsoluteExpiration.DefaultEnabled)
                {
                    #region In case user provides sliding expiration as an enum

                    if (absoluteExpiration == 0)
                    {
                        if (policy.SlidingExpiration.LongerEnabled && slidingExpiration == 2)
                        {
                            hint = new IdleExpiration(new TimeSpan(policy.SlidingExpiration.Longer * TICKS));
                        }
                        else if (policy.SlidingExpiration.DefaultEnabled && slidingExpiration == 1)
                        {
                            hint = new IdleExpiration(new TimeSpan(policy.SlidingExpiration.Default * TICKS));
                        }
                    }
                    #endregion

                    #region In case user provides absolute expiration as an enum

                    if (slidingExpiration == 0)
                    {
                        if (policy.AbsoluteExpiration.LongerEnabled && absoluteExpiration == 2)// If not enabled try to check if Longer Expiration is enabled
                        {
                            hint = new FixedExpiration(DateTime.Now.AddSeconds(policy.AbsoluteExpiration.Longer).ToUniversalTime());
                        }
                        else if (absoluteExpiration == 1 && policy.AbsoluteExpiration.DefaultEnabled)// check if Longer is enabled in config
                        {
                            hint = new FixedExpiration(DateTime.Now.AddSeconds(policy.AbsoluteExpiration.Default).ToUniversalTime());
                        }
                    }
                    #endregion
                }
                else
                {
                    absoluteExpiration = 0;// If user provides custom expiration but it is NOT enabled then item will stay in cache, forever.
                }
            }

            //We expect Web.Cache to send in UTC DateTime, AbsoluteExpiration is dealt in UTC
            if (absoluteExpiration != 0 && absoluteExpiration != 1 && absoluteExpiration != 2 && absoluteExpiration != DateTime.MaxValue.ToUniversalTime().Ticks)
            {
                hint = new FixedExpiration(new DateTime(absoluteExpiration, DateTimeKind.Utc));
            }
            if (slidingExpiration != 0 && slidingExpiration != 1 && slidingExpiration != 2)
            {
                hint = new IdleExpiration(new TimeSpan(slidingExpiration));
            }
            ExpirationHint depHint = GetExpirationHintObj(dependency, false, serializationContext);

            if (depHint != null)
            {
                if (hint != null)
                {
                    if (depHint is AggregateExpirationHint)
                    {
                        ((AggregateExpirationHint)depHint).Add(hint);
                        hint = depHint;
                    }
                    else
                    {
                        hint = new AggregateExpirationHint(hint, depHint);
                    }
                }
                else
                {
                    hint = depHint;
                }
            }


            if (hint != null && resyncOnExpiration)
            {
                hint.SetBit(ExpirationHint.NEEDS_RESYNC);
            }

            return(hint);
        }