How do I cache lookup data with Entity Framework 6

守給你的承諾、 提交于 2021-01-29 04:21:03

问题


I'm learning my way around EF, and I know caching is faster than a round trip to the DB for things like state, country, etc. But I'm not sure how to implement it. I was looking at this post (entities from local cache) that mentioned an extension, but is there something built in I should leverage?

I'd like to have a function like this that wouldn't have to go to the db every time:

public static int GetCountryId(string countryCode = "US")
{
    if (countryCode == string.Empty)
        countryCode = "US"; // Assume US
    return db.Country.Where
        (
            p => p.CountryCode == countryCode
        ).Select(p => p.CountryId).First();
}

Updated:

I'm now thinking about a GetCached function that would use generics to hold some lookup lists in memory. Somthing like this:

public class Lookups
{
    private static MkpContext db = new MkpContext();

    public static IEnumerable GetCached(CachedLists list)
    {
        ObjectCache cache = MemoryCache.Default;
        var listOut = cache[list.ToString()] as IEnumerable;
        if (listOut != null) return listOut;

        switch (list)
        {
            case CachedLists.Countries:
                cache.Set
                (
                    list.ToString(),
                    db.Country.ToList(),
                    new DateTimeOffset(DateTime.Now,new TimeSpan(1,0,0,0))
                );
                break;
            default:
                return null;
        }

        listOut = cache[list.ToString()] as IEnumerable;
        return listOut;
    }

}    

public enum CachedLists
{
    Countries,
}

But the above solution would leave me with an un-typed Enumerable. I'd love to be able to specify the types somehow, or better yet, do some sort of extension.


回答1:


There are a lot of options, but here's one approach that will work well if users are mostly querying the same few country codes:

Create a MemoryCache instance to use as a static, private, readonly field on your class. In your method, try to get a cached item from this cache if there is one with the given countryCode as its key. If it's not there, do your database query and Add the result into the cache before returning it.

By the way, the approach in the article you linked probably isn't a very good approach: it will only help if you've already got data in the specific database context instance that you're dealing with, and usually it's best for your contexts to be short-lived. It's also really CPU-intensive to compile an expression into a function, and then run that function against every entity that the context has cached, just to find out whether the item is there or not. If you then find out that it's not there, and you have to go back to the database anyway, you just wasted time and resources.



来源:https://stackoverflow.com/questions/32276711/how-do-i-cache-lookup-data-with-entity-framework-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!