1

I have a table in sql server which I will query many times from the .net MVC code and I want to implement appfabric cache instead of sql server query(only for this query). How do I implement this? If the query is like- select address from people where id = @cid;

cid can be 1,2 or 3 which comes from the code

1
  • This blog post should cover most of what you need. Commented Dec 23, 2013 at 6:30

2 Answers 2

3

Do you really need AppFabric for this scenario ?

I mean, AppFabric Caching targets high volume web sites where database is a bottleneck and local caching is no longer a good choice. Yes, it's caching but for large data and distributed scenarios. In addition, you have to install additional dedicated servers for the caching tier.

Maybe a basic MemoryCache is enough to start : Local Cache, Nothing to install, no additional servers required and built-in support in .net bcl.

Here is a basic example :

//Create a cache instance
ObjectCache cache = MemoryCache.Default;     

//check the cache for a specific value
if (cache.Contains("mydata"))     
{         
    //get an item from the cache
    var value=cache.Get("mydata");     
}     
else    
{     
    //load data
    var data=fetchData();
    //add an data to the cache 
    cache.Add("mydata", data);     
} 

For caching a small piece of data and for one (or a few) web servers, it's fairly enough.

Sign up to request clarification or add additional context in comments.

6 Comments

As long as you don't need to invalidate the cache remotely - and you're targeting .Net >= 4 - the MemoryCache is indeed very useful. We have a system where we use an AppFabric cluster, falling back to a MemoryCache if the cluster is down: we have to accept that we cannot invalidate cached data for the lifetime of the MemoryCache cache, but that is acceptable in our scenario, and we use a short cache expiration in the MemoryCache to mitigate the problem.
I am just learning the basics, our application do have high volume. So we need to start with appfabric little by little.
@Nithin As I said, before jumping in AppFabric, it's costless to try MemoryCache. With a little bit of abstraciton, it's not so difficult to implement a generic cache client for MemoryCache and for AppFabric and switch between implementions with your favorite IoC. High volume is not a real problem. What is important : how many web servers do you have ? what is the db activity ?
@Cybermaxs-Betclic what's your strategy for remote invalidation with MemoryCache? Say you have some entities cached, but they are then updated, how do you remove them from the cache? With AppFabric - and other distributed caches, of course - this is trivial, but how do you manage it with local MemoryCaches on +1 servers?
@stuartd. IT can be done with a PubSub. MemoryCache also supports invalidation through ChangeMonitor. I blogged an implementation using Redis here and source code is here
|
0

Whichever cache you use, you would add different cache entries for each query, using a construct of a base key and the ID as the cache key, and the return from the database as the cached value:

public Address GetData(int cId)
{
    const string AddressCacheKeyBase = "AddressCacheKey";
    string key = AddressCacheKeyBase + cId;

    var cachedData = cache.Get(key); 

    if (cachedData != null)
    {   
         return (Address)cachedData;      
    }

    Address address = GetAddressFromDatabase(cID);
    cache.Add(key, address);

    return address;
}

2 Comments

Can we have single key with multiple values in the cache? May be as a single object?
@Nithin you certainly can, you could use a Dictionary<int, Address> and have the Id as the key and the Address as the value. However whenever you need to add - or invalidate - a value, you need to read the Dictionary from the cache, add or update the address, then store the Dictionary back into the cache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.