private xxx _info;
        public static object lockInfo;
        private static DateTime beginTime;
        public xxx Info
        {
            get
            {
                TimeSpan timespan = DateTime.Now - beginTime;
                if (_info == null || timespan.Days >= 30)
                {
                    lock (lockInfo)
                    {
                        if (_info == null || timespan.Days >= 30)
                        {
                            beginTime = DateTime.Now;
                            _info = DataContext.info.where(p => p.id == id).FirstOrDefault();
                        }
                    }
                }
                return _info;
            }
        }

解决方案 »

  1.   

    把info静态化。。而且每过30天会自动更新
      

  2.   

    这里不应该在ef上做缓存,而是asp.net应用层就可以做缓存。http://www.cnblogs.com/fqw1987815/archive/2010/09/25/1834444.html
      

  3.   

    这个连接我看过,在应用层上做缓存,可是针对函数方法怎么作缓存呢?
    也就是我把他做成一个方法函数,然后把这个函数方法标识成缓存,不知怎么写了...我用的是MVC,但MVC的缓存只可以对ActionResult才有效。
      

  4.   

    可以用 Second Level Cache,https://efcache.codeplex.com/
      

  5.   

    感觉可以,可是用EntityFramework.Cache一查没啥东西,新出来的?不知道用咯..
      

  6.   

    EF和ADO有啥缓存,没有
    asp.net提供缓存的,自己写函数实现,简单一点的
            if( HttpContext.Cache["temp"]==null)
            {
                HttpContext.Cache["temp"]=DataContext.info.where(p => p.id == id).FirstOrDefault();
            }
            return HttpContext.Cache["temp"] as List<Object>();
      

  7.   

    感觉可以,可是用EntityFramework.Cache一查没啥东西,新出来的?不知道用咯..EntityFramework是缓存机制~~~不想说~ 但可以明确的说~他的缓存机制不适合你的情况~
    所以用.net的MemoryCache;http://msdn.microsoft.com/zh-cn/library/system.runtime.caching.memorycache.aspx
    看列子~ 你自己把他封装得更好一点
      

  8.   

    用法和例子可以看:SECOND LEVEL CACHE FOR EF 6.1
    其实就是把https://efcache.codeplex.com/里面的那段代码复制到你的项目中即可,不用其它任何修改。
    搜索的话找“ef second level cache”或“ef二级缓存”
      

  9.   

    用ADD方法将数据信息加入缓存
    //利用Cache.Add()方法加入缓存
     //将数据项目加入缓存
        protected void btnAddCache_Click(object sender, EventArgs e)
        {
            //利用Cache.Add()方法将数据加入缓存
            Cache.Add("Name", txtUserName.Text, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
            Cache.Add("Photo", txtTel.Text, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
            Cache.Add("Position", txtJob.Text, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
            txtMsg.Text = "缓存加入成功!";
        }
     //显示缓存数据
        protected void btnDisplayCache_Click(object sender, EventArgs e)
        {
            IDictionaryEnumerator CacheIDE = Cache.GetEnumerator();//显示缓存数据
            int i = 0;
            string info = null;
            info += "缓存项目数据(Key / Value):" + "<br>";
            while (CacheIDE.MoveNext())//循环输出缓存项目
            {
                info += i.ToString() + ". ";
                info += CacheIDE.Key.ToString() + " : ";
                info += CacheIDE.Value.ToString() + "<br>";
                i++;
            }//CodeGo.net/
            if (Cache["Name"] == null)//判断缓存是否有数据项目
            {
                txtMsg.Text = "缓存内容为Null值!";
            }
            else
            {
                txtMsg.Text = info;
            }
        }