Skip to content

Commit

Permalink
Refactor cache module: Update StudioCMSVirtualCache class and impleme…
Browse files Browse the repository at this point in the history
…nt caching utilities
  • Loading branch information
Adammatthiesen committed Dec 19, 2024
1 parent d20c7ff commit c239d26
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 70 deletions.
13 changes: 3 additions & 10 deletions packages/studiocms_core/src/sdk-utils/cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { sdk } from 'studiocms:config';
import { CMSSiteConfigId, versionCacheLifetime } from '../consts';
import StudioCMSVirtualCache from './cache/StudioCMSVirtualCache';
import studioCMS_SDK from './index';
import type {
Expand All @@ -11,16 +10,10 @@ import type {

export type { STUDIOCMS_SDK_CACHE, PageDataCacheObject, SiteConfigCacheObject, VersionCacheObject };

const { cacheConfig } = sdk;

// Create the virtual cache
const VirtualCache = new StudioCMSVirtualCache(
new Map(),
new Map(),
new Map(),
sdk.cacheConfig,
studioCMS_SDK,
CMSSiteConfigId,
versionCacheLifetime
);
const VirtualCache = new StudioCMSVirtualCache(cacheConfig, studioCMS_SDK);

// Export the cache
export const studioCMS_SDK_Cache: STUDIOCMS_SDK_CACHE = VirtualCache.cacheModule();
Expand Down
107 changes: 47 additions & 60 deletions packages/studiocms_core/src/sdk-utils/cache/StudioCMSVirtualCache.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CMSSiteConfigId, versionCacheLifetime } from '../../consts';
import type {
BaseCacheObject,
CombinedPageData,
Expand All @@ -14,57 +15,34 @@ import type {
import { StudioCMSCacheError } from './StudioCMSCacheError';

/**
* The StudioCMSCache class provides caching functionality for StudioCMS.
* It manages the caching of pages, site configurations, and version information.
* The class interacts with the StudioCMS SDK to fetch and update data from the database.
* The `StudioCMSVirtualCache` class provides caching utilities for the StudioCMS SDK.
* It supports caching for site configurations, versions, and page data.
*
* @class StudioCMSCache
* @property {Map<string, PageDataCacheObject>} pages - A map to store cached page data.
* @property {Map<string, SiteConfigCacheObject>} siteConfig - A map to store cached site configuration data.
* @property {Map<string, VersionCacheObject>} version - A map to store cached version information.
* @property {ProcessedCacheConfig} cacheConfig - Configuration settings for the cache.
* @property {STUDIOCMS_SDK} sdk - The StudioCMS SDK instance for interacting with the database.
* @property {number} CMSSiteConfigId - The ID of the CMS site configuration.
* @property {number} versionCacheLifetime - The lifetime of the version cache in milliseconds.
* @property {string} SiteConfigMapID - The identifier for the site configuration map.
* @property {string} VersionMapID - The identifier for the version map.
* @class
* @classdesc This class handles caching operations for the StudioCMS SDK, including
* fetching, updating, and clearing cache entries for site configurations, versions,
* and page data.
*
* @constructor
* @param {Map<string, PageDataCacheObject>} pagesCacheMap - A map to store cached page data.
* @param {Map<string, SiteConfigCacheObject>} siteConfigCacheMap - A map to store cached site configuration data.
* @param {Map<string, VersionCacheObject>} versionCacheMap - A map to store cached version information.
* @param {ProcessedCacheConfig} cacheConfig - Configuration settings for the cache.
* @param {STUDIOCMS_SDK} studioCMS_SDK - The StudioCMS SDK instance for interacting with the database.
* @param {number} CMSSiteConfigId - The ID of the CMS site configuration.
* @param {number} versionCacheLifetime - The lifetime of the version cache in milliseconds.
* @param {ProcessedCacheConfig} cacheConfig - The configuration for the cache.
* @param {STUDIOCMS_SDK} studioCMS_SDK - The StudioCMS SDK instance.
*/
export class StudioCMSVirtualCache {
private pages: Map<string, PageDataCacheObject>;
private siteConfig: Map<string, SiteConfigCacheObject>;
private version: Map<string, VersionCacheObject>;
private cacheConfig: ProcessedCacheConfig;
private sdk: STUDIOCMS_SDK;
private CMSSiteConfigId: number;
private versionCacheLifetime: number;
private readonly SiteConfigMapID: string = '__StudioCMS_Site_Config';
private readonly VersionMapID: string = '__StudioCMS_Latest_Version';
private readonly StudioCMSPkgId: string = 'studiocms';
private readonly CMSSiteConfigId = CMSSiteConfigId;
private readonly versionCacheLifetime = versionCacheLifetime;

constructor(
pagesCacheMap: Map<string, PageDataCacheObject>,
siteConfigCacheMap: Map<string, SiteConfigCacheObject>,
versionCacheMap: Map<string, VersionCacheObject>,
cacheConfig: ProcessedCacheConfig,
studioCMS_SDK: STUDIOCMS_SDK,
CMSSiteConfigId: number,
versionCacheLifetime: number
) {
this.pages = pagesCacheMap;
this.siteConfig = siteConfigCacheMap;
this.version = versionCacheMap;
private readonly cacheConfig: ProcessedCacheConfig;
private readonly sdk: STUDIOCMS_SDK;

private pages = new Map<string, PageDataCacheObject>();
private siteConfig = new Map<string, SiteConfigCacheObject>();
private version = new Map<string, VersionCacheObject>();

constructor(cacheConfig: ProcessedCacheConfig, studioCMS_SDK: STUDIOCMS_SDK) {
this.cacheConfig = cacheConfig;
this.sdk = studioCMS_SDK;
this.CMSSiteConfigId = CMSSiteConfigId;
this.versionCacheLifetime = versionCacheLifetime;
}

// Misc Utils
Expand All @@ -80,15 +58,24 @@ export class StudioCMSVirtualCache {
return new Date().getTime() - entry.lastCacheUpdate.getTime() > lifetime;
}

/**
* Checks if the cache is enabled based on the cache configuration.
*
* @returns {boolean} True if the cache is enabled, false otherwise.
*/
private isEnabled(): boolean {
return this.cacheConfig.enabled;
}

/**
* Fetches the latest version of the StudioCMS package from the NPM registry.
*
* @returns {Promise<string>} A promise that resolves to the latest version string of the StudioCMS package.
* @throws {StudioCMSCacheError} If there is an error fetching the latest version from NPM.
*/
private async getLatestVersionFromNPM(): Promise<string> {
private async getLatestVersionFromNPM(pkg: string, ver = 'latest'): Promise<string> {
try {
const npmResponse = await fetch('https://registry.npmjs.org/studiocms/latest');
const npmResponse = await fetch(`https://registry.npmjs.org/${pkg}/${ver}`);
const npmData = await npmResponse.json();
return npmData.version as string;
} catch (error) {
Expand Down Expand Up @@ -144,16 +131,16 @@ export class StudioCMSVirtualCache {
*/
public async getVersion(): Promise<VersionCacheObject> {
try {
if (!this.cacheConfig.enabled) {
const version = await this.getLatestVersionFromNPM();
if (!this.isEnabled()) {
const version = await this.getLatestVersionFromNPM(this.StudioCMSPkgId);

return this.versionReturn(version);
}

const latestVersion = this.version.get(this.VersionMapID);

if (!latestVersion || this.isCacheExpired(latestVersion, this.versionCacheLifetime)) {
const version = await this.getLatestVersionFromNPM();
const version = await this.getLatestVersionFromNPM(this.StudioCMSPkgId);

const latestVersion = this.versionReturn(version);

Expand All @@ -177,11 +164,11 @@ export class StudioCMSVirtualCache {
*/
public async updateVersion(): Promise<VersionCacheObject> {
try {
const latestVersion = await this.getLatestVersionFromNPM();
const latestVersion = await this.getLatestVersionFromNPM(this.StudioCMSPkgId);

const newVersion = this.versionReturn(latestVersion);

if (!this.cacheConfig.enabled) {
if (!this.isEnabled()) {
return newVersion;
}

Expand All @@ -201,7 +188,7 @@ export class StudioCMSVirtualCache {
*/
public clearVersion(): void {
// Check if caching is disabled
if (!this.cacheConfig.enabled) {
if (!this.isEnabled()) {
return;
}

Expand All @@ -226,7 +213,7 @@ export class StudioCMSVirtualCache {
*/
public async getSiteConfig(): Promise<SiteConfigCacheObject> {
try {
if (!this.cacheConfig.enabled) {
if (!this.isEnabled()) {
const newSiteConfig = await this.sdk.GET.database.config();

if (!newSiteConfig) {
Expand Down Expand Up @@ -278,7 +265,7 @@ export class StudioCMSVirtualCache {
const returnConfig: SiteConfigCacheObject = this.siteConfigReturn(newSiteConfig);

// Check if caching is disabled
if (!this.cacheConfig.enabled) {
if (!this.isEnabled()) {
// Transform and return the data
return returnConfig;
}
Expand All @@ -303,7 +290,7 @@ export class StudioCMSVirtualCache {
*/
public clearPageById(id: string): void {
// Check if caching is disabled
if (!this.cacheConfig.enabled) {
if (!this.isEnabled()) {
return;
}

Expand All @@ -323,7 +310,7 @@ export class StudioCMSVirtualCache {
*/
public clearPageBySlug(slug: string, pkg: string): void {
// Check if caching is disabled
if (!this.cacheConfig.enabled) {
if (!this.isEnabled()) {
return;
}

Expand Down Expand Up @@ -353,7 +340,7 @@ export class StudioCMSVirtualCache {
*/
public clearAllPages(): void {
// Check if caching is disabled
if (!this.cacheConfig.enabled) {
if (!this.isEnabled()) {
return;
}

Expand All @@ -379,7 +366,7 @@ export class StudioCMSVirtualCache {
public async getAllPages(): Promise<PageDataCacheObject[]> {
try {
// Check if caching is disabled
if (!this.cacheConfig.enabled) {
if (!this.isEnabled()) {
const pages = await this.sdk.GET.database.pages();
return pages.map((page) => this.pageDataReturn(page));
}
Expand Down Expand Up @@ -437,7 +424,7 @@ export class StudioCMSVirtualCache {
public async getPageById(id: string): Promise<PageDataCacheObject> {
try {
// Check if caching is disabled
if (!this.cacheConfig.enabled) {
if (!this.isEnabled()) {
const page = await this.sdk.GET.databaseEntry.pages.byId(id);

if (!page) {
Expand Down Expand Up @@ -483,7 +470,7 @@ export class StudioCMSVirtualCache {
public async getPageBySlug(slug: string, pkg: string): Promise<PageDataCacheObject> {
try {
// Check if caching is disabled
if (!this.cacheConfig.enabled) {
if (!this.isEnabled()) {
const page = await this.sdk.GET.databaseEntry.pages.bySlug(slug, pkg);

if (!page) {
Expand Down Expand Up @@ -539,7 +526,7 @@ export class StudioCMSVirtualCache {
): Promise<PageDataCacheObject> {
try {
// Check if caching is disabled
if (!this.cacheConfig.enabled) {
if (!this.isEnabled()) {
await this.sdk.UPDATE.page(data.pageData);
await this.sdk.UPDATE.pageContent(data.pageContent);

Expand Down Expand Up @@ -595,7 +582,7 @@ export class StudioCMSVirtualCache {
): Promise<PageDataCacheObject> {
try {
// Check if caching is disabled
if (!this.cacheConfig.enabled) {
if (!this.isEnabled()) {
await this.sdk.UPDATE.page(data.pageData);
await this.sdk.UPDATE.pageContent(data.pageContent);

Expand Down

0 comments on commit c239d26

Please sign in to comment.