2013-04-09 76 views
3

我目前正在.NET 4中開發一個Windows服務。它連接到一個WS,它發回我需要的信息。 我使用一個計時器:每x秒,服務詢問web服務的信息。但爲了避免每次訪問WS,我想將這些憑據存儲在緩存中。Windows服務和緩存

我搜索了一下,沒有發現任何與Windows服務情況相關的東西(它總是關於ASP.NET環境)。

我試過MemoryCache(從ObjectCacheSystem.Runtime.Caching)沒有成功。 這是我的課我使用緩存。

我是好的還是完全錯的?

public class Caching 
{ 
    private const string CST_KEY = "myinfo"; 
    private const string CST_CACHENAME = "mycache"; 

    private MemoryCache _cache; 

    public Caching() 
    { 
     _cache = new MemoryCache(CST_CACHENAME); 
    } 

    private CacheItemPolicy CacheItemPolicy 
    { 
     get 
     { 
      return new CacheItemPolicy 
       { 
        SlidingExpiration = new TimeSpan(1, 0, 0, 0), 
        AbsoluteExpiration = new DateTimeOffset(0, 0, 1, 0, 0, 0, new TimeSpan(1, 0, 0, 0)) 
       }; 
     } 
    } 

    public bool SetClientInformation(ClientInformation client_) 
    { 
     if (_cache.Contains(CST_KEY)) 
      _cache.Remove(CST_KEY); 

     return _cache.Add(CST_KEY, client_, CacheItemPolicy); 
    } 

    public bool HasClientInformation() 
    { 
     return _cache.Contains(CST_KEY); 
    } 

    public ClientInformation GetClientInformation() 
    { 
     return _cache.Contains(CST_KEY) ? (ClientInformation) _cache.Get(CST_KEY) : null; 
    } 
} 

MemoryCache是不是很好用的類?

在[另一篇文章] [1]中,他們建議ASP.NET緩存(System.Web.Caching),但在Windows服務中似乎很奇怪,不是嗎?

如果你能指導我一點,它將不勝感激。

編輯

我通過new DateTimeOffset(DateTime.UtcNow.AddHours(24))改變new DateTimeOffset(0, 0, 1, 0, 0, 0, new TimeSpan(1, 0, 0, 0))沒有區別和它完美的作品

[1]:Caching for .NET (not in websites)強調文本

+0

這是同樣的問題:http://stackoverflow.com/questions/6059860/memorycache-add-returns-true-but-does-not-add-item-to-cache – 2013-04-09 11:45:33

回答

2

試試這個。

cacheItemPolicy.AbsoluteExpiration = new DateTimeOffset(DateTime.UtcNow.AddHours(24)); 

基本上給出的文檔,即使作品如何說的DateTimeOffset需要一個月是1-12你發送0年的絕對有效期限我不能肯定。

你應該得到一個參數超出範圍的異常。 ??? 如果你去here

並運行此...使用系統

;

namespace Dela.Mono.Examples 
{ 
    public class HelloWorld 
    { 
     public static void Main(string[] args) 
     { 
     Console.WriteLine(new DateTimeOffset(0, 0, 1, 0, 0, 0, new TimeSpan(1, 0, 0, 0))); 
     } 
    } 

您將得到以下例外。

Compiling the source code.... 
$/usr/local/bin/mcs /tmp/136548353410035/csharp136548353410035.cs 2>&1 

Executing the program.... 
$mono /tmp/136548353410035/csharp136548353410035.exe 

Unhandled Exception: System.ArgumentOutOfRangeException: Argument is out of range. 
Parameter name: Parameters describe an unrepresentable DateTime. 
at System.DateTime..ctor (Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond) [0x00000] in <filename unknown>:0 
at System.DateTime..ctor (Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second) [0x00000] in <filename unknown>:0 
at System.DateTimeOffset..ctor (Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, TimeSpan offset) [0x00000] in <filename unknown>:0 
at Dela.Mono.Examples.HelloWorld.Main (System.String[] args) [0x00000] in <filename unknown>:0 
+0

嗨!感謝您的回答。你統治!它完美的工作!我沒有回來的錯誤,所以我認爲這是關於我使用MemoryCache對象或類似的東西。非常感謝。 – Gnial0id 2013-04-09 12:28:17