Google Cloud Memcache API Client Library
The `google-cloud-memcache` client library for Python provides programmatic access to the Google Cloud Memorystore for Memcached API. It allows developers to manage and interact with fully-managed Memcached instances on Google Cloud. The library is currently at version 1.15.0 and is actively maintained as part of the broader `googleapis/google-cloud-python` monorepo, with frequent updates.
Warnings
- breaking Google Cloud Memorystore for Memcached is being deprecated and will be shut down on January 31, 2029. After February 1, 2027, you cannot create new Memorystore for Memcached instances in new projects. Migration to Memorystore for Valkey (a Redis-compatible service) is strongly recommended for existing and new workloads.
- gotcha The `google-cloud-memcache` library is distinct from the legacy App Engine `google.appengine.api.memcache` service. The App Engine Memcache service is NOT available for Python 3 runtimes. If you are developing a Python 3 App Engine application and need caching, you must use the `google-cloud-memcache` client library to connect to a provisioned Memorystore for Memcached instance (or preferably Memorystore for Valkey due to deprecation) via Serverless VPC Access.
- gotcha Standard Google Cloud authentication is required. If running locally, ensure you've authenticated via `gcloud auth application-default login` or set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to a service account key file. Permissions to manage Memcached instances are also required.
Install
-
pip install google-cloud-memcache
Imports
- CloudMemcacheClient
from google.cloud.memcache_v1 import CloudMemcacheClient
Quickstart
import os
from google.cloud.memcache_v1 import CloudMemcacheClient
# Set your Google Cloud Project ID and a region where Memcached instances exist or can be created.
# For example, 'us-central1' or 'europe-west1'.
project_id = os.environ.get('GOOGLE_CLOUD_PROJECT', 'your-project-id')
location_id = 'us-central1' # e.g., 'us-central1'
try:
# Initialize a client
client = CloudMemcacheClient()
# The parent resource for all instances, in the format: `projects/{project_id}/locations/{location_id}`
parent = f"projects/{project_id}/locations/{location_id}"
# List Memcached instances in the specified location
print(f"Listing Memcached instances in {parent}:")
request = {"parent": parent}
for instance in client.list_instances(request=request):
print(f" Instance: {instance.name}, State: {instance.state.name}")
print("Successfully listed Memcached instances.")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure you have authenticated with `gcloud auth application-default login` ")
print("and that the Memcache API is enabled for your project.")
print(f"You can set GOOGLE_CLOUD_PROJECT and optionally GOOGLE_APPLICATION_CREDENTIALS environment variables.")