1

What's the best practice for caching data within a request?

More specifically, let's say I a have a table with several rows of data and each row needs to do a separate database query to get details on a Bakery. I know that by the end of the request I will have to get data on each individual Bakery in Boston. I'd like to select all Bakeries I need in one shot before I add data to the table so I have them.

1) If I do BakeriesTableRetrieval.GetRows( new BakeriesTableEqualityCondition.CityId( CityIds.Boston ) ), will database caching magically not have to make a round trip when I later do BakeriesTableRetrieval.GetRowMatchingId( idOfSomeBakeryInBoston )?

2) Assuming 1 is false or that I have a different situation where I just need to cache my own stuff, how do I best keep my own pre-request cache?

William Gross
  • 2,083
  • 2
  • 17
  • 35
Greg Smalter
  • 6,571
  • 9
  • 42
  • 63

1 Answers1

1

My answers below apply when you are using EWL's own web framework (EWF), or if you are otherwise inside a DataAccessState.Current.ExecuteWithCache block.

  1. Yes, database caching will automatically eliminate those single-row queries, as long as you are using Table Retrieval. With Query Retrieval, you can accomplish the same thing if you implement the updateSingleRowCaches partial method.

  2. Use DataAccessState.Current.GetCacheValue.

William Gross
  • 2,083
  • 2
  • 17
  • 35
  • Small wrinkle in why #1 doesn't work. I'm not querying by the primary key, I'm querying by a pair of values that is unique. Does #1 work with complex primary keys? If I made the table have that tuple as its primary key instead of having a separate single one would it help auto-caching? – Greg Smalter Jun 16 '15 at 18:07
  • @GregSmalter: I don't think #1 currently supports complex primary keys. – William Gross Jun 29 '15 at 17:01