Skip to content

Commit

Permalink
refactor: move URL_CACHE to wrangler.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
ngshiheng committed Oct 5, 2022
1 parent 5b87515 commit e7ef0e5
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 8 deletions.
2 changes: 0 additions & 2 deletions src/handlers/createShortUrl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { URL_CACHE } from '../utils/constants'
import { generateUniqueUrlKey } from '../utils/urlKey'

/*
Expand Down Expand Up @@ -43,7 +42,6 @@ export const createShortUrl = async (request, event) => {
}),
{ headers: { 'Content-Type': 'application/json' } }
)

const cache = await caches.open(URL_CACHE)

event.waitUntil(
Expand Down
3 changes: 1 addition & 2 deletions src/middleware/shortUrlCache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { URL_CACHE } from '../utils/constants'

/*
A cache middleware that checks if `originalUrl` is present in the `URL_CACHE` cache.
Expand All @@ -16,6 +14,7 @@ export const shortUrlCacheMiddleware = async (request) => {
})
}

/* eslint-disable no-undef */
const cache = await caches.open(URL_CACHE)
const response = await cache.match(originalUrl)

Expand Down
3 changes: 0 additions & 3 deletions src/utils/constants.js

This file was deleted.

3 changes: 2 additions & 1 deletion src/utils/urlKey.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { customAlphabet } from 'nanoid'
import { ALPHABET } from './constants'

const ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

/*
Generate a unique `urlKey` using `nanoid` package.
Expand Down
3 changes: 3 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ kv_namespaces = [
]

[vars]
URL_CACHE = "apiCache"
URL_EXPIRATION_TTL = 86400

[dev]
Expand All @@ -24,6 +25,7 @@ kv_namespaces = [
{ binding = "URL_DB", id = "bf98832a3f6e45e18f3c39b6db1d329a" },
]
[env.staging.vars]
URL_CACHE = "apiCache"
URL_EXPIRATION_TTL = 86400


Expand All @@ -35,4 +37,5 @@ kv_namespaces = [
{ binding = "URL_DB", id = "709b8cf5ddf74f739f1535c34ff0634b" },
]
[env.production.vars]
URL_CACHE = "apiCache"
URL_EXPIRATION_TTL = 604800

0 comments on commit e7ef0e5

Please sign in to comment.