Nate B.
Nate B.

Reputation: 942

Windows Service and cache

I'm currently developing a windows service in .net 4. It connects to a WS which send back the info I need. I use a timer: every x seconds, the service asks the webservice the info. But to avoid accessing WS at each elapse, I would like to store these credentials in cache.

I googled and do not find anything relevant for the Windows Service situation (it's always about ASP.NET environment).

I've tried MemoryCache (from ObjectCache from System.Runtime.Caching) without success. Here is my class I use the cache.

Am I a in the good way or completely wrong ?

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;
    }
}

Is MemoryCache the good class to use ?

In [another post][1], they suggest ASP.NET Cache (System.Web.Caching), but it seems weird in a Windows Service, isn't it ?

If you could guide me a bit, it would be appreciated.

Edit

I changed new DateTimeOffset(0, 0, 1, 0, 0, 0, new TimeSpan(1, 0, 0, 0)) by new DateTimeOffset(DateTime.UtcNow.AddHours(24)) without difference and it works perfectly !

[1]: Caching for .NET (not in websites)emphasized text

Upvotes: 4

Views: 10613

Answers (1)

John Sobolewski
John Sobolewski

Reputation: 4572

Try this instead.

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

Basically you were sending an absolute expiration date of the year 0. I'm not certain how that even works given the documentation says that DateTimeOffset needs the month to be 1-12.

You should get an argument out of range exception. ??? If you go here

And run this...

using System;

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)));
      }
   } 

You will get the following exception.

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 

Upvotes: 2

Related Questions