Cache dependencies rules between keys

Topics: User Forum
Jul 12, 2008 at 11:46 AM
Would it be possible to add rules for dependencies between keys
Suppose i add the following items to the cache
- item#1
- item#2
- item #3
And suppose that item#3 is depended on item#1 and item#2,
This means that whenever item#1 or item#2 is updated/deleted that item#3 should automaticly be removed from the cache

If SharedCache would support something like this then it would save a lot of client-actions
since atm the clients needs to implement this behaviour and remove item#3 whenever item#1 or item#2 is updated/deleted

Jul 15, 2008 at 12:18 PM
hi erwin,

thanks for your post.

We have this issue also tested to create dependencies and we ended up with regular expressions. One of the people told me that he
uses it quite extensivly now. Have a look at the test program how you can work with regular expressions - its quite easy and
very fast.

Feb 16, 2009 at 8:17 AM
Edited Feb 16, 2009 at 8:18 AM
i'm also missing a cache dependency for at least keys - however great tool.
can you please post exactly where to find the example for regex you talked about?
or can you post the snippets here?
thanks, tob
Feb 16, 2009 at 6:21 PM
hi tob,

i have uploaded a sample to the following URL:

Hopefully this help you to implemente your needs.

Feb 17, 2009 at 11:31 AM
hi roni,
thanks for the good examples!

however i'm still stuck a little bit:
the person (manager:person) example is a 1:n relationship and therefore pretty easy. also the person does not "contain" another class/objects that can be cached.

lets imagine a (sales)person could have handled multiple "orders". person now has a property List<Order> Orders.
The Order class has a property to its "parent" salesperson - Person SalesPerson{get;set;}
The order itself also contains multiple products - so it has a List<Product> Products property.

The Product class itself contains a Orders (List<Order>) property to get all the Orders that contains this product.

Now imagine a website, where users can select the language: german and english
The Product class has a Description (string) Property that can vary due to the CultureInfo of the current thread. (Thread.CurrentThread.CurrentCulture).

How to handle such "dependencies" with regular expressions and sharedcache if
a) the Person changes
b) a Order changes
c) a Product changes
d) the selected web-language/cultureinfo changes

i really would like to know how to handle caching with dependencies in such a application.
how to invalidate certain objects from the cache if some objects a) higher or b) lower in the relation  changes?
are you able to provide an example on such an application with sharedcache?
i would be really grateful!

thanks, tob
Feb 17, 2009 at 1:45 PM
hi tob,

can you provide me your preffered c# classes? once you provide me them i will extend the SharedCacheSolution with your code, therefore remember please only to provide code which is i will deploy afterwards.

Feb 18, 2009 at 2:18 AM
This problem intrigues me. To help me understand:





AddToCache (joePerson.ID, joePerson);

You do this:

UPDATE Order SET Order.SomeProperty = 5 WHERE Order.ID = 1

And you want something like:

Person INNER JOIN Order ON Order.PersonId = Person.Id
WHERE Order.Id = 1

EXPIRE: Person.Id(s)

If so, the problem that I see is that this would require implicit knowledge of the types involved in all of these relationships. The cache only has knowledge of the key and the payload (object).

I think if you wanted this to work, you would have to flatten out your cache additions to make them similar to tables:

// Person.Id = 12
AddToCache ("12-1,2", joePerson);

// Optional
// Order ID = 1
AddToCache ("1-12", someOrder);

// OrderID = 2
AddToCache("2-12", someOrder);

So for orders in the cache, format is always "{0}-{1}" where 0 = Order.Id and 1 = Order.Person.
For Persons, format is {0}-{1},{2},{3}..... where 0 = Person.Id and 1,2,3 is a comma separated list of orders.

Where order 1 changes, you would have to search for Orders where /1/, Person's where /\d+-.*1/

As far as the descriptions, perhaps string Product.Description should be:


so Description = Descriptions[thread.CurrentThread.CurrentCulture]

Is that helpful?

Feb 18, 2009 at 10:06 AM
Edited Feb 18, 2009 at 10:11 AM
i created a small project that contains a user, order and product class. the product class has a Parent and a Children property, too. i tried to comment a few fields and methods but i think you should get an idea about it.

as i can not attach a file here i uploaded the project to rapidshare:
Your file has been saved and can now be downloaded 10 times. It will be deleted after 90 days without download

i hope you will come along with it!

mikenac: you describe it correctly.
If so, the problem that I see is that this would require implicit knowledge of the types involved in all of these relationships. The cache only has knowledge of the key and the payload (object).

I think if you wanted this to work, you would have to flatten out your cache additions to make them similar to tables:
is there another away to archive this?
because imagine: your cached objects would not only depend on 1 or 2 other objects. instead it would depend on say 6 or 10 other objects.

sure the key could then be more accurate - i.e. for the user object cache key: "UserID:1-OrderID:1,2-OtherObjectOne:5,7,8,61-OtherObjectTwo:424,555..."
but this would be really complicated and certainly errorprone.

i have a project that has many objects/tables that interrelate with each other and now want to improve the performance of it by using sharedcache.

just for your information:
my projects spread 3 web applications and some windows services that are all using the same DB therefore i can NOT use the / framework's cache because it isn't distributed caching.
i also can not use SQL Server 2005 SqlDependencies because many querys are using joins and other things - in short: due to the SqlDependencies limitations i can not use it)

thanks for your help so far!!

Feb 22, 2009 at 3:01 AM
just wanted to let you know that i started today a promo based on your classes. hopefully i find enough time to finish it within the next few days.