在Dlinq缓存 [英] Cache in Dlinq

查看:61
本文介绍了在Dlinq缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要重复执行相同的查询,这些查询按ID返回单个实体,

如:


客户cust =(来自db.Customers中的c

其中c.CustomerID ==" AIRBU"

select c).SingleOrDefault();


DLinq保存跟踪对象列表内部因此客户AIRBU存在于

内存中。


如何强制DLinq查看其列表而不是往返服务器?

我查看了实体框架,但还没有在

EF中找到任何缓存解决方案。


Andrus。

I need to repeatedly execute same queries which returns single entity by id,
like:

Customer cust = (from c in db.Customers
where c.CustomerID=="AIRBU"
select c).SingleOrDefault();

DLinq holds tracked object list internally so customer "AIRBU" exists in
memory.

How to force DLinq to look its list and not to make round trip to server ?
I looked into Entity Framework also but havent found any caching solution in
EF.

Andrus.

推荐答案

" LINQ in Action"声明Single()是一个例外,因为它首先检查缓存,而不是数据库。我还没有尝试过(长期

天......) - 值得一试; -p


(我假设SingleOrDefault没有资格获得此异常

,因为你已经尝试过了...... - 而且它可能需要谓词

完全基于主要关键栏目[s])

Marc
"LINQ in Action" states that Single() is an exception, in that it
checks the cache first, not the database. I haven''t tried it yet (long
day...) - but worth a try ;-p

(I''m assuming that SingleOrDefault doesn''t qualify for this exception
since you''ve tried it... - and it would presumably need the predicate
to be based solely on the primary key column[s])

Marc


OK;当Single()做了其他任何事情而不是点击数据库时,我找不到一个案例...我想知道作者的想法是什么?


无论如何......这是真正的,真正的hacky做的工作;我是

起泡......

使用系统;

使用System.Data.Linq;

使用System.Data.Linq.Mapping;

使用System.Diagnostics;

使用System.Linq;

使用System.Reflection;

使用WindowsFormsApplication4; // ns到DataClasses1DataContext =

Northwind


静态类DataContextExt {

公共静态T GetFromCache< T>(此表< T> Ttable,params

object [] keyValues)其中T:class

{

if(table == null)throw new ArgumentNullException(" table");

返回GetFromCache< T>(table.Context,keyValues);

}

public static T GetFromCache< T>(这个DataContext上下文,params

object [] keyValues)其中T:class

{

//表面活动时待命 - 这很脏,平原简单

//(哦,脆弱,而不是编译时安全,并要求

//足够的信任......你明白了;令人讨厌的令人讨厌的讨厌)

if(context == null)throw new

ArgumentNullException(" context");

const BindingFlags FLAGS = BindingFlags.Instance |

BindingFlags.Public | BindingFlags.NonPublic;

object services = context.GetType()。GetProperty(" Services",

FLAGS).GetValue(context,null);


object [] args = {context.Mapping.GetMetaType(typeof(T)),

keyValues};

类型[] paramTypes = {typeof(MetaType),typeof(object [])};

return(T)services.GetType()。GetMethod(" GetCachedObject",

FLAGS,null ,paramTypes,null).Invoke(services,args);

}

}

静态类程序

{

static void Main()

{

using(DataClasses1DataContext ctx = new

DataClasses1DataContext())

{

//缓存原始

供应商sup = ctx.Suppliers.First();

int id = sup.SupplierID;


//从上下文获取

供应商相同= ctx.GetFromCache<供应商>(id);

Trace.WriteLine(ReferenceEquals(sup,same),Got from

Context );


//从表中获取

供应商再次= ctx.Suppliers.GetFromCache(id);

跟踪。 WriteLine(ReferenceEquals(sup,再次),来自

表);


//并检查如果它不存在会发生什么...

供应商没有= ctx.GetFromCache<供应商>(53);

Trace.WriteLine(ReferenceEquals(nothing,null)," Null when br / >
缺少);

}


}

}
OK; I couldn''t find a single case when Single() did anything other
than hit the database... I wonder what the authors had in mind?

Anyways... here is something truly, truly hacky that does the job; I''m
off to lather...
using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using WindowsFormsApplication4; // ns to DataClasses1DataContext =
Northwind

static class DataContextExt {
public static T GetFromCache<T>(this Table<Ttable, params
object[] keyValues) where T : class
{
if (table == null) throw new ArgumentNullException("table");
return GetFromCache<T>(table.Context, keyValues);
}
public static T GetFromCache<T>(this DataContext context, params
object[] keyValues) where T : class
{
// surfactants on standby - this is dirty, plain and simple
// (oh, and brittle, and not compile-time safe, and requires
// sufficient trust... you get the idea; nasty nasty nasty)
if (context == null) throw new
ArgumentNullException("context");
const BindingFlags FLAGS = BindingFlags.Instance |
BindingFlags.Public | BindingFlags.NonPublic;
object services = context.GetType().GetProperty("Services",
FLAGS).GetValue(context, null);

object[] args = { context.Mapping.GetMetaType(typeof(T)),
keyValues };
Type[] paramTypes = { typeof(MetaType), typeof(object[]) };
return (T)services.GetType().GetMethod("GetCachedObject",
FLAGS, null, paramTypes, null).Invoke(services, args);
}
}
static class Program
{
static void Main()
{
using (DataClasses1DataContext ctx = new
DataClasses1DataContext())
{
// cache the original
Supplier sup = ctx.Suppliers.First();
int id = sup.SupplierID;

// get from the context
Supplier same = ctx.GetFromCache<Supplier>(id);
Trace.WriteLine(ReferenceEquals(sup, same), "Got from
Context");

// get from the table
Supplier again = ctx.Suppliers.GetFromCache(id);
Trace.WriteLine(ReferenceEquals(sup, again), "Got from
Table");

// and check what happens if it isn''t there...
Supplier nothing = ctx.GetFromCache<Supplier>(53);
Trace.WriteLine(ReferenceEquals(nothing, null), "Null when
missing");
}

}
}


Marc,
Marc,

OK;当Single()做了其他任何其他事情而不是点击数据库时,我找不到一个案例......我想知道作者的想法是什么?
OK; I couldn''t find a single case when Single() did anything other
than hit the database... I wonder what the authors had in mind?



MSDN明确指出每个简单查询必须返回结果表单身份

缓存(如果存在),而不是到达数据库。


你知道为什么它不工作吗?

如何以文件化的方式使用身份缓存?

MSDN clearly states that every simple query must return result form identity
cache if it exists, not reaching to database.

Do you have any idea why it is not working ?
How to use identity cache in documented way ?


无论如何......这是真正的,真正的hacky做的工作;我是

起泡......
Anyways... here is something truly, truly hacky that does the job; I''m
off to lather...



你在哪里找到了可以创建这样程序的信息?

我还没有找到有关服务属性和GetCachedObject的任何文档

方法。


Andrus。

Where did you found information which allows to create such program ?
I havent found any documentation on Services property and GetCachedObject
method.

Andrus.


这篇关于在Dlinq缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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