Skip to content

Commit

Permalink
fix: cache middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
ItzNotABug committed Oct 5, 2024
1 parent 305b4f0 commit d804d57
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
18 changes: 8 additions & 10 deletions middlewares/api-cache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,21 @@ npm install @itznotabug/appexpress-apicache
```javascript
// import
import AppExpress from '@itznotabug/appexpress';
import apiCache from '@itznotabug/appexpress-apicache';
import * as cache from '@itznotabug/appexpress-apicache';

// setup
const express = new AppExpress();

// set options
const cacheModule = apiCache({
express.middleware(cache.createApiCache({
excludes: ['/admin'],
timeout: 1000 * 60 * 5 // 5 minutes, use 0 for no expiry!
});
express.middleware(cacheModule);
timeout: 1000 * 60 * 5 // 5 minutes, use 0 for no expiry!
}));
```

### Excluding a particular request -

```javascript
express.get('/user/payment', async (req, res) => {
express.get('/user/paymentMethods', async (req, res) => {
const user = await sdk.getUser(req);
const paymentMethods = await sdk.getPaymentMethods(user);

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

```javascript
express.get('/search/results', async (req, res) => {
if (cacheModule.hasCache(req.url)) {
if (cache.hasCache(req.url)) {
res.empty();
return;
}
Expand All @@ -81,8 +79,8 @@ express.get('/search/results', async (req, res) => {
### Clear a cache for url or all cache

```javascript
cacheModule.clearCache(url);
cache.clearCache(url);

// remove all
cacheModule.clearAllCache();
cache.clearAllCache();
```
44 changes: 21 additions & 23 deletions middlewares/api-cache/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ const configOptions = {
expirationTime: 1000 * 60 * 5,
};

/**
* Check if a given URL has a cached response in memory.
*
* @param {string} url - The URL to check for a cache response.
* @returns {boolean} True if a memory cache exists for the URL, false otherwise.
*/
export const hasCache = (url) => !!memoryCache.get(url);

/**
* Clear cache for a given url.
*
* @param {string} url - The URL for which to remove the cache.
*/
export const clearCache = (url) => memoryCache.remove(url);

/**
* Clears all cached responses in the memory.
*/
export const clearAllCache = () => memoryCache.clear();

/**
* Middleware that serves cached responses.
*
Expand All @@ -20,9 +40,8 @@ const configOptions = {
* Caching won't be applied if a path matches any one in excluded paths.
* @param {number} [timeout=300000] - Cache expiry in milliseconds. Default 5 minutes. Pass `0` for no expiry!
* @param {boolean} [cacheControl=true] - Should add a `cache-control` header. Default true. This header is not overridden if one already exists.
* @returns {{ hasCache: function, clearCache: function, clearAllCache: function }}
*/
export default function ({
export function createApiCache({
excludes = [],
timeout = 300000,
cacheControl = true,
Expand All @@ -32,27 +51,6 @@ export default function ({
configOptions.cacheControl = cacheControl;

return {
/**
* Check if a given URL has a cached response in memory.
*
* @param {string} url - The URL to check for a cache response.
* @returns {boolean} True if a memory cache exists for the URL, false otherwise.
*/
hasCache: (url) => !!memoryCache.get(url),

/**
* Clear cache for a given url.
*
* @param {string} url - The URL for which to remove the cache.
*/
clearCache: (url) => memoryCache.remove(url),

/**
* Clears all cached responses in the memory.
*/
clearAllCache: () => memoryCache.clear(),

// internal middleware methods.
incoming: (request, response) => {
serveCacheIfAvailable(request, response);
},
Expand Down
2 changes: 1 addition & 1 deletion middlewares/api-cache/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@itznotabug/appexpress-apicache",
"version": "0.0.4",
"version": "0.0.5",
"description": "A api caching middleware for AppExpress.",
"author": "@itznotabug",
"type": "module",
Expand Down

0 comments on commit d804d57

Please sign in to comment.