⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filedependency.cs

📁 HeyCacher 高性能缓存方案(带源码) 1. 文件的所有权益归上传用户所有 2. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途 3. CSDN下载频道仅提供交流平台
💻 CS
字号:
//===============================================================================
// CSDN HeyCache 
//===============================================================================
// 修改记录:[按最后修改时间倒排序]
// 2007.06.11 by tangwei
//
// 代码来源:参考于dotnet企业库3.0版
//===============================================================================

using System;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Security.Permissions;
using Microsoft.Practices.EnterpriseLibrary.Caching.Properties;

namespace HeyCacher.Components.Expirations
{
    /// <summary>
    ///	使用文件为依赖的过期策略
    /// </summary>
    [Serializable]
    [ComVisible(false)]
    public class FileDependency : INetCacheItemExpiration
    {
        public string FileName
        {
            get { return dependencyFileName; }
        }
        private string dependencyFileName;

        public DateTime LastModifiedTime
        {
            get { return lastModifiedTime; }
        }
        private DateTime lastModifiedTime;

        public FileDependency() { }
        public FileDependency(string fullFileName)
        {
            innerCreate(fullFileName, null);
        }
        public FileDependency(string fullFileName, params string[] dependency)
        {
            innerCreate(fullFileName, dependency);
        }

        public void Create(params string[] Params)
        {
            string fullFileName = null;
            string[] dependency = null;
            if (Params.Length > 0)
            {
               fullFileName = Params[0];
               if (Params.Length > 1)
               {
                   dependency = new string[Params.Length - 1];
                   for (int i = 1; i < Params.Length; i++)
                   {
                       dependency[i - 1] = Params[i];
                   }
               }
            }
            innerCreate(fullFileName, dependency);
        }

        public void innerCreate(string fullFileName, params string[] dependency)
        {
            if (dependency != null)
            {
                this.m_Dependency = dependency;
            }

            if (string.IsNullOrEmpty(fullFileName))
            {
                throw new ArgumentException("fullFileName", Resources.ExceptionNullFileName);
            }

            dependencyFileName = Path.GetFullPath(fullFileName);
            EnsureTargetFileAccessible();

            if (!File.Exists(dependencyFileName))
            {
                throw new ArgumentException(Resources.ExceptionInvalidFileName, "fullFileName");
            }

            this.lastModifiedTime = File.GetLastWriteTime(fullFileName);
        }

        public bool HasExpired()
        {
            EnsureTargetFileAccessible();

            if (File.Exists(this.dependencyFileName) == false)
            {
                return true;
            }

            DateTime currentModifiedTime = File.GetLastWriteTime(dependencyFileName);
            if (DateTime.Compare(lastModifiedTime, currentModifiedTime) != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void Notify()
        {
        }
        public void Initialize(ExpirationItem owningCacheItem)
        {
        }

        /// <summary>
        /// 有效机器标识
        /// </summary>
        /// <returns></returns>
        public string[] AvailableIn()
        {
            return m_Dependency;
        }
        private string[] m_Dependency = { Components.AppSetting.Mark };

        private void EnsureTargetFileAccessible()
        {
			// keep from changing during demand
			string file = dependencyFileName;
            FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Read, file);
            permission.Demand();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -