2016-07-24 45 views

回答

1

您可以实现自定义OutputCacheProvider。

public class FileCacheProvider : OutputCacheProvider 
{ 
    private string _cachePath; 

    private string CachePath 
    { 
     get 
     { 
      if (!string.IsNullOrEmpty(_cachePath)) 
       return _cachePath; 

      _cachePath = ConfigurationManager.AppSettings["OutputCachePath"]; 
      var context = HttpContext.Current; 

      if (context != null) 
      { 
       _cachePath = context.Server.MapPath(_cachePath); 
       if (!_cachePath.EndsWith("\\")) 
        _cachePath += "\\"; 
      } 

      return _cachePath; 
     } 
    } 

    public override object Add(string key, object entry, DateTime utcExpiry) 
    { 
     Debug.WriteLine("Cache.Add(" + key + ", " + entry + ", " + utcExpiry + ")"); 

     var path = GetPathFromKey(key); 

     if (File.Exists(path)) 
      return entry; 

     using (var file = File.OpenWrite(path)) 
     { 
      var item = new CacheItem { Expires = utcExpiry, Item = entry }; 
      var formatter = new BinaryFormatter(); 
      formatter.Serialize(file, item); 
     } 

     return entry; 
    } 

    public override object Get(string key) 
    { 
     Debug.WriteLine("Cache.Get(" + key + ")"); 

     var path = GetPathFromKey(key); 

     if (!File.Exists(path)) 
      return null; 

     CacheItem item = null; 

     using (var file = File.OpenRead(path)) 
     { 
      var formatter = new BinaryFormatter(); 
      item = (CacheItem)formatter.Deserialize(file); 
     } 

     if (item == null || item.Expires <= DateTime.Now.ToUniversalTime()) 
     { 
      Remove(key); 
      return null; 
     } 

     return item.Item; 
    } 

    public override void Remove(string key) 
    { 
     Debug.WriteLine("Cache.Remove(" + key + ")"); 

     var path = GetPathFromKey(key); 

     if (File.Exists(path)) 
      File.Delete(path); 
    } 

    public override void Set(string key, object entry, DateTime utcExpiry) 
    { 
     Debug.WriteLine("Cache.Set(" + key + ", " + entry + ", " + utcExpiry + ")"); 

     var item = new CacheItem { Expires = utcExpiry, Item = entry }; 
     var path = GetPathFromKey(key); 

     using (var file = File.OpenWrite(path)) 
     { 
      var formatter = new BinaryFormatter(); 
      formatter.Serialize(file, item); 
     } 
    } 

    private string GetPathFromKey(string key) 
    { 
     return CachePath + MD5(key) + ".txt"; 
    } 

    private string MD5(string s) 
    { 
     var provider = new MD5CryptoServiceProvider(); 
     var bytes = Encoding.UTF8.GetBytes(s); 
     var builder = new StringBuilder(); 

     bytes = provider.ComputeHash(bytes); 

     foreach (var b in bytes) 
      builder.Append(b.ToString("x2").ToLower()); 

     return builder.ToString(); 
    } 
} 

,并在web.config中注册您的提供商

<appSettings> 
    <add key="OutputCachePath" value="~/Cache/" /> 
</appSettings> 

<caching> 
    <outputCache defaultProvider="FileCache"> 
    <providers> 
     <add name="FileCache" type="MyCacheProvider.FileCacheProvider, MyCacheProvider"/> 
    </providers> 
    </outputCache> 
</caching> 
+0

谢谢你的答案,你的代码是好的,如果缓存位于在该项目托管的目录。但我需要的是在项目目录之外的另一个文件夹中创建缓存文件(或者可能位于另一个磁盘驱动器中)。 –

+0

您应该更改IIS设置。 对于在Windows Server 2008 R2上运行的IIS 7 ...在IIS管理器中,选择运行您的网站的应用程序池。 点击“高级设置”。将有一个标识条目(它位于过程模型部分下)。点击它,为有权访问共享的帐户提供凭据。 有关详细信息,请参阅以下问题 - http://stackoverflow.com/questions/14611015/iis7-accessing-network-share –