Google Cloud Memcache API Client Library

raw JSON →
1.15.0 verified Tue May 12 auth: no python install: verified quickstart: verified

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.

pip install google-cloud-memcache
error ModuleNotFoundError: No module named 'google.cloud.memcache'
cause The `google-cloud-memcache` library or its specific sub-module is not installed or accessible in the Python environment where the code is being run.
fix
Ensure the library is installed using pip: pip install google-cloud-memcache.
error telnet: Unable to connect to remote host: Connection refused
cause The client application cannot establish a network connection to the Memcached instance, often due to incorrect IP address/port, firewall rules blocking access, or the Memcached instance not being in a 'READY' state or not reachable from the client's network.
fix
Verify the Memcached instance's IP address and port, check VPC network peering and firewall rules to ensure they allow traffic, and confirm the instance is in a 'READY' state and located in the same region and network as the client.
error ERROR: (gcloud.memcache.instances.create) { "code": 9, "message": "Unable to create instance. The allocated private IP address space is exhausted." }
cause The private services access connection for your VPC network has exhausted its allocated IP address range, preventing the creation of new Memcached instances.
fix
Allocate additional IP addresses to your private services access connection. Refer to Google Cloud documentation on expanding IP address allocation for private services access.
error Unable to create instance. Enable private service access for the authorized network and try again.
cause A private services access connection has not been established or is incorrectly configured for the network where you are attempting to create the Memorystore for Memcached instance.
fix
Establish a private services access connection for your project's network by following the setup instructions in the Google Cloud documentation.
error Permission denied
cause The principal (user account or service account) attempting to perform an action on the Memcached instance or interact with the API lacks the necessary Identity and Access Management (IAM) permissions.
fix
Grant the required IAM roles to the principal for the Memcached instance or project. Common roles include 'Cloud Memorystore Memcached Editor' or more granular permissions like memcache.instances.get for read access.
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.
fix Plan to migrate existing Memcached instances to Memorystore for Valkey. For new projects, avoid creating Memorystore for Memcached instances. Consult the official Google Cloud documentation for migration guides.
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.
fix For Python 3 App Engine, use the `google-cloud-memcache` library to interact with a Memorystore for Memcached instance, or switch to Memorystore for Valkey. Do not expect `google.appengine.api.memcache` to work in Python 3.
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.
fix Run `gcloud auth application-default login` in your terminal or provide a service account key JSON file via `os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/path/to/key.json'`.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 1.67s 69.2M
3.10 alpine (musl) - - 1.69s 68.1M
3.10 slim (glibc) wheel 6.6s 1.10s 67M
3.10 slim (glibc) - - 0.99s 66M
3.11 alpine (musl) wheel - 2.35s 73.9M
3.11 alpine (musl) - - 2.57s 72.7M
3.11 slim (glibc) wheel 5.4s 1.54s 72M
3.11 slim (glibc) - - 1.49s 70M
3.12 alpine (musl) wheel - 2.43s 65.3M
3.12 alpine (musl) - - 2.57s 64.2M
3.12 slim (glibc) wheel 4.9s 1.82s 63M
3.12 slim (glibc) - - 1.93s 62M
3.13 alpine (musl) wheel - 2.19s 65.0M
3.13 alpine (musl) - - 2.50s 63.8M
3.13 slim (glibc) wheel 4.6s 1.80s 63M
3.13 slim (glibc) - - 2.10s 62M
3.9 alpine (musl) wheel - 1.56s 69.2M
3.9 alpine (musl) - - 1.47s 68.2M
3.9 slim (glibc) wheel 7.1s 1.24s 67M
3.9 slim (glibc) - - 1.07s 66M

This quickstart demonstrates how to instantiate the `CloudMemcacheClient` and list existing Memcached instances within a specified Google Cloud project and location. Ensure your `GOOGLE_CLOUD_PROJECT` environment variable is set and you've authenticated with `gcloud auth application-default login` or equivalent service account credentials. The Memcache API must also be enabled for your project.

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.")