Google Cloud Memcache API Client Library

1.15.0 · active · verified Sat Mar 28

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

Install

Imports

Quickstart

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

view raw JSON →