当前位置:网站首页>The out of the box caching function of angular server-side rendering applications
The out of the box caching function of angular server-side rendering applications
2022-06-21 15:38:00 【WangZiXi】
About passing Angular Universal Rendered page source code , We have two caching methods :
- HTTP cache
When using network cache , It's all about setting the correct response headers on the server . They specify cache lifetime and cache policy . An example is as follows :
Cache-Control: max-age = 31536000
This option is applicable to unauthorized areas and the presence of long-term unchanging data .
- In Memory cache
The memory cache can be used for rendering pages and API request . Both use cases through the development kit @ngx-ssr/cache Provide .
take NgxSsrCacheModule Module added to AppModule Caching API Request and on the server in the browser .
maxSize Property is responsible for the maximum cache size . value 50 Indicates that the cache will contain more than 50 The last... From the application GET request .
maxAge Property is responsible for the cache lifecycle . Specifies... In milliseconds .
The usage code is as follows :
import {
BrowserModule } from '@angular/platform-browser';
import {
NgModule } from '@angular/core';
import {
AppComponent } from './app.component';
import {
NgxSsrCacheModule } from '@ngx-ssr/cache';
import {
environment } from '../environments/environment';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
NgxSsrCacheModule.configLruCache({
maxAge: 10 * 60_000, maxSize: 50 }),
],
bootstrap: [AppComponent],
})
export class AppModule {
}
for example , Same package @ngx-ssr/cache There is a sub module for everything in @ngx-ssr/cache/express. It imports a withCache function . This function is the wrapper of the rendering engine . How to use it is as follows :
import {
ngExpressEngine } from '@nguniversal/express-engine';
import {
LRUCache } from '@ngx-ssr/cache';
import {
withCache } from '@ngx-ssr/cache/express';
server.engine(
'html',
withCache(
new LRUCache({
maxAge: 10 * 60_000, maxSize: 100 }),
ngExpressEngine({
bootstrap: AppServerModule,
})
)
);
Although you can render static websites from the server , But this approach has many limitations , Including code duplication and lack of flexibility —— Especially when reading data from a database . Fortunately, ,Express.js Provides a way to create a dynamic template from a server-side application through a template engine HTML Page method .
The template engine works in a fairly simple way : Create a template , And pass the variable to it using the appropriate syntax . then , On the appropriate path of the rendering template , Assign values to variables declared in the template file . These are compiled in real time when the template is rendered .
A basic feature of template engines is that they allow us to create reusable components called parts , These components can be reused in other files . This helps prevent code duplication and makes changes easier to implement .
There are a variety of template engines that can work with Express Use it together . Express The default template engine in is Jade, Now known as Pug. however ,Express Installed by default in Jade Still use the old version .
边栏推荐
- Kubeneters installation encountered gcr The IO image warehouse cannot be pulled
- Jason Basics
- Angular 服务器端渲染应用的开箱即用的缓存功能问题
- C multithreading
- [leetcode] sum of two numbers - go language solution
- Several common implementation methods of mock interface test
- [Yugong series] February 2022 wechat applet -app Debug JSON configuration attribute
- New project template of punctual atom F103 based on firmware library
- GO语言-指针
- Select article (040) - what is the log output when you click this paragraph?
猜你喜欢

Tomb. Weekly update of Finance (February 14-20)

Design of LVDS interface based on FPGA

ABP Framework 5.3.0 版本新增功能和变更说明

Promotion guide for large enterprises: material preparation, PPT writing and on-site defense

Build an efficient and scalable result cache

What is Objective-C ID in swift- What is the equivalent of an Objective-C id in Swift?

Niuke - real exercise-01

Several common implementation methods of mock interface test

Redis introduction and Practice (with source code)

Gather high-quality ar application developers, and help the AR field prosper with technology
随机推荐
Promotion guide for large enterprises: material preparation, PPT writing and on-site defense
Fluent encapsulates an immersive navigation bar with customizable styles NavigationBar
Daily practice (23): the first character that appears only once
Rk3399 platform development series explanation (network debugging) 7.32. What is network routing?
What is the server room
Tomb. Weekly update of Finance (February 14-20)
Kubeneters installation encountered gcr The IO image warehouse cannot be pulled
【PyTorch基础教程29】DIN模型
For the first time in China, Tsinghua and other teams won the wsdm2022 only best paper award, and Hong Kong Chinese won the "time test Award"
微服务架构带来的分布式单体
Je ne veux pas ouvrir un compte en ligne.
2020-11-12 meter skipping
Stm32l431 immediate sleep mode (code + explanation)
What is a good product for children's serious illness insurance? Please recommend it to a 3-year-old child
What is Objective-C ID in swift- What is the equivalent of an Objective-C id in Swift?
‘maxflow‘ has no attribute ‘Graph‘
Native JS routing, iframe framework
小蓝做实验(统计质数个数)
Several common implementation methods of mock interface test
Gold, silver and four interviews are necessary. The "brand new" assault on the real topic collection has stabilized Alibaba Tencent bytes