fix: rss & sitemap cache

This commit is contained in:
Innei
2021-09-23 18:42:53 +08:00
parent 0238a059b0
commit a6d08122ae
4 changed files with 16 additions and 10 deletions

View File

@@ -13,9 +13,10 @@ export enum RedisItems {
Ips = 'ips',
}
export const CacheKeys = Object.freeze({
AggregateCatch: 'mx-api-cache:aggregate_catch',
SiteMapCatch: 'mx-api-cache:aggregate_sitemap_catch',
RSS: 'mx-api-cache:rss',
RSSCatch: 'mx-api-cache:rss_catch',
} as const)
export enum CacheKeys {
AggregateCatch = 'mx-api-cache:aggregate_catch',
SiteMapCatch = 'mx-api-cache:aggregate_sitemap_catch',
SiteMapXmlCatch = 'mx-api-cache:aggregate_sitemap_xml_catch',
RSS = 'mx-api-cache:rss',
RSSXmlCatch = 'mx-api-cache:rss_xml_catch',
}

View File

@@ -18,7 +18,7 @@ export class FeedController {
) {}
@Get('/')
@CacheKey(CacheKeys.RSSCatch)
@CacheKey(CacheKeys.RSSXmlCatch)
@CacheTTL(3600)
@HTTPDecorators.Bypass
@Header('content-type', 'application/xml')
@@ -27,18 +27,20 @@ export class FeedController {
await this.aggregateService.buildRssStructure()
const { title } = this.configs.get('seo')
const { avatar } = await this.configs.getMaster()
const now = new Date()
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>${title}</title>
<link href="/atom.xml" rel="self"/>
<link href="/feed" rel="self"/>
<link href="${xss(url)}"/>
<updated>${new Date().toISOString()}</updated>
<updated>${now.toISOString()}</updated>
<id>${xss(url)}</id>
<author>
<name>${author}</name>
</author>
<generator>${'Mix Space CMS'}</generator>
<lastBuildDate>${now.toISOString()}</lastBuildDate>
<language>zh-CN</language>
<image>
<url>${xss(avatar)}</url>

View File

@@ -1,7 +1,8 @@
import { CacheTTL, Controller, Get, Header } from '@nestjs/common'
import { CacheKey, CacheTTL, Controller, Get, Header } from '@nestjs/common'
import { minify } from 'html-minifier'
import { HTTPDecorators } from '~/common/decorator/http.decorator'
import { ApiName } from '~/common/decorator/openapi.decorator'
import { CacheKeys } from '~/constants/cache.constant'
import { AggregateService } from '../aggregate/aggregate.service'
@Controller('sitemap')
@ApiName
@@ -10,6 +11,7 @@ export class SitemapController {
@Get('/')
@CacheTTL(3600)
@CacheKey(CacheKeys.SiteMapXmlCatch)
@HTTPDecorators.Bypass
@Header('content-type', 'application/xml')
async getSitemap() {

View File

@@ -51,9 +51,10 @@ export class CacheService {
public clearAggregateCache() {
return Promise.all([
this.redisClient.del(CacheKeys.RSS),
this.redisClient.del(CacheKeys.SiteMapCatch),
this.redisClient.del(CacheKeys.RSSXmlCatch),
this.redisClient.del(CacheKeys.AggregateCatch),
this.redisClient.del(CacheKeys.SiteMapCatch),
this.redisClient.del(CacheKeys.SiteMapXmlCatch),
])
}
}