user1618825
user1618825

Reputation:

Setting Sql Dependency with ICacheClient

I am using ServiceStack for caching purpose in an ASP.NET MVC4 API project. Now I need to set a sql dependency for it.

  1. Is there a way to set SQL dependency ICacheClient?
  2. I thought of doing it by generating unique string from post parameters. Is there a way to generate unique string from post parameters or query string parameters?

Upvotes: 0

Views: 156

Answers (1)

David
David

Reputation: 8670

Firstly you need to enable change tracking in your database.

The microsoft way of doing this would be to use the aspnet_regsql tool to 'enable' cache dependency on one or many of your database tables.

aspnet_regsql -S localhost -E -d yourdatabase -ed //enable database for cache

aspnet_regsql -S localhost -E -d yourdatabase -et -t yourtable // enabling table for cache

This creates update,insert,delete triggers on the table that increment a counter in another table it creates. So when you i.e run an update on Users table the corresponding row in the cache table will have its counter incremented.

Now you need to hook into this new table from your ServiceStack layer.

One way would be to query the table every X seconds (it will be a quick query as there will only be as many rows as there are enabled tables). The returned data may look like:

TABLE    CHANGE_COUNT
Users    13
Tags     432

Using the data returned you can construct generational cache keys. If/when the change count increments it will mean the next time you get your cache key the number in it will have changed, therefor psuedo-invalidating the cache.

Upvotes: 2

Related Questions