-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[nextjs] Add Vercel KV support for editing cache (#1530)
* add kv-focused cache provider
- Loading branch information
1 parent
d036314
commit 48164d4
Showing
8 changed files
with
220 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
packages/create-sitecore-jss/src/templates/nextjs/src/pages/api/editing/data/[key].ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
packages/sitecore-jss-nextjs/src/editing/vercel-editing-data-cache.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { expect, use } from 'chai'; | ||
import * as vercelKv from '@vercel/kv'; | ||
import sinon from 'sinon'; | ||
import { EditingData } from './editing-data'; | ||
import { VercelEditingDataCache } from './vercel-editing-data-cache'; | ||
import sinonChai from 'sinon-chai'; | ||
|
||
use(sinonChai); | ||
const sandbox = sinon.createSandbox(); | ||
|
||
describe('vercel editing data cache', () => { | ||
const setup = (key: string, value: Record<string, unknown> | null) => { | ||
const kvStub: vercelKv.VercelKV = new vercelKv.VercelKV({ | ||
url: 'test', | ||
token: 'test', | ||
}); | ||
sandbox.stub(kvStub, 'set').resolves(); | ||
sandbox | ||
.stub(kvStub, 'get') | ||
.withArgs(key) | ||
.resolves(value); | ||
sandbox.stub(kvStub, 'expire').resolves(); | ||
sandbox.stub(vercelKv, 'createClient').returns(kvStub); | ||
return kvStub; | ||
}; | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('should get entries from storage', async () => { | ||
const key = 'top-secret'; | ||
const expectedResult: EditingData = { | ||
path: '/rome', | ||
language: 'en', | ||
layoutData: { | ||
sitecore: { | ||
route: null, | ||
context: {}, | ||
}, | ||
}, | ||
dictionary: {}, | ||
}; | ||
JSON.stringify(expectedResult); | ||
setup(key, expectedResult); | ||
|
||
const result = await new VercelEditingDataCache('test', 'tset').get(key); | ||
|
||
expect(result as EditingData).to.deep.equal(expectedResult); | ||
}); | ||
|
||
it('should return undefined on cache miss', async () => { | ||
const key = 'no-such-key'; | ||
setup(key, null); | ||
const result = await new VercelEditingDataCache('test', 'tset').get('no-such-key'); | ||
expect(result).to.deep.equal(undefined); | ||
}); | ||
|
||
it('should invalidate entry after get', async () => { | ||
const key = 'top-secret'; | ||
const expectedResult: EditingData = { | ||
path: '/rome', | ||
language: 'en', | ||
layoutData: { | ||
sitecore: { | ||
route: null, | ||
context: {}, | ||
}, | ||
}, | ||
dictionary: {}, | ||
}; | ||
JSON.stringify(expectedResult); | ||
const kvStub = setup(key, expectedResult); | ||
|
||
await new VercelEditingDataCache('test', 'tset').get(key); | ||
|
||
expect(kvStub.expire).to.have.been.calledWith(key, 0); | ||
}); | ||
|
||
it('should put entries into storage', async () => { | ||
const key = 'top-secret'; | ||
const entry: EditingData = { | ||
path: '/rome', | ||
language: 'en', | ||
layoutData: { | ||
sitecore: { | ||
route: null, | ||
context: {}, | ||
}, | ||
}, | ||
dictionary: {}, | ||
}; | ||
const kvStub = setup('key', {}); | ||
|
||
await new VercelEditingDataCache('test', 'tset').set(key, entry); | ||
|
||
expect(kvStub.set).to.have.been.calledWith(key, JSON.stringify(entry)); | ||
}); | ||
|
||
it('should put entries into storage with ttl', async () => { | ||
const key = 'top-secret'; | ||
const entry: EditingData = { | ||
path: '/rome', | ||
language: 'en', | ||
layoutData: { | ||
sitecore: { | ||
route: null, | ||
context: {}, | ||
}, | ||
}, | ||
dictionary: {}, | ||
}; | ||
const kvStub = setup('key', {}); | ||
|
||
await new VercelEditingDataCache('test', 'tset').set(key, entry); | ||
|
||
expect(kvStub.set).to.have.been.calledWith(key, JSON.stringify(entry), { ex: 120 }); | ||
}); | ||
|
||
it('should throw if initialized without API URL and token', () => { | ||
expect(() => new VercelEditingDataCache('', '')).to.throw(); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
packages/sitecore-jss-nextjs/src/editing/vercel-editing-data-cache.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { VercelKV, createClient } from '@vercel/kv'; | ||
import { EditingDataCache } from './editing-data-cache'; | ||
import { EditingData } from './editing-data'; | ||
import { debug } from '@sitecore-jss/sitecore-jss'; | ||
|
||
/** | ||
* Implementation of editing cache for Vercel deployments | ||
* Uses Vercel KV database and client to store data | ||
* Set TTL for cache data in constructor (default: 60 seconds) | ||
*/ | ||
export class VercelEditingDataCache implements EditingDataCache { | ||
protected redisCache: VercelKV; | ||
private defaultTtl = 120; | ||
|
||
/** | ||
* @param {string} redisUrl KV endpoint URL. Usually stored in process.env.KV_REST_API_URL | ||
* @param {string} redisToken KV endpoint tokem. Usually stored in process.env.KV_REST_API_TOKEN | ||
*/ | ||
constructor(redisUrl: string | undefined, redisToken: string | undefined) { | ||
if (!redisUrl || !redisToken) { | ||
throw Error( | ||
'API URL or token are missing, ensure you have set the KV or Upstash storage correctly.' | ||
); | ||
} | ||
this.redisCache = createClient({ | ||
url: redisUrl, | ||
token: redisToken, | ||
}); | ||
} | ||
|
||
set(key: string, editingData: EditingData): Promise<void> { | ||
debug.editing(`Putting editing data for ${key} into redis storage...`); | ||
return new Promise<void>((resolve, reject) => { | ||
this.redisCache | ||
.set(key, JSON.stringify(editingData), { ex: this.defaultTtl }) | ||
.then(() => resolve()) | ||
.catch((err) => reject(err)); | ||
}); | ||
} | ||
|
||
get(key: string): Promise<EditingData | undefined> { | ||
debug.editing(`Getting editing data for ${key} from redis storage...`); | ||
return new Promise<EditingData | undefined>((resolve, reject) => { | ||
this.redisCache | ||
.get(key) | ||
.then((entry) => { | ||
const result = (entry || undefined) as EditingData; | ||
this.redisCache.expire(key, 0).then(() => resolve(result)); | ||
}) | ||
.catch((err) => reject(err)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters