caching question

5 posts, 0 answers
  1. RICHARD FRIEND
    RICHARD FRIEND avatar
    16 posts
    Member since:
    Nov 2008

    Posted 14 Dec 2009 Link to this post

    Hi I have had the L2 cache enabled and have connected sql profiler to my database, i see all the queries going to the db everytime and not being loaded from the cache.

    My question is does the L2 cache work with linq statements ?

    Regards
  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 16 Dec 2009 Link to this post

    Hello RICHARD FRIEND,

    The L2 cache surely works with Linq queries the same way as with OQL. Can you please elaborate a bit on the scenario that you are using? Note that if you want to access objects from the L2 cache, the scope that was used to load these objects should be still alive.

    Here is an example of a cached query:
    static void TestL2Cache()
    {
        IObjectScope scope1 = ObjectScopeProvider1.GetNewObjectScope();
        IObjectScope scope2 = ObjectScopeProvider1.GetNewObjectScope();
     
        List<Customer> list1 = GetCustomers(scope1);
     
        //the database will not be called again
        List<Customer> list2 = GetCustomers(scope2);
     
        scope1.Dispose();
        scope2.Dispose();
    }
     
    static List<Customer> GetCustomers(IObjectScope scope)
    {
        var result = from c in scope.Extent<Customer>()
                     where c.City == "London"
                     select c;
     
        return result.ToList();
    }

    Kind regards,
    Alexander
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
  4. RICHARD FRIEND
    RICHARD FRIEND avatar
    16 posts
    Member since:
    Nov 2008

    Posted 16 Dec 2009 Link to this post

    Hi Im struggling to get this to work as required.

    I have my cache strategy set to all and the L2 cache enabled.

    I currently have a scope that is attached to the HttpContext of each request, this is used for updated.

    I also have a static scope that is used for reading objects only (ii added this since you said it needs to still be alive).

    My query selects from the static scope like this.

     

    return from o in MyStaticExtent where

     

    o.Role.AspnetUsers.Any(m=>m.UserName ==userName )==

    true &&

     

    o.AccessControlKey.ResourceKey ==key

    select o;

    However,

    I would expect the query to run once to the database and select all since my cache strategy is set to 'all', however it passes the where on to the database and calls the db for each subsequent request with a different parameter ...

    How does the cache strategy work, and is it something im doing (the join maybe ?) that is preventing it from working as required...

    Thanks

     

  5. Richard
    Richard avatar
    8 posts
    Member since:
    Sep 2008

    Posted 17 Dec 2009 Link to this post

    I have exact the same problems with L2 caching.
    So I created a test environment using LINQ and OQL Queries returning the same results.

    The result:
    - Usage of L2 caching is independent from the query language.
    - L2 caching depends on the query result

    The following example shows a query with LINQ.
    I executed this query twice with condition 1, twice with condition 2.

    The first condition delivers only a small number of records => L2 caching working on second execution
    The second condition delivers more records, about 1000 => Records are read from the database while 2nd execution

    The query:
    _list = (From p in _scope.Extent(Of Bewohner)() Where p.BewField.Equals("Condition") Select p).ToList

    In backend configuration 2nd level cache = true, cache query results = true, maximum objects in cache = 1000000, maximum queries in cache = 1000

    Regards
    Richard Kling
  6. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 18 Dec 2009 Link to this post

    Hello guys,

    In fact there might be a problem. By default the data is read on chunks of fifty objects so it is necessary to iterate the whole result set in order to load all the chunks in the cache. However if this is already done no additional calls to the database should be performed. It seems queries with more than 50 objects in the result are not cached correctly and we will have to investigate a bit further. We will notify you when we have more details.

    Kind regards,
    Alexander
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top
DevCraft banner