开发者

ASP.NET Caching at data access layer (best practice question)

开发者 https://www.devze.com 2022-12-17 10:42 出处:网络
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):

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 t开发者_Go百科he 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.


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.


Coming back to this question after some time. I've come to the thought that this can be quite a subjective issue. For what it's worth, I actually ended up storing the Passenger by a key generated from the Passenger ID in the application cache, for example Passenger_100. Then I created a proxy object in the key that would generated from Passenger URL (Passenger_myurl), which acts as a reference back to the Passenger_100 key.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号