{"id":1180,"date":"2023-07-13T05:00:00","date_gmt":"2023-07-13T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1180"},"modified":"2023-07-12T11:54:50","modified_gmt":"2023-07-12T18:54:50","slug":"using-services-to-cache-data","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/using-services-to-cache-data\/","title":{"rendered":"Using services to cache data"},"content":{"rendered":"\n<p>Yesterday, we saw that <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/lifecycle-of-angular-applications\/\" target=\"_blank\" rel=\"noopener\" title=\"\">services are singletons that are created once for all and stay in memory as long as the application is open<\/a>. <\/p>\n\n\n\n<p>This makes our services the best place to store data that can be shared between multiple different components. Such services can use <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/subject-behaviorsubject-and-replaysubject\/\" target=\"_blank\" rel=\"noopener\" title=\"\">BehaviorSubjects<\/a> or <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/signals-why-and-when-do-we-need-them\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Signals<\/a> to cache that data and replay the last value automatically to any new subscriber (component, directive, pipe, service, etc.). <\/p>\n\n\n\n<p>Here is an example using a signal to store a value and expose it in a read-only fashion:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"671\" height=\"467\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-4.png\" alt=\"\" class=\"wp-image-1183\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-4.png 671w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-4-300x209.png 300w\" sizes=\"auto, (max-width: 671px) 100vw, 671px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>This <a href=\"https:\/\/blog.angulartraining.com\/angular-signals-best-practices-around-exposing-signals-5385452150a1\" target=\"_blank\" rel=\"noopener\" title=\"\">article compares BehaviorSubjects and Signals<\/a> to cache a shareable value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, we saw that services are singletons that are created once for all and stay in memory as long as the application is open. This makes our services the best place to store data that can be shared between multiple different components. Such services can use BehaviorSubjects or Signals to cache that data and replay [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,21],"tags":[],"class_list":["post-1180","post","type-post","status-publish","format-standard","hentry","category-angular","category-architecture"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1180"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1180\/revisions"}],"predecessor-version":[{"id":1185,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1180\/revisions\/1185"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}