I tried CloudMemcacheClient, but it is meant for managing instances:
const {CloudMemcacheClient} = require('@google-cloud/memcache');
const cmc = new CloudMemcacheClient();
How do I use Memcached i.e. set/get/delete values from cloud functions?
As CloudMemcacheClient class is intended for instance management. They are not suitable for direct data operations (setting, getting, and deleting values). To perform data operations within a Memcached instance from Cloud Functions, use the Memcached class from a standard Memcached client library such as memcached, node-memcached, or memjs which are suitable for setting, getting, and deleting keys and values.
To achieve this follow the below steps:
Choose and install a standard Memcached client library of your choice. For example, a popular one is memcached
: using the command npm install memcached
Get your Memcached instance details from the Cloud Console or using the gcloud CLI, such as the instance name, host, and port for each node (or alternatively you can also use Auto Discovery).
Import the installed library and create a client in your Cloud Function, as shown below:
const Memcached =require('memcached');
const client = new Memcached();
client.set()
, client.get()
and client.del()
.Also make sure that you need to use serverless VPC access to connect cloud functions directly to the VPC network which allows access to Memorystore instances with an internal IP address.
Use the Auto Discovery feature for dynamic discovery and management of Memcached nodes. The memjs
library is the best choice for this purpose, as it offers built-in Auto Discovery. It can automatically detect nodes in a Memcached cluster, eliminating the need for manual configuration or environment variables.
Remember to make the adjustment on some specific details as required based on your chosen library and environment.