Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CacheDependency"/> class that monitors an array of file paths (to files or
        /// directories), an array of cache keys, or both for changes. It also
        /// makes itself dependent upon another instance of the <see cref="CacheDependency"/>
        /// class and a time when the change monitoring begins.
        /// </summary>
        /// <param name="fileNames">An array of file paths (to files or directories) that the cached object
        /// is dependent upon. When any of these resources change, the cached object becomes obsolete and
        /// is removed from the cache.</param>
        /// <param name="cacheKeys">An array of cache keys that the new object monitors for changes. When
        /// any of these cache keys change, the cached object associated with this dependency object
        /// becomes obsolete and is removed from the cache.</param>
        /// <param name="dependency">Another instance of the <see cref="CacheDependency"/> class that this
        /// instance is dependent upon.</param>
        /// <param name="start">The time when change tracking begins.</param>
        /// <exception cref="ArgumentNullException"><paramref name="fileNames"/> or <paramref name="cacheKeys"/> contains a
        /// null reference (Nothing in Visual Basic).</exception>
        /// <remarks>
        /// If any of the files or directories in the array were to change or be removed from the array,
        /// the cached item becomes obsolete and is removed from the application's <see cref="Cache"/> object.
        /// <para>
        /// Also, if any of the directories or files specified in the <paramref name="fileNames"/> parameter is not found in the
        /// file system, they are treated as missing files. If any of them are created after the object with the
        /// dependency is added to the <see cref="Cache"/>, the cached object will be removed from the <see cref="Cache"/>.For example,
        /// assume that you add an object to the <see cref="Cache"/> with a dependency on the following file path:
        /// c:\stocks\xyz.dat. If that file is not found when the <see cref="CacheDependency"/> object is created, but is
        /// created later, the cached object is removed upon the creation of the xyz.dat file.
        /// </para>
        /// </remarks>


        public CacheDependency(string[] fileNames, string[] cacheKeys, CacheDependency dependency, DateTime start)//:base(start)
        {
            CacheDependency fileDependency = null;
            CacheDependency keyDependency  = null;

            if (fileNames != null)
            {
                if (fileNames.Length == 0)
                {
                    throw new ArgumentException("fileNames array must have atleast one file name");
                }
                foreach (string fileName in fileNames)
                {
                    if (fileName == null)
                    {
                        throw new ArgumentNullException("fileName");
                    }
                    if (fileName == string.Empty)
                    {
                        throw new ArgumentException("fileName cannot be empty string");
                    }
                }

                fileDependency = new FileDependency(fileNames, start);
            }

            if (cacheKeys != null)
            {
                if (cacheKeys.Length == 0)
                {
                    throw new ArgumentException("fileNames array must have atleast one file name");
                }

                foreach (string cachekey in cacheKeys)
                {
                    if (cachekey == null)
                    {
                        throw new ArgumentNullException("cacheKey");
                    }
                    if (cachekey == string.Empty)
                    {
                        throw new ArgumentException("cacheKey cannot be empty string");
                    }
                }

                keyDependency = new KeyDependency(cacheKeys, start);
            }

            if (fileDependency != null || keyDependency != null || dependency != null)
            {
                if (_dependencies == null)
                {
                    _dependencies = new List <CacheDependency>();
                }

                if (fileDependency != null)
                {
                    _dependencies.Add(fileDependency);
                }

                if (keyDependency != null)
                {
                    _dependencies.Add(keyDependency);
                }

                if (dependency != null)
                {
                    _dependencies.Add(dependency);
                }
            }

            _startAfter = start;
        }
Esempio n. 2
0
        public static Dependency GetProtoBufDependency(CacheDependency cacheDependency, Dependency dependency)
        {
            if (cacheDependency is Runtime.Dependencies.FileDependency)
            {
                Runtime.Dependencies.FileDependency fileDependency  = cacheDependency as Runtime.Dependencies.FileDependency;
                Protobuf.FileDependency             protoDependency = new Protobuf.FileDependency();
                protoDependency.filePaths.AddRange(fileDependency.fileNames);
                protoDependency.startAfter = fileDependency.StartAfterTicks;

                dependency.fileDep.Add(protoDependency);

                return(dependency);
            }
            else if (cacheDependency is Runtime.Dependencies.KeyDependency)
            {
                Runtime.Dependencies.KeyDependency keyDependency = cacheDependency as Runtime.Dependencies.KeyDependency;

                Alachisoft.NCache.Common.Protobuf.KeyDependency protoDependency = new Alachisoft.NCache.Common.Protobuf.KeyDependency();
                protoDependency.keys.AddRange(keyDependency.CacheKeys);
                protoDependency.startAfter = keyDependency.StartAfterTicks;

                dependency.keyDep.Add(protoDependency);

                return(dependency);
            }
            else if (cacheDependency is Runtime.Dependencies.DBCacheDependency)
            {
                Runtime.Dependencies.DBCacheDependency dbDependency = cacheDependency as Runtime.Dependencies.DBCacheDependency;

                switch (dbDependency.Type)
                {
                case Runtime.Dependencies.DBDependencyType.OleDbCacheDependency:
                    OleDbDependency oleDbDependency = new OleDbDependency();
                    oleDbDependency.connectionString = dbDependency.ConnectionString;
                    oleDbDependency.dbCacheKey       = dbDependency.PrimaryKey;
                    dependency.oleDbDep.Add(oleDbDependency);

                    break;

                case Runtime.Dependencies.DBDependencyType.SqlCacheDependency:
                    Sql7Dependency sqlDependency = new Sql7Dependency();
                    sqlDependency.connectionString = dbDependency.ConnectionString;
                    sqlDependency.dbCacheKey       = dbDependency.PrimaryKey;
                    dependency.sql7Dep.Add(sqlDependency);

                    break;
                }

                return(dependency);
            }
            else if (cacheDependency is Runtime.Dependencies.SqlCacheDependency)
            {
                Runtime.Dependencies.SqlCacheDependency sqlDependency = cacheDependency as Runtime.Dependencies.SqlCacheDependency;

                YukonDependency yukonDependency = new YukonDependency();
                yukonDependency.commandType      = Convert.ToInt32(sqlDependency.CommandType);
                yukonDependency.connectionString = sqlDependency.ConnectionString;
                yukonDependency.query            = sqlDependency.CommandText;

                if (sqlDependency.CommandParams != null)
                {
                    foreach (KeyValuePair <string, Runtime.Dependencies.SqlCmdParams> pair in sqlDependency.CommandParams)
                    {
                        Runtime.Dependencies.SqlCmdParams param = pair.Value;


                        YukonParam yukonParam = new YukonParam();
                        yukonParam.key = pair.Key;

                        YukonCommandParam yukonCmdParam = new YukonCommandParam();
                        yukonCmdParam.dbType           = (int)param.SqlParamType;
                        yukonCmdParam.direction        = (int)param.SqlParamDir;
                        yukonCmdParam.isNullable       = param.IsNullable;
                        yukonCmdParam.localeId         = param.LocaleID;
                        yukonCmdParam.offset           = param.Offset;
                        yukonCmdParam.precision        = param.Precision;
                        yukonCmdParam.scale            = param.Scale;
                        yukonCmdParam.size             = param.Size;
                        yukonCmdParam.sourceColumn     = param.SourceColumn;
                        yukonCmdParam.sourceColumnNull = param.SourceColumnNullMapping;
                        yukonCmdParam.sqlValue         = param.SqlValue != null?param.SqlValue.ToString() : "";

                        yukonCmdParam.version           = (int)param.SrcVersion;
                        yukonCmdParam.typeName          = param.TypeName;
                        yukonCmdParam.typeId            = (int)param.Type;
                        yukonCmdParam.udtTypeName       = param.UdtTypeName;
                        yukonCmdParam.nullValueProvided = param.Value == null ? true : false;

                        if (!yukonCmdParam.nullValueProvided)
                        {
                            if (param.Type == CmdParamsType.Binary || param.Type == CmdParamsType.VarBinary /*|| param.Type == CmdParamsType.Image */ || param.Type == CmdParamsType.Timestamp)
                            {
                                byte[] val = param.Value as byte[];
                                if (val == null)
                                {
                                    throw new OperationFailedException("Expected 'System.Byte[]' value for parameter '" + param.SourceColumn + "'");
                                }
                                else
                                {
                                    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                                    yukonCmdParam.value = encoding.GetString(val);
                                }
                            }
                            else if (param.Type == CmdParamsType.DateTime || param.Type == CmdParamsType.DateTime2 || param.Type == CmdParamsType.Date || param.Type == CmdParamsType.SmallDateTime)
                            {
                                try
                                {
                                    DateTime val = (DateTime)param.Value;
                                    yukonCmdParam.value = val.Ticks.ToString();
                                }
                                catch (InvalidCastException ex)
                                {
                                    throw new OperationFailedException("Expected 'System.DateTime' value for parameter type '" + param.Type + "'");
                                }
                            }
                            else if (param.Type == CmdParamsType.Time)
                            {
                                try
                                {
                                    TimeSpan val = (TimeSpan)param.Value;
                                    yukonCmdParam.value = val.Ticks.ToString();
                                }
                                catch (InvalidCastException ex)
                                {
                                    throw new OperationFailedException("Expected 'System.TimeSpan' value for parameter '" + param.Type + "'");
                                }
                            }
                            else if (param.Type == CmdParamsType.DateTimeOffset)
                            {
                                try
                                {
                                    DateTimeOffset val = (DateTimeOffset)param.Value;
                                    yukonCmdParam.value = String.Concat(val.Date.Ticks, ",", val.Offset.Minutes);
                                }
                                catch (InvalidCastException ex)
                                {
                                    throw new OperationFailedException("Expected 'System.DateTimeOffset' value for parameter '" + param.Type + "'");
                                }
                            }
                            else
                            {
                                yukonCmdParam.value = param.Value.ToString();
                            }
                        }

                        yukonParam.cmdParam = yukonCmdParam;
                        yukonDependency.param.Add(yukonParam);
                    }
                }

                dependency.yukonDep.Add(yukonDependency);

                return(dependency);
            }

            else if (cacheDependency is Runtime.Dependencies.OracleCacheDependency)
            {
                Runtime.Dependencies.OracleCacheDependency oracleDependency = cacheDependency as Runtime.Dependencies.OracleCacheDependency;

                OracleDependency protoDependency = new OracleDependency();
                protoDependency.commandType      = (int)oracleDependency.CommandType;
                protoDependency.connectionString = oracleDependency.ConnectionString;
                protoDependency.query            = oracleDependency.CommandText;

                if (oracleDependency.CommandParams != null)
                {
                    foreach (KeyValuePair <string, Runtime.Dependencies.OracleCmdParams> pair in oracleDependency.CommandParams)
                    {
                        Runtime.Dependencies.OracleCmdParams oracleCommandParams = pair.Value;

                        OracleParam param = new OracleParam();
                        param.key = pair.Key;

                        OracleCommandParam oracleCmdParam = new OracleCommandParam();
                        oracleCmdParam.dbType    = (int)oracleCommandParams.Type;
                        oracleCmdParam.direction = (int)oracleCommandParams.Direction;
                        oracleCmdParam.value     = oracleCommandParams.Value != null?oracleCommandParams.Value.ToString() : "";

                        param.cmdParam = oracleCmdParam;

                        protoDependency.param.Add(param);
                    }
                }

                dependency.oracleDep.Add(protoDependency);

                return(dependency);
            }


            else if (cacheDependency is Runtime.Dependencies.ExtensibleDependency)
            {
                Runtime.Dependencies.ExtensibleDependency extDependency = cacheDependency as Runtime.Dependencies.ExtensibleDependency;

                IFormatter   formatter = new BinaryFormatter();
                MemoryStream stream    = new MemoryStream();
                formatter.Serialize(stream, extDependency);

                Alachisoft.NCache.Common.Protobuf.ExtensibleDependency extensibleDependency = new Alachisoft.NCache.Common.Protobuf.ExtensibleDependency();
                extensibleDependency.data = stream.ToArray();

                dependency.xtDep.Add(extensibleDependency);

                return(dependency);
            }
            else if (cacheDependency is NosDBDependency)
            {
                NosDBDependency sqlDependency = cacheDependency as NosDBDependency;

                NosDbDependency nosDependency = new NosDbDependency();
                nosDependency.timeout          = Convert.ToInt32(sqlDependency.Timeout);
                nosDependency.connectionString = sqlDependency.ConnectionString;
                nosDependency.query            = sqlDependency.CommandText;

                if (sqlDependency.Parameters != null)
                {
                    CommandHelper.PopulateValues(sqlDependency.Parameters, nosDependency.param);
                }
                dependency.NosDep.Add(nosDependency);

                return(dependency);
            }
            else
            {
                foreach (Runtime.Dependencies.CacheDependency dep in cacheDependency.Dependencies)
                {
                    dependency = GetProtoBufDependency(dep, dependency);
                }

                return(dependency);
            }
        }