feat(medusa): Cache modules (#3187)

This commit is contained in:
Frane Polić
2023-03-10 15:09:26 +01:00
committed by GitHub
parent f43f03badb
commit f97b3d7cce
42 changed files with 783 additions and 186 deletions
+13 -1
View File
@@ -3,13 +3,25 @@ const DB_USERNAME = process.env.DB_USERNAME
const DB_PASSWORD = process.env.DB_PASSWORD
const DB_NAME = process.env.DB_TEMP_NAME
const redisUrl = process.env.REDIS_URL || "redis://localhost:6379"
module.exports = {
plugins: [],
projectConfig: {
redis_url: process.env.REDIS_URL,
redis_url: redisUrl,
database_url: `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME}`,
database_type: "postgres",
jwt_secret: "test",
cookie_secret: "test",
},
modules: {
cacheService: {
resolve: "@medusajs/cache-inmemory",
// don't set cache since this is shared between tests
// and since we have "test-product" / "test-variant" as ids
// in a bunch of tests, this could cause that incorrect data is returned
// (e.g. price selection caches calculations under `ps:${variantId}`)
options: { ttl: 0 },
},
},
}
+1
View File
@@ -9,6 +9,7 @@
"build": "babel src -d dist --extensions \".ts,.js\""
},
"dependencies": {
"@medusajs/cache-inmemory": "*",
"@medusajs/medusa": "*",
"faker": "^5.5.3",
"medusa-interfaces": "*",
@@ -23,7 +23,6 @@ module.exports = ({ cwd, redisUrl, uploadDir, verbose, env }) => {
COOKIE_SECRET: "test",
REDIS_URL: redisUrl ? redisUrlWithDatabase : undefined, // If provided, will use a real instance, otherwise a fake instance
UPLOAD_DIR: uploadDir, // If provided, will be used for the fake local file service
CACHE_TTL: 0, // By default the cache service is disabled and 0 means that none of the cache key/value will be stored.
...env,
},
stdio: verbose
-58
View File
@@ -1,58 +0,0 @@
const path = require("path")
const Redis = require("ioredis")
const { GenericContainer } = require("testcontainers")
require("dotenv").config({ path: path.join(__dirname, "../.env") })
const workerId = parseInt(process.env.JEST_WORKER_ID || "1")
const DB_USERNAME = process.env.DB_USERNAME || "postgres"
const DB_PASSWORD = process.env.DB_PASSWORD || ""
const DbTestUtil = {
db_: null,
setDb: function (connection) {
this.db_ = connection
},
clear: async function () {
/* noop */
},
teardown: async function () {
/* noop */
},
shutdown: async function () {
/* noop */
// TODO: stop container
},
}
const instance = DbTestUtil
module.exports = {
initRedis: async function ({ cwd }) {
// const configPath = path.resolve(path.join(cwd, `medusa-config.js`))
// const { projectConfig } = require(configPath)
const container = await new GenericContainer("redis")
.withExposedPorts(6379)
.start()
const redisClient = new Redis({
host: container.getHost(),
port: container.getMappedPort(6379),
db: workerId,
})
instance.setDb(redisClient)
return redisClient
},
useRedis: function () {
return instance
},
}
@@ -39,5 +39,9 @@ module.exports = {
resources: "shared",
resolve: "@medusajs/inventory",
},
cacheService: {
resolve: "@medusajs/cache-inmemory",
options: { ttl: 5 },
},
},
}
+1
View File
@@ -9,6 +9,7 @@
"build": "babel src -d dist --extensions \".ts,.js\""
},
"dependencies": {
"@medusajs/cache-inmemory": "*",
"@medusajs/medusa": "*",
"faker": "^5.5.3",
"medusa-fulfillment-webshipper": "*",