Skip to content

Commit 13865b4

Browse files
authored
Merge pull request #17 from ItzNotABug/fix-cache-middleware
Fix: Cache Middleware
2 parents 305b4f0 + d804d57 commit 13865b4

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

middlewares/api-cache/README.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,21 @@ npm install @itznotabug/appexpress-apicache
2323
```javascript
2424
// import
2525
import AppExpress from '@itznotabug/appexpress';
26-
import apiCache from '@itznotabug/appexpress-apicache';
26+
import * as cache from '@itznotabug/appexpress-apicache';
2727

2828
// setup
2929
const express = new AppExpress();
3030

31-
// set options
32-
const cacheModule = apiCache({
31+
express.middleware(cache.createApiCache({
3332
excludes: ['/admin'],
34-
timeout: 1000 * 60 * 5 // 5 minutes, use 0 for no expiry!
35-
});
36-
express.middleware(cacheModule);
33+
timeout: 1000 * 60 * 5 // 5 minutes, use 0 for no expiry!
34+
}));
3735
```
3836

3937
### Excluding a particular request -
4038

4139
```javascript
42-
express.get('/user/payment', async (req, res) => {
40+
express.get('/user/paymentMethods', async (req, res) => {
4341
const user = await sdk.getUser(req);
4442
const paymentMethods = await sdk.getPaymentMethods(user);
4543

@@ -64,7 +62,7 @@ express.get('/user/code', async (req, res) => {
6462

6563
```javascript
6664
express.get('/search/results', async (req, res) => {
67-
if (cacheModule.hasCache(req.url)) {
65+
if (cache.hasCache(req.url)) {
6866
res.empty();
6967
return;
7068
}
@@ -81,8 +79,8 @@ express.get('/search/results', async (req, res) => {
8179
### Clear a cache for url or all cache
8280

8381
```javascript
84-
cacheModule.clearCache(url);
82+
cache.clearCache(url);
8583

8684
// remove all
87-
cacheModule.clearAllCache();
85+
cache.clearAllCache();
8886
```

middlewares/api-cache/cache.js

+21-23
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ const configOptions = {
1010
expirationTime: 1000 * 60 * 5,
1111
};
1212

13+
/**
14+
* Check if a given URL has a cached response in memory.
15+
*
16+
* @param {string} url - The URL to check for a cache response.
17+
* @returns {boolean} True if a memory cache exists for the URL, false otherwise.
18+
*/
19+
export const hasCache = (url) => !!memoryCache.get(url);
20+
21+
/**
22+
* Clear cache for a given url.
23+
*
24+
* @param {string} url - The URL for which to remove the cache.
25+
*/
26+
export const clearCache = (url) => memoryCache.remove(url);
27+
28+
/**
29+
* Clears all cached responses in the memory.
30+
*/
31+
export const clearAllCache = () => memoryCache.clear();
32+
1333
/**
1434
* Middleware that serves cached responses.
1535
*
@@ -20,9 +40,8 @@ const configOptions = {
2040
* Caching won't be applied if a path matches any one in excluded paths.
2141
* @param {number} [timeout=300000] - Cache expiry in milliseconds. Default 5 minutes. Pass `0` for no expiry!
2242
* @param {boolean} [cacheControl=true] - Should add a `cache-control` header. Default true. This header is not overridden if one already exists.
23-
* @returns {{ hasCache: function, clearCache: function, clearAllCache: function }}
2443
*/
25-
export default function ({
44+
export function createApiCache({
2645
excludes = [],
2746
timeout = 300000,
2847
cacheControl = true,
@@ -32,27 +51,6 @@ export default function ({
3251
configOptions.cacheControl = cacheControl;
3352

3453
return {
35-
/**
36-
* Check if a given URL has a cached response in memory.
37-
*
38-
* @param {string} url - The URL to check for a cache response.
39-
* @returns {boolean} True if a memory cache exists for the URL, false otherwise.
40-
*/
41-
hasCache: (url) => !!memoryCache.get(url),
42-
43-
/**
44-
* Clear cache for a given url.
45-
*
46-
* @param {string} url - The URL for which to remove the cache.
47-
*/
48-
clearCache: (url) => memoryCache.remove(url),
49-
50-
/**
51-
* Clears all cached responses in the memory.
52-
*/
53-
clearAllCache: () => memoryCache.clear(),
54-
55-
// internal middleware methods.
5654
incoming: (request, response) => {
5755
serveCacheIfAvailable(request, response);
5856
},

middlewares/api-cache/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@itznotabug/appexpress-apicache",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "A api caching middleware for AppExpress.",
55
"author": "@itznotabug",
66
"type": "module",

0 commit comments

Comments
 (0)