Explanation
The navigation tree (NT) is loaded from an API (that's not in my control) and it shows nodes that are one of a couple of types each (categories, processes, etc), and the loading process is done every time someone goes to my search page (Search.cshtml
). Note that I created NT from scratch. It's not a plugin of any kind.
For optimisation purposes, I need to cache the whole NT (every node). The cached NT would then be quickly loaded and filtered per user's permissions, when that user gets to the search page.
Now, I have the methods to load all nodes and filter them per user, I also have a model called partialNavigationTree
(pNT) that has 1 method for erasing empty folders, and 3 lists like this List<NavTreeNode>
, where NTN is also a model that stores some info that's needed for rendering the node to search page.
So, the goal is to have a static, thread-safe pNT variable, that is loaded when the web app initializes, that a controller's method could read from. I already have a specific class that would hold this variable called static class TheHelper
.
My plan
In class RouteConfig
, method RegisterRoutes
, I would put the code to load the static variable CachedNavigationTree
(CNT) from TheHelper
class, and then let the controller that shows search page copy the contents of CNT to his local variables, filter the nodes in each node list, and then pass on to the view that local variable. This way I get CNT when the web app starts, and gain speed when loading the NT for each user.
Concerns and questions
- How do I declare the static CNT? Is declearing it like this
public static CachedNavigationTree { get; set; }
going to cause problems when multiple users are trying to read from it at the same time? Is THIS the thread-safe way to protect 1class
variable? - The
RouteConfig.RegisterRoutes
is the "deepest" function I found (that is closest to the web app startup point), so I put my "do this when app starts" code in there. Is there a "deeper" / better place to put this kind of code in? - Since lists of nodes in pNT model are
List<T>
s, how do I go about copying those lists? If we had 2 lists, A and B, and lets say A is full, then type inB = A;
, than the B list's items would only refer to those in list A, which means that changing list Bs items would change list As items as well and vice versa. Is this correct? Should I worry about the way I copy the lists, or is what I said above false, and I can comfortably sayvar localList = TheHelper.CachedNavigationTree;
? - This is the way of caching that I came up with with the knowledge I have. I wouldn't be suprised if there was already a premade way of caching class variables in C#/MVC. If it exists, and you know of it, please elaborate.
EDIT
Question 3 is actualy - how to copy a full list into an empty one, and actualy generate the items from the full list again. Because in C#, when you put List<string> A = new ...; List<string> B = A;
, then when you change (lets say) first item in B list, that item will also change in A list. So there are actualy just 2 names for the same list, not 2 lists in RAM. Hope this clearars question 3. Also, answer's here. Im sorry for the bad explanation.