2016-07-24 12 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マネージャで、Webサイトが実行されているアプリケーションプールを選択します。 [詳細設定]をクリックします。 IDのエントリがあります(プロセスモデルセクションの下にあります)。これをクリックし、共有にアクセスする権限を持つアカウントの資格情報を入力します。 詳細については、次の質問をご覧ください。http://stackoverflow.com/questions/14611015/iis7-accessing-network-share –

関連する問題