如何使用Entity Framework 6缓存查找数据 [英] How do I cache lookup data with Entity Framework 6

查看:53
本文介绍了如何使用Entity Framework 6缓存查找数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有关EF的方法,而且我知道对于州,国家/地区等而言,缓存要比往返DB来回快.但是我不确定如何实现它.我在看这篇文章(来自本地缓存的实体),但是我应该利用内置的东西吗?

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:

我现在正在考虑一个GetCached函数,该函数将使用泛型在内存中保存一些查找列表.这样的事情:

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,
}

但是上述解决方案使我无法输入Enumerable.我希望能够以某种方式指定类型,或者更好地进行某种扩展.

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.

推荐答案

有很多选项,但是如果用户主要查询的是相同的几个国家/地区代码,这是一种很好的方法:

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:

创建 MemoryCache实例,用作类中的静态,私有,只读字段.在您的方法中,如果有一个以给定 countryCode 作为键的项,则尝试从此缓存中获取一个缓存项.如果不存在,请进行数据库查询,然后将结果添加到缓存中,然后再返回.

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.

顺便说一句,您链接的文章中的方法可能不是一个很好的方法:只有在您要处理的特定数据库上下文实例中已经有数据的情况下,它才有帮助.最适合您的情况是短暂的.将一个表达式编译成一个函数,然后针对上下文已缓存的每个实体运行该函数,以找出该项目是否存在,这确实是CPU密集型的.如果随后发现它不存在,并且无论如何都必须返回数据库,那只会浪费时间和资源.

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.

这篇关于如何使用Entity Framework 6缓存查找数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆