Archive for the ‘AddCacheLevelCallback’ Tag
Notification callback in Velocity CTP3
This is an example of how to make the Notification callback work in Velocity CTP3.
Here is my web.config – the cache is a routing client:
<?xml version=“1.0“?>
<configuration>
<configSections>
<!– required to read the <dataCacheClient> element –>
<section name=“dataCacheClient“
type=“Microsoft.Data.Caching.DataCacheClientSection,CacheBaseLibrary“
allowLocation=“true“
allowDefinition=“Everywhere“/>
<!– required to read the <fabric> element, when present –>
<section name=“fabric“
type=“System.Data.Fabric.Common.ConfigFile,FabricCommon“
allowLocation=“true“
allowDefinition=“Everywhere“/>
<!– routing client–>
<dataCacheClient deployment=“routing“>
<clientNotification pollInterval=“300“ />
<!– cache host(s) –>
<hosts>
<host
name=“VIRTUALB-UJT42H“
cachePort=“22233“
cacheHostName=“DistributedCacheService“/>
</hosts>
</dataCacheClient>
</configuration>
Here is the cache cluster configuration via the command Export-CacheClusterConfig:
<?xml version=“1.0“ encoding=“utf-8“?>
<configuration>
<configSections>
<section name=“dataCache“ type=“Microsoft.Data.Caching.DataCacheSection, CacheBaseLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91“ />
</configSections>
<dataCache cluster=“Velocity_Cluster_1“ size=“Small“>
<caches>
<cache type=“partitioned“ consistency=“strong“ name=“default“>
<policy>
<eviction type=“lru“ />
<expiration defaultTTL=“10“ isExpirable=“true“ />
<serverNotification isEnabled=“true“ />
</policy>
</cache>
<cache type=“partitioned“ consistency=“strong“ name=“default_2“>
<policy>
<eviction type=“lru“ />
<expiration defaultTTL=“10“ isExpirable=“true“ />
<serverNotification isEnabled=“true“ />
</policy>
</cache>
</caches>
<hosts>
<host clusterPort=“22234“ hostId=“1825498580“ size=“1024“ quorumHost=“true“
name=“VIRTUALB-UJT42H“ cacheHostName=“DistributedCacheService“
cachePort=“22233“ />
</hosts>
<advancedProperties>
<partitionStoreConnectionSettings providerName=“System.Data.SqlClient“
connectionString=“Data Source=VIRTUALB-UJT42H\SQLEXPRESS;Initial Catalog=Velocity_Shared_1;User Id=Not_sa;Password=ForYourEyesOnly;“
leadHostManagement=“false“ />
</advancedProperties>
</dataCache>
</configuration>
Here is the code of CacheWorker.cs (kind of helper class with singleton pattern):
using System;
using Microsoft.Data.Caching;
namespace VelocityTest
{
public class CacheWorker
{
private static object _synclock = new object();
private static CacheWorker _cacheWorker = null;
private static DataCacheFactory _cacheFactory = null;
private static DataCache _defaultCache = null;
private CacheWorker()
{
}
public static CacheWorker Instance
{
get
{
lock (_synclock)
{
try
{
if (_cacheWorker == null)
{
_cacheWorker = new CacheWorker();
// we use configuration file
_cacheFactory = new DataCacheFactory();
// default cache
_defaultCache = _cacheFactory.GetDefaultCache();
}
return _cacheWorker;
}
catch (DataCacheException ex)
{
return null;
}
}
}
}
public DataCacheFactory CacheFactory
{
get { return _cacheFactory; }
}
public DataCache DefaultCache
{
get { return _defaultCache; }
}
}
}
Here is the partial code-behind of the test page:
public partial class _Default : System.Web.UI.Page
{
public const string CACHE_KEY = “{8768036E-2B99-4563-8BA5-C2D07B5B6023}”;
protected static DataCacheFactory _factory = CacheWorker.Instance.CacheFactory;
protected static DataCache _cache = CacheWorker.Instance.DefaultCache;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataCacheOperation filter = DataCacheOperation.AddItem | DataCacheOperation.RemoveItem | DataCacheOperation.ReplaceItem | DataCacheOperation.ClearRegion | DataCacheOperation.CreateRegion | DataCacheOperation.RemoveRegion;
DataCacheNotificationDescriptor dn = _cache.AddCacheLevelCallback(filter, OnCacheNotificationReceived);
SetCache(MyPerson, true);
ShowCache(GetCache());
}
btnUpdate.Click += new EventHandler(btnUpdate_Click);
btnRefresh.Click += new EventHandler(btnRefresh_Click);
btnRemove.Click += new EventHandler(btnRemove_Click);
btnGet.Click += new EventHandler(btnGet_Click);
btnAnotherPage.Click += new EventHandler(btnAnotherPage_Click);
}
// other code is here….
public void OnCacheNotificationReceived(string cacheName, string regionName, string key, DataCacheItemVersion version, DataCacheOperation cacheOperation, DataCacheNotificationDescriptor nd)
{
StringBuilder sb = new StringBuilder();
sb.Append(“cacheName: “ + cacheName + Environment.NewLine);
sb.Append(“regionName: “ + regionName + Environment.NewLine);
sb.Append(“key: “ + key + Environment.NewLine);
sb.Append(“cacheOperation: “ + cacheOperation.ToString() + Environment.NewLine);
txtMessageBox.Text = sb.ToString();
}
}
Basically the configuration has the Notification enabled and set the client as routing client. The callback is fired when ever there is a change in cache item or region.
Leave a Comment