a doubt regarding implementation of shared cache

Apr 24, 2009 at 11:20 AM
Dear friends,

This may be a silly question.

I am going to start using sharedcache.

Doubt#1
I have a screen that have search option with ID, Name, Location. If I want to used sharedcache I want to create 3 maps with keys, ID, Name, Location. My code will be like follows. Am I right?

IndexusDistributionCache

.SharedCache.Add(ID_Key, Patientobj);

 

IndexusDistributionCache.SharedCache.Add(Name_Key, Patientobj);

 

IndexusDistributionCache.SharedCache.Add(Location_key, Patientobj);

Doubt#2
I have a screen that have search option with ID, Name, Location. A user trying to get details of patient with Names stard with 'JO' and Location Start with "NE'. If I am not using SharedCache I can directly query to DB like select * from Patient_Table where Name like 'JO%' and Location like 'NE%'. Any body could tell me how can I replace this scenario using Shared Cache? Please forgive me if this is a blondor question :)

Thanks and Regards
Shihab

 

Apr 24, 2009 at 12:46 PM
Doubt#1
from my understanding of your case you will cache 3 times the same data.

[Serializable]
class Patient
{
  public string id {get; set;};
  public string key {get; set;};
  public string location {get; set;};
}

Patient myObject = new Patient();
myObject.id = "test";
myObject.key = "ZRH";
myObject.location = "Zurich";

IndexusDistributionCache.SharedCache.Add(myObject.id, myObject );

in this way you store your data only once.

Doubt#2
you will have to solve this over the key if you really want to do that.

e.g. for such a key:

string key = myObject.id + "_" +myObject.key + "_" + myObject.location 

test_ZRH_Zurich will be the result, from here you can use normal regular expressions.


regards,
roni
Apr 24, 2009 at 12:54 PM
Hi, I was able to explore the Shared Cache's source code. I saw that when using regular expressions, all keys are searched and compared with the pattern you provide, which (I think) cause a big performans penalty. What  I needed was  providing multiple keys while inserting items into the cache, so that I could make search using whatever key(s) I need.
Apr 24, 2009 at 1:35 PM
Edited Apr 24, 2009 at 1:37 PM
Trying to cache the result of free form searches is difficult as a cache is most suited for quick access to common data, and searches generally vary wildly and are not common. You could attempt to store common search queries using the parameters as the key, but I would only do this if the objects themselves were fetched separately by their natural key. So for example, you would generally get and store your Patient objects by their Id and store them in the cache when they are retrieved from your database.
IndexusDistributionCache.SharedCache.Add(patient.Id, patient);
Then your search could return patient Id's as results and you can then lookup each patient by Id and after a few searches many of your common patient objects would already be in the cache and all of the patient object data would not have to be pulled from the database each time. Once that is built then you can attempt to keep a list of search queries keyed by parameters and store them in the cache as well, so that if someone searches for the same exact query you would already have the results of which patient Id's that were returned from the database for that query. So you could store an array of patient Id's in the cache as the result of the search :
//use this as your key, so each search would have a unique key
string searchKey = string.Format("PatientSearch_{0}_{1}", Name, Location) ;

//you would check to see if that search key exists in the cache
int[] patientIds = IndexusDistributionCache.SharedCache.Get<int[]>(searchKey);

if(patientIds == null)
{
//not in the cache so do your db query
//store the results
}

//call your lookup by id (which would also be cached) here for each patient
That is one possible approach, but again, it all depends on the usage patterns of your system and what operations are expensive. Searches are what a relational database does well (if properly indexed) and unless you are google they should only represent a portion of your usage patterns. Generally you would use the cache to store things that will have a very high cache hit ratio so that the database can be freed up to do the things that is is well suited for, like deep searches.

-John
Apr 28, 2009 at 10:01 AM
string searchKey = string.Format("PatientSearch_{0}_{1}", Name, Location) ;

one more doubt, in the above case if there is more than one patient with same name and from same location, it will be a problem rt?
Apr 28, 2009 at 10:40 AM
No, the above is an example of how you could key and store search results not a single instance. So if your search allowed for the search of two parameters it would be keyed on those two parameters and you would cache the ids of the results.  If I searched for Name like 'JO%' and Location like 'NE%' then your cache key would be PatientSearch_NE%_JO% and you would store the ids returned in that search so that if someone searched for those exact parameters again you would already have the ids of the Patient objects for that search stored. You would then retrieve and cache each Patient by Id seperately with a unique key on the PatientId.