This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Normal 0 false false false EN-AU X-NONE X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US;} Hi, I'm trying out lightspeed and trying to understand how the caching works but i'm having a bit of trouble and cannot find much information on it. I've found a couple of pages in the user manual that discuss the first and second level cache and i've found a few posts that refer to the subject but cannot find any more detail. Are you able to point me to some posts or sample code? Alternatively can you help me with the code I am trying out, I have copied it below and in the comments I have indicated where the database is getting hit (determined by running SQL Profiler). I have also indicated where the database is getting hit when I did not expect it to. What i would like to do is understand why the database is getting hit at those points and how to change my caching options so as to prevent these database hits. Thank you very much, kelly Here is my code (I based it on example code in the lighspeed user guide): private LightSpeedContext<LSModelUnitOfWork> _context; //this line causes the database to be hit the first time its run when the app opens //but no more database hits for subsequent calls (as expected) //this line gets the child entity collection (which in the model has Cached=True and Cache Expiry=60, same as CustomerEntity) //it causes the database to be hit every time the line is executed (i would have expected the ChildEntity list to be cached?)
//causes the database to be hit every time the line is executed (not as expected) //causes the database to be hit every time the line is executed (not as expected)
|
|
|
The documentation on caching at http://www.mindscapehq.com/documentation/lightspeed/Performance-and-Tuning/Caching is the best source of information. In your example above, you are expecting that the objects would be cached for lazy load situations, however this is not the case. The caching will try and fetch entities from the cache if they are requested by Id only (the only exception to this is FindAll caching which only applies if you are fetching an entire table which is useful for caching reference data collections). In lazy load situations we need to know what the set looks like at the database first, so we execute the normal query to lazily load the child collection. When we load the entities in from that reader we first check if there is an entity in the cache by checking the Id, if there is then it will be loaded from cache (either Identity Map or 2nd Level). If not then the entity will be loaded and subsequently added to the caches as appropriate. But there will always be database queries executed for lazy loads or if you have a query for cachable entites that you subsequently issue. If you want to have "query caching" where the results of a query are fetched from a cache you will need to implement this manually. We are planning on reviewing this in a future release but for now you will need to implement this yourself. Let us know if anything is unclear or if you have any more questions :)
Jeremy |
|
|
Normal 0 false false false EN-AU X-NONE X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} Thanks very much for your response. I’ve read your response and reread the documentation and also gone back through as many of the posts as I could find that talk about caching. I think I understand it a little better but can you just confirm I’ve understood correctly? Caching will mean that the database does not get queried (unless the specified ID is not already in the cache) if I used Find by ID i.e. var customer = UnitOfWork.FindById<TblCustomer>(123); However, in lazy loading, there will always be database queries executed whenever I try to access the child collections of this entity and this is not something I can change by adjusting properties on the entity – I would need to manually implement caching to achieve this? In addition to Find by ID I can also use FindAll to cache the data? I wasn’t quite clear what you were referring to when you mentioned this but I have searched for information on FindAll and it seems it means to do call Find<Entity Type> without specifying a query – is this correct? E.g. unitOfWork.Find<TblCustomer>().ToList(); And based on some testing it seems if I set CacheFindAllResult to true for the TblCustomer entity then the data will be cached and subsequent calls the Find() will not hit the database. This also appears to be true for unitOfWork.TblCustomers.ToList().OrderBy(c => c.CustomerCode).ToList(); So it seems that I could use this to cache data? But If I do use this it doesn’t seem to load the child collections of the entity I have done the Find() on e.g. running this unitOfWork.Find<TblCustomer>().FirstOrDefault().ChildEntities.Count; will return the correct count for ChildEntities the first time I call it but returns 0 on each subsequent call. Is this also an example of where I would need to implement caching manually in order to retain the child collection values? thank you very much for your help |
|
|
For the FindById and lazy loading yes your understanding is correct. You will need to implement some form of manual caching to cover this if you want to avoid it, however bear in mind that you cant implement anything to override LightSpeeds behavior in regards to lazy loading, we will always need to issue a database query. You could however investigate if eager loading might be appropriate and/or cache the entity once it and its child collection has been loaded for subsequent use in a cache that you are managing. For FindAll, yes that is correct. When you execute unitOfWork.TblCustomers.ToList() or unitOfWork.Find<TblCustomer>() you are asking for all TblCustomers so this is a FindAll query. With your query on ChildEntities, for subsequent loads from the cache in a FindAll situation the entities are not automatically attached to the current UnitOfWork so you will need to attach them to that UnitOfWork using UnitOfWork.Attach() to allow lazy loads to occur. We cant lazy load unless the entity is attached to a UnitOfWork.
Jeremy |
|