/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Globalization;
using System.IO;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Security.Permissions;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Properties;
namespace Everest.Providers.Caching{
/// <summary>
/// This class tracks a file cache dependency.
/// </summary>
[Serializable]
[ComVisible(false)]
public class DirectoryDependency : ICacheItemExpiration, IDisposable
{
private readonly string dependencyDirectory;
private DateTime preModified = DateTime.Now;
private Dictionary<string, DateTime> files = new Dictionary<string, DateTime>();
/// <summary>
/// Initializes a new instance of the <see cref="DirectoryDependency"/> class.
/// </summary>
/// <param name="fullDirectory">The full directory.</param>
public DirectoryDependency(string fullDirectory)
{
if (string.IsNullOrEmpty(fullDirectory))
{
throw new ArgumentException("fullDirectory");
}
dependencyDirectory = Path.GetFullPath(fullDirectory);
EnsureTargetFileAccessible();
if (!Directory.Exists(dependencyDirectory))
{
throw new ArgumentException("ExceptionInvalidFileName", "fullFileName");
}
preModified = Directory.GetLastWriteTimeUtc(dependencyDirectory);
//Initialize();
}
//private void Initialize()
//{
// preModified = Directory.GetLastWriteTime(this.dependencyDirectory);
//}
/// <summary>
/// Gets the name of the dependent file.
/// </summary>
/// <value>
/// The name of the dependent file.
/// </value>
public string DirectoryName
{
get { return dependencyDirectory; }
}
/// <summary>
/// Specifies if the item has expired or not.
/// </summary>
/// <returns>Returns true if the item has expired, otherwise false.</returns>
public bool HasExpired()
{
bool expired = false;
if (!Directory.Exists(this.dependencyDirectory))
{
return true;
}
var lastestModified = Directory.GetLastWriteTime(this.dependencyDirectory);
expired = preModified != lastestModified;
preModified = lastestModified;
return expired;
}
/// <summary>
/// Notifies that the item was recently used.
/// </summary>
public void Notify()
{
}
/// <summary>
/// Not used
/// </summary>
/// <param name="owningCacheItem">Not used</param>
public void Initialize(CacheItem owningCacheItem)
{
}
private void EnsureTargetFileAccessible()
{
// keep from changing during demand
string file = dependencyDirectory;
FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Read, file);
permission.Demand();
}
#region IDisposable Members
public void Dispose()
{
}
#endregion
}
}
|