Freigeben über


DataCache.Put with Regions

If you really want to understand how something works.  Try creating an interface and then creating a stubbed implementation that behaves exactly the same way.  It’s a big job but wow – I’m learning so much about Windows Server AppFabric Caching by doing this.

Consider this method DataCache.Put

 public DataCacheItemVersion Put (
    string key,
    Object value,
    string region
)

I assumed that Put would fail if the key does not exist.  Actually it doesn’t.  The docs says that it Adds or replaces an object in a specified region.

Then I wondered about this

 private static void WhatHappensIfYouPutToADifferentRegion(DataCache dc)
{
    try
    {

        dc.CreateRegion("region1");
        dc.CreateRegion("region2");

        // Put will also add a value
        dc.Put("key", "Add this value to region1", "region1");
        dc.Put("key", "Add this value to region2", "region2");

        // should work
        var value = dc.Get("key", "region2");

        // Also works
        var value2 = dc.Get("key", "region1");

        // Put to a different region does not move an item from region to region
    }
    catch (Exception dcx)
    {
        Console.WriteLine(dcx.ToString());
    }
}

I thought that Put to a different region might remove the object from region1 and add it to region2 effectively moving it from region to region, but of course that makes no sense because it also adds items so it will actually add it to region2 and leave the one in region1 alone.