In my application I have these TypeScript classes called Schedule
and in the UI I would like to be able to represent these objects as natural language descriptions such as "Every 2nd Monday of the month at 6:00 AM".
To calculate these "Schedule Descriptions" I have an Angular service ScheduleDescriptionService
which I call getScheduleDescription()
and I pass it a Schedule
and it returns the description back as a string
.
Unfortunately I learned that this getScheduleDescription()
function is very expensive and it gets called a lot in some parts of the application. What I want to do is to solve the performance issue by caching the descriptions after they're generated so they don't have to be generated every time the UI wants to display them.
The reason I put this function into an Angular service is because I need the function that generates these descriptions to have access to an internationalization service and as far as I know I can't inject an Angular service into a normal TypeScript class like Schedule
otherwise I might have put getScheduleDescription()
into the Schedule
class that way I can just deal with each Schedule
caching its own description.
I can't use an Angular pipe because I need to be able to get the descriptions in my code-behind so I need to somehow manage caching for a lot of schedule descriptions and I can't just handle the caching in any one Angular component because I will need to display these descriptions all over the place in my application. So that leaves me to believe that I have to cache these descriptions in my ScheduleDescriptionService
Angular service.
Is this the right way to go? If so, how do I cache in this situation? If the service lasts the lifetime of my application what if my user looks at a lot of descriptions? Should I clear the cache if they stop looking at a page with schedule descriptions? How do I manage change detection?
Thanks in advance.