New Posts New Posts RSS Feed: CacheStateManager
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

CacheStateManager

 Post Reply Post Reply
Author
bassvix View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Jul-2010
Location: Brazil
Posts: 7
Post Options Post Options   Quote bassvix Quote  Post ReplyReply Direct Link To This Post Topic: CacheStateManager
    Posted: 20-Aug-2010 at 10:57am
I found the ultimate solution ...

IEntityQuery QueryCache = (Activator.CreateInstance (typeof (EntityQuery <>). MakeGenericType (new Type [] () MyEntityType)) the IEntityQuery);
queryCache.QueryStrategy = QueryStrategy.CacheOnly;
queryCache.EntityManager = MyEntityManager;
queryCache.ExecuteAsync (). Completed + = new EventHandler <EntityQueriedEventArgs> (QueryCache_Completed);


QueryCache_Completed void (object sender, EntityQueriedEventArgs e)
(
  
... error handling ...

  
e.Results <<<contains only instances of the type MyEntityType cache (not search the database)
Back to Top
bassvix View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Jul-2010
Location: Brazil
Posts: 7
Post Options Post Options   Quote bassvix Quote  Post ReplyReply Direct Link To This Post Posted: 20-Aug-2010 at 8:05am
I found a solution, but still with an error, use. With (QueryStrategy.CacheOnly) and ran to fetch only the entities in the cache, but my second problem is that the IEntityQuery used for the search is based on a PassthruEsqlQuery.

TheDynamicTypeVar is a variable of type Type. (Type of entity basis)

This does not work: (error: Must Be FetchStrategy is PassthruEsqlQuery DatasourceOnly)
new PassthruEsqlQuery (TheDynamicTypeVar, "SELECT VALUE FROM YOU AS TheEntitySetName TE). With (QueryStrategy.CacheOnly);

This would work (not possible in my implementation):

query = new EntityQuery <T> (InstanceOfTheDynamicType.EntityAspect.EntitySetName, MyEntityManager). With (QueryStrategy.CacheOnly);

It also:

query = new EntityQuery <MyEntityType> (InstanceOfTheDynamicType.EntityAspect.EntitySetName, MyEntityManager). With (QueryStrategy.CacheOnly);

Already this does NOT work:

query = new EntityQuery <TheDynamicTypeVar> (InstanceOfTheDynamicType.EntityAspect.EntitySetName, MyEntityManager). With (QueryStrategy.CacheOnly);

Using a variable (TheDynamicTypeVar) to replace the generic type T in new EntityQuery <T>?

Thank you.
Back to Top
bassvix View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Jul-2010
Location: Brazil
Posts: 7
Post Options Post Options   Quote bassvix Quote  Post ReplyReply Direct Link To This Post Posted: 20-Aug-2010 at 7:04am
Good morning.

I could not understand the solution to the problem. I'm using Silverlight with DevForce 6.0.4.

I also need to restore entities that are in the cache after executing asynchronously - InvokeServerMethodAsync () for a method that returns a EntityCacheState.

void AsyncMethod_Completed (InvokeServerMethodOperation args)
(
  
... error handling ...

  
MyEntityManager.CacheStateManager.RestoreCacheState (args.Result the EntityCacheState);
...

Now at this point MyEntityManager contains entities that were returned, but can not access them.

Non-public property (MyEntityManager.CacheStateManager.GetCacheState (). EntitiesToSerialize). _items
displays the entity while debugging.

Since I am using Silverlight can not run MyEntityManager.ExecuteQuery (), and when I try MyEntityManager.ExecuteQueryAsync () the query is not executed in the cache, but in the database.

Please help me?

Could show in-line code?

Thank you.
Back to Top
mtx_252 View Drop Down
Newbie
Newbie


Joined: 11-Aug-2009
Posts: 8
Post Options Post Options   Quote mtx_252 Quote  Post ReplyReply Direct Link To This Post Posted: 20-Oct-2009 at 12:06pm
ok, have resolved the problem. Turns out to be PEBKAC ( Problem Exists Between Keyboard And Chair ). I had a separate entity manager which had a reference to the EntityManager from the main class. Turns out I was creating the EntityManager class and setting it's local EM to a version of the EM before I restored it from Cache so of course none of the Entities were available. I have since modified the code to make sure I pass in an updated EM anytime I restore from cache or refresh it with any new objects.
Thanks
Back to Top
mtx_252 View Drop Down
Newbie
Newbie


Joined: 11-Aug-2009
Posts: 8
Post Options Post Options   Quote mtx_252 Quote  Post ReplyReply Direct Link To This Post Posted: 20-Oct-2009 at 11:23am
yes i load my EntityManager will the Entities from the web service call. I want to then cache the EntityManager in Isolated Storage ( Silverlight's own storage mechanism ) so that next time I start my Silverlight app, I check the cache and restore the EntityManager from the cache. This results in a much faster startup time. However what I am finding is that I can't seem to query any of the Entities from this cached version. If I connect to the server and reload all my Entities from the server then everything works. This, however, defeats the purpose of caching the EM. Here is the code : ( The message box does appear however I can't seem to query the objects in the cache ). I have added a sample query at the very end
 

private void load_EntityManager()

{

if (ISManager.check_StoreFile(cENTITYMANAGER))

{

try

{

context.CacheStateManager.RestoreCacheState((Stream)ISManager.get_ISStream(cENTITYMANAGER), RestoreStrategy.Normal, true);

MessageBox.Show("Restored from Cache");

EntityManager_Updated(this);

//Connect();

}

catch (Exception ex)

{

//if we had an error deserializing the Entity Manager then delete the

//Isolated Storage object and connect

ISManager.delete_StoreFile(cENTITYMANAGER);

Connect();

}

}

else

{

Connect();

}

}

 

sample query

/// <summary>

///

/// </summary>

/// <param name="ci"></param>

/// <returns></returns>

public String get_WOMSItemXMLData(String wiUID)

{

WOMSItem_XMLData wixd = entityData.WOMSItem_XMLDatas

.Where(c => c.WOMSItem_UID.ToString() == wiUID).FirstOrDefault();

if (wixd != null)

return wixd.WOMSItem_Data;

else

return null;

}

Back to Top
GregD View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 374
Post Options Post Options   Quote GregD Quote  Post ReplyReply Direct Link To This Post Posted: 19-Oct-2009 at 5:42pm
I'm a little confused about what the exact issues are at this point.  If I understand you, you want to be able, during an application session, to save some collection of cache contents to a local EntityState disk file. At app startup (or some other time), you will load data from the disk file if there is one; otherwise, you'll load the same collection of data from the database.

You say that if you load from the database you can query the EntityManager's cache successfully; but if you load from the local EntityState file you can't? That doesn't make a lot of sense to me as is, because the state of the EntityManager cache is the state of the EntityManager cache, no matter how it got to that state; and if the entities you want are there, they're there, no matter how they got there. So there must be something else going on. How exactly does GetAll() go about asking for the data it wants?

To answer your other question: an EntityManager cache can be converted to an EntityCacheState object which is serializable. So you can populate an EntityManager's cache in a server-side method that returns an EntityCacheState object populated from that server-side EM. Then client-side you can import the EntityCacheState contents into the cache managed by the client-side EntityManager.


Edited by GregD - 19-Oct-2009 at 5:44pm
Back to Top
mtx_252 View Drop Down
Newbie
Newbie


Joined: 11-Aug-2009
Posts: 8
Post Options Post Options   Quote mtx_252 Quote  Post ReplyReply Direct Link To This Post Posted: 19-Oct-2009 at 1:46pm
a further update.
 
I have tried to cache my entities separately but it looks like any data structures using Domain Entities are not serializable. Is there any way I can serialize my EntityManager or Entity Data without waiting for my client to connect to the BOS and retrieving all the entities each time
 
Thx
Back to Top
mtx_252 View Drop Down
Newbie
Newbie


Joined: 11-Aug-2009
Posts: 8
Post Options Post Options   Quote mtx_252 Quote  Post ReplyReply Direct Link To This Post Posted: 19-Oct-2009 at 1:18pm
I may have spoken too quickly on this matter. Here is what I am trying to accomplish
 
1) Connect to my data source and retrieve all Entities. This should only happen if no cached EntityManager
2) Next time I connect I restore my EntityManager by retrieving cached version from IsolatedStorage ( this is Silverlight application )
3) I now want to query my Entities using the cached version.
4) I originally thought the problem was from using ExecuteQueryAsync and switched to ExecuteQuery. However this only works if I have already retrieved all my Entities and not with the cached version.
 
Is this a problem with Silverlight or am I missing something here?
Back to Top
GregD View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 374
Post Options Post Options   Quote GregD Quote  Post ReplyReply Direct Link To This Post Posted: 16-Oct-2009 at 11:48am
Glad you found a solution, mtx, and thanks also for posting the resolution!
Back to Top
mtx_252 View Drop Down
Newbie
Newbie


Joined: 11-Aug-2009
Posts: 8
Post Options Post Options   Quote mtx_252 Quote  Post ReplyReply Direct Link To This Post Posted: 15-Oct-2009 at 1:40pm
I have resolved the problem. Turns out using ExecuteQueryAsync for both the initial query and against the restored cache does not work. I switched to using ExecuteQuery for the restored cached instance and now all objects are being returned
Back to Top
mtx_252 View Drop Down
Newbie
Newbie


Joined: 11-Aug-2009
Posts: 8
Post Options Post Options   Quote mtx_252 Quote  Post ReplyReply Direct Link To This Post Posted: 15-Oct-2009 at 12:34pm
I want to be able to save and restore the EntityManager cache using Isolated Storage in Silverlight. I seem to be able to save and restore the cache but when I query any of the entities I don't get any items returned. Here is the code
 

private void load_EntityManager()

{

if (ISManager.check_StoreFile(cENTITYMANAGER))

{

context.CacheStateManager.RestoreCacheState((Stream)ISManager.get_ISStream(cENTITYMANAGER), RestoreStrategy.Normal, true);

pHDBOObj.ChannelItem_States = ChannelItem_State.GetAll(context, this);

MessageBox.Show(pHDBOObj.ChannelItem_States.Count.ToString());

}

the EntityManager is restored but the ChannelItem_States list has 0 elements whereas when I am connected and execute the ChannelItem_State.GetAll(context, this) query it has the correct 5 items. I save the Cache after all queries are performed.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down