Correct way to get Items and authorizations from StorageCache

Topics: NetSqlAzMan Core
Feb 25, 2012 at 11:06 PM
Andrea, good afternoon. Thank you for the great product.

I have some questions working with NetSqlAzmanStorage. In my applications I use StorageCache.

1). Why is it impossible to get all the Items (IAzManltems) without quering DB (by means of StorageCache)?

To get all the Items I should use the following code:

var items =
	_instance
		._storageCache
		.Storage
		.GetStore(_StoreName)
		.Applications[_ApplicationName]
		.GetItems();

Execution of this code carries out 4 sql queries – not a pleasant thing if you have to invoke this code quite often.

Is this by design?

2). If it is necessary for a user to remove authorizations, if I understand it right, the sequence should be as follows:

1. Receive all the Items (as in point 1);

2. Execute  storageCache.CheckAccess for each item;

3. If storageCache.CheckAccess returns ‘Allow’ – get authorizations for each item by means of GetAuthorizations(AuthorizationType.Allow);

4. If authorization SID equals SID of the user for whom it is necessary to remove authorization – execute Delete method.

See the full code below:

var items =
	_instance
		._storageCache
		.Storage
		.GetStore(_StoreName)
		.Applications[_ApplicationName]
		.GetItems();

foreach (var azManItem in items)
{
	if (_storageCache.CheckAccess(
			_StoreName, _ApplicationName, azManItem.Name, sid.StringValue, DateTime.UtcNow, false) 
			== AuthorizationType.Allow)
	{
		var authorizations = azManItem.GetAuthorizations(AuthorizationType.Allow);
		foreach (IAzManAuthorization azManAuthorization in authorizations)
		{
			if (azManAuthorization.SID.BinaryValue.SequenceEqual(sid.BinaryValue))
			{
				azManAuthorization.Delete();
			}
		}
	}
}

There comes a problem – each execution of GetAuthorizations() method carries out one query to the DB. And if we need to delete ample quantity of authorizations, it becomes a whole problem.

Would you please tell whether there is another way to delete authorizations without referring to DB (I use cached information in StorageCache)?

Thank you for your help!

Developer
Feb 26, 2012 at 3:08 AM

Any Get function will make calls to the database. All property reference should keep you stay in the memory. So, You can do something like this:

var items =
	_instance
		._storageCache
		.Storage
		.Stores[_StoreName]
		.Applications[_ApplicationName]
		.Items;

var authorizations = azManItem.Authorizations;  // You may use LINQ query to further filter the authorizations.

Be aware that there is  an Indexer in NetSqlAzManStorage class. Supposedly it should also stay in memory but it actually makes a database call. I'll fix this ASAP. So, do not use:

var store = _instance._storageCache.Storage[_StoreName];

Same for the NetSqlAzManStore class. Do not use:

var application = Store[applicationName];

 

 

Hope this will help you.

 

Richard