2

I have a website that customers can log into and buy products from a specific range (based on their customer number). The navbar has a dropdown for product categories, but doesn't show any where there would be no products.

At the minute, the database is queried on each page to populate the product categories menu, but this feels inefficient. I'm now wondering if the list of 'allowed' products should be stored in either cookies or session variables at the beginning of a session.

From what I have gathered from other questions, a session variable would normally be used, but they have warned against storing a lot of data in session variables. In this case, I would be storing a potentially large list of products for each user, so would cookies be better? I wouldn't class a list of products as being particularly sensitive, and I would do a server-side check before placing the order. Or should I stick with the current solution of querying the database each time?

To be clear, I will still store the information in the database as well, this question is only asking about a temporary storage for quick access throughout the session.

I have already looked at the following questions, but I still don't feel like I've quite got the answer for my particular scenario, and the questions that are close haven't mentioned cookies.

Community
  • 1
  • 1
Amy Barrett
  • 564
  • 3
  • 14
  • 27
  • Cache. Always cache. If you're simply trying to avoid making round trips to a database, session and cookies are never appropriate choices. – Chris Pratt Mar 02 '17 at 19:22
  • Please can someone tell me why the down vote? – Amy Barrett Mar 02 '17 at 21:43
  • Not sure, but generally speaking, your question is off topic for Stack Overflow. – Chris Pratt Mar 02 '17 at 22:19
  • @ChrisPratt I was unsure between here and software engineering, but chose here because of the other questions I'd come across. – Amy Barrett Mar 02 '17 at 22:24
  • Haha. Yeah, all of those are off topic as well. Stack Overflow is for questions relating to specific issues encountered during programming: how to complete a specific task, solve an error, etc. This would be more appropriate in Software Engineering, since it's a more abstract design/implementation question. – Chris Pratt Mar 02 '17 at 22:28
  • @ChrisPratt The question has now been put on hold for being opinion-based. Would this also be the case for Software Engineering? If not, is there a way to move the question, or would I have to make a new one? If it would still be too opinion-based, have you (or anyone else) got any suggestions for how to improve the question? – Amy Barrett Mar 03 '17 at 09:51
  • @ChrisPratt She Asked something – Suraj Jain Dec 29 '17 at 11:52

1 Answers1

3

The approach I would take would probably be something like this:

  • Store the product data in a cache that is shared between all customers and use the primary key of each product as (part of) the cache key for each product. The cache strategy used could vary depending on scalability requirements (System.Runtime.Caching vs redis vs file caching). In general, I would probably use a sliding expiration or LRU cache (see this .NET example) for the product data so the most popular products stay cached longer than less popular products. When a request comes for a particular primary key, the cache is checked first for the key and if it returns null, the product is looked up from the database and cache populated before returning the product (see this example).
  • For each customer, store only the primary key and other relevant data (what class of discount they may get, etc.). If the primary key list is small enough, you can probably get by with a cookie (encrypted) for this. If you need this to scale to give each customer more than will fit in a cookie, then query the database for the primary keys.
  • When the customer requests the data, get the list of primary keys first, then use those keys to access the pre-cached product data to build the view. You can store as many different primary key lists as you need to meet your requirements without having to get the product data from the database for every customer or every use case for an individual customer.

Think twice about using session state - the advice there is not to use session state for user profile data.

Of course, the above doesn't take into consideration any other marketing requirements that may be needed. So you may need to adjust this strategy for your needs. For example, if you need to update the product information faster than the cache would normally expire, you might consider an approach that updates the product data in the database first and, if successful, gets a write lock on the cache and updates the cache, too. That would allow for near-real-time cache updates without having to invalidate and reload the cache from the database.

Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212