I serailizing/caching of queries possible?

Jul 3, 2011 at 6:30 PM

Hello !

Thanks, that you gave the world this project !

In my usage though, I am very intgerested to cache the results. Many, mayn queries are repeated
over and over again and my goal is to cache the query and the result. I am not in-depth with Linq
though. From your examples, would it be possible to cache the query? I mentioned my small
internal kownledge about Linq, but I would assume, that - from your sample:

dynamic result = context
.Query("CN=Users,CN=Employees,DC=Northwind,DC=local", SearchScope.OneLevel)
.Where("cn=Andrew Fuller")
.Select("objectguid")
.FirstOrDefault();

var guid = new Guid(result.ObjectGuid as byte[]);

var user = context.Query<User>()
.Where(u => u.ObjectGuid == guid)
.FirstOrDefault();

Console.WriteLine(user.CommonName); //Query runs here?

the query for the user will finally run, in the marked line !??
For this case, the "user" would represent the expression to execute.
If this is the case, I could just cache the expression as a key and
the result as the value [not clear in the moment, if I can use
"memcached" or db4o to implement such a cache].

A small answer would be really great!

Thanks a lot!

br++mabra

Coordinator
Jul 8, 2011 at 5:07 AM
Edited Jul 8, 2011 at 5:08 AM

Thanks for the support!  I tried to stick as close as possible to the traditional LINQ behavior.  There are two ways to trigger query execution:

  • Immediate results triggered by the methods First, FirstOrDefault, Single, SingleOrDefault, Any, Count, ToPage, and PageAll.
  • The first time the query is enumerated

So in my example FirstOrDefault will trigger execution right then.  

I haven't thought a lot about caching results, but that is a great idea.  I think the most reliable key for a query would probably be a hash of the selected attributes, the filter, and the SearchScope.  Caching the expression may have unexpected results since part of the expression tree includes the originating method.  If the query is executed from two different methods the resulting expressions will not be equal even if they produce the exact same LDAP filters.  I will bump this up on the features list for 3.0, but if you would like to tackle it now you're more than welcome.  Thanks again for the feedback!

 

public void Main()
{
    //Filters are identical, but expressions are not
    Method1();
    Method2();
}

public void Method1()
{
   var query = context.Query<User>()
      .Where(u => u.CommonName == "John Doe");
}

public void Method2()
{
   var query = context.Query<User>()
      .Where(u => u.CommonName == "John Doe");
}