views:

368

answers:

1

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:

  1. 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).

  2. 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?

  3. 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.

A: 

If you give some more information about your system we may be more helpful. For example how many passengers will you have ? How powerful is your server etc, etc.

But with the given info I can say:

  1. You can cache the Passenger objects in an array after finding them. Then when you want make another search first search in this array. If the passenger is not found in your array, then go to database, get the passenger info, add the pasenger info to your array and return the found passenger info.

  2. This way seems nice in my opinion. We use this also in some mid size web sites. If someone shows better options I'd appreciate also.

Aykut
Hi there, would using this array method add more overhead? Also, if I update the passenger data, then I would want to remove the stale data from the cache and using the array seems to me as if it would add more complexity/overhead.The number of passengers will be about 100,000.
Astrofaes
If you are going to a database (1. open connection, 2. run some query, 3. close connection) for everyuser then I think my method should work faster. But I assume here that you do not search for passengers too often. For example if you cache the passengers for half an hour then your array size should not exceed 10000 elements. And you're right about complexity. When you update a passenger, you should also update/clean the data in your array.
Aykut