【问题标题】:Windows Service and cacheWindows 服务和缓存
【发布时间】:2013-04-09 20:31:13
【问题描述】:

我目前正在.net 4 中开发一个 Windows 服务。它连接到一个 WS,它发回我需要的信息。 我使用一个计时器:每隔 x 秒,服务会向网络服务询问信息。但是为了避免每次都访问 WS,我想将这些凭据存储在缓存中。

我用谷歌搜索并没有找到任何与 Windows 服务情况相关的内容(它总是与 ASP.NET 环境有关)。

我试过MemoryCache(来自ObjectCache来自System.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 是好用的类吗?

在 [another post][1] 中,他们建议使用 ASP.NET Cache (System.Web.Caching),但在 Windows 服务中这似乎很奇怪,不是吗?

如果您能指导我一点,将不胜感激。

编辑

我将new DateTimeOffset(0, 0, 1, 0, 0, 0, new TimeSpan(1, 0, 0, 0)) 更改为new DateTimeOffset(DateTime.UtcNow.AddHours(24)) 没有区别 效果很好

[1]:Caching for .NET (not in websites)强调文字

【问题讨论】:

标签: c# caching windows-services


【解决方案1】:

试试这个。

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

基本上,您发送的是 0 年的绝对到期日期。鉴于文档说 DateTimeOffset 需要月份为 1-12,我不确定它是如何工作的。

你应该得到一个参数超出范围异常。 ??? 如果你去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 

【讨论】:

  • 嗨!感谢您的回答。你统治!它完美无缺!我没有返回错误,所以我认为这是关于我使用 MemoryCache 对象或类似的东西的方式。非常感谢。
猜你喜欢
  • 2012-01-18
  • 1970-01-01
  • 1970-01-01
  • 2013-11-26
  • 2021-12-30
  • 2017-11-24
  • 1970-01-01
  • 2014-12-02
  • 1970-01-01
相关资源
最近更新 更多