Distributed Cache Library in C#

Last week I had a conversation with another software engineer who asked me about caching. We had an interesting conversation and it was only later that I remembered that I had experimented with a distributed cache library using the precursor to ServiceWire under the covers along with the v3 open edition of ServiceStack.Text for serialization.

It’s funny that I had completely forgotten about this work. It would have been helpful had I remembered it at the time, but now that I’ve dug it back up, it might be a good idea to dust it off and put some finishing touches on it.

Here’s a small test sample:

[TestMethod]
public void TestClientConfigurationAndConnect()
{
   CacheConfiguration.Current.HostStripedNodePort = 8098;
   CacheConfiguration.Current.ClientStripedClusterEndPoints.Add("cluster",
      new IPEndPoint[]
      {
         new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8098) 
      });

   using (var host = new CacheHost("cluster"))
   {
      host.Open();
      using (ICacheClient client = CacheClient.Connect("b", "cluster"))
      {
         Assert.IsNotNull(client);
         int tval = 89;
         int oval;
         client.Write("k", tval);
         var result = client.TryRead("k", out oval, 0, s => 45);
         Assert.IsNotNull(result);
         Assert.AreEqual(tval, oval);
      }
   }
}

This library was designed to create a local in-memory cache or a distributed cache with ease. Have a look at the code and you’ll find that hosting the cache service in any application domain is rather easy. And if you have multiple service hosts, cache items are distributed via hash and buckets allow you to create more than one named cache which allows the same key to be used without conflict. This would allow more than one client process to utilize the distributed cache.

Of course, this code is not ready for prime time. It still has many rough edges and lacks any monitoring or stats collection. As an experiment it was fun to write. And my only excuse for not blogging about it before is that I honestly got busy with other things and forgot about it.

Have fun with it and as always, if you get any good use out of it, I’d love to hear from you