This directory holds the code for the Memcached Tutorial.
You can either:
- install and use it as a Medusa application;
- or copy its source files into an existing Medusa application.
- Node.js v20+
- Git CLI
- PostgreSQL
- Memcached server
- Caching feature flag enabled. Set the following environment variable:
MEDUSA_FF_CACHING=true- Clone the repository and change to the
memcached-cachingdirectory:
git clone https://github.com/medusajs/examples.git
cd examples/memcached-caching2. Rename the .env.template file to .env.
3. If necessary, change the PostgreSQL username, password, and host in the DATABASE_URL environment variable.
4. Set the Memcached environment variable:
MEMCACHED_SERVERS=Where MEMCACHED_SERVERS is a comma-separated list of Memcached servers to connect to. Locally, it should be 127.0.0.1:11211 or localhost:11211.
5. Install dependencies:
yarn # or npm install6. Setup and seed the database:
npx medusa db:setup
yarn seed # or npm run seed7. Start the Medusa application:
yarn dev # or npm run devYou'll see the following message in the terminal, indicating that Medusa connected to Memcached:
info: Connecting to Memcached...
info: Successfully connected to MemcachedYou can set other options as defined in the ModuleOptions type.
If you have an existing Medusa application, copy the src/modules/memcached directory to your application.
Then, add the Memcached Module Provider with the Caching Module to medusa-config.ts:
module.exports = defineConfig({
// ...
modules: [
{
resolve: "@medusajs/medusa/caching",
options: {
in_memory: {
enable: true
},
providers: [
{
resolve: "./src/modules/memcached",
id: "caching-memcached",
// Optional, makes this the default caching provider
is_default: true,
options: {
serverUrls: process.env.MEMCACHED_SERVERS?.split(',') ||
["127.0.0.1:11211"],
// add other optional options here...
},
},
// other caching providers...
],
}
}
],
})Next, add the following environment variable:
MEMCACHED_SERVERS=Where MEMCACHED_SERVERS is a comma-separated list of Memcached servers to connect to. Locally, it should be 127.0.0.1:11211 or localhost:11211.
After that, install the memjs package:
yarn add memjs # or npm install memjsThis guide was implemented with
algoliasearch@^5.21.0.
Finally, start the Medusa application:
yarn dev # or npm run devYou'll see the following message in the terminal, indicating that Medusa connected to Memcached:
info: Connecting to Memcached...
info: Successfully connected to MemcachedYou can set other options as defined in the ModuleOptions type.