Hello,
I currently have a LINQ 2 SQL data model and a supporting 'Passenger' repository class. The repository is as follows (off the top of my head so the code may have errors):
public class PassengerRepository
{
public static Passenger GetPassenger(int passengerId)
{
Passenger p = null;
// Linq to sql code to retrieve passenger by passengerId and store in 'p'
return p;
}
public static Passenger GetPassengerByUrl(string passengerUrl)
{
Passenger p = null
// Linq to sql code to retrieve passenger by passengerUrl and store in 'p'
return p;
}
}
On top of this, I have a PassengerController class as follows:
public class PassengerController
{
public static Passenger GetPassenger(int passengerId)
{
if (Cache["Passenger_" + passengerId] != null)
return (Passenger)Cache["Passenger_" + passengerId];
Passenger p = null;
p = PassengerRepository.GetPassenger(passengerId);
Cache["Passenger_" + passengerId] = p;
return p;
}
public static Passenger GetPassenger(string passengerUrl)
{
if (Cache["PassengerUrl_" + passengerUrl] != null)
return (Passenger)Cache["PassengerUrl_" + passengerUrl];
Passenger p = null;
p = PassengerRepository.GetPassengerByUrl(passengerUrl);
Cache["PassengerUrl_" + passengerUrl] = p;
return p;
}
}
PassengerId and PassengerUrl are always unique for a particular passenger.
My problem is that I am storing duplicates of the Passenger object if fetching by either Id or Url because the Cache keys will be different. When I fetch the data by Id, I am storing it in the cache with a key dependent on PassengerId and thus I can't check if that Passenger object has already been stored for a particular Url. This applies the other way, if fetching by Url, I can't check in the cache if a Passenger object exists for a particular Id. My questions are:
What would be the best way of only storing one instance of the Passenger object in the cache if fetching by either Url or Id? I was thinking maybe creating a Caching wrapper and then when fetching by Url, perhaps once I've got the Passenger datam I store the Passenger in the cache by Id, and then create a new key in the cache for Url and store a string variable with the keyname for the Passenger Id object. (i.e in the passenger Url key, I would store a reference to the passenger Id key).
On a side note, I have all my data access methods in the Controller/Repository classes as static - is this performant and efficient memory wise?
Am I putting my caching in the right place? Do most people put it in the repository class or is the controller an acceptable place to put it?
Any advice would be gratefully appreciated!
Cheers, A.