Spring Boot on GCP
  • Introduction
  • Getting Started
    • Google Cloud Platform
    • Cloud Shell
    • gcloud CLI
    • Hello World!
      • Cloud Shell
      • App Engine
      • Cloud Run
      • Kubernetes Engine
      • Compute Engine
      • Cloud Functions
  • Application Development
    • Development Tools
    • Spring Cloud GCP
    • Cloud Services
      • Databases
        • Cloud SQL
        • Cloud Spanner
        • Cloud Firestore
          • Datastore Mode
          • Native Mode
      • Messaging
        • Cloud Pub/Sub
        • Kafka
      • Secret Management
      • Storage
      • Cache
        • Memorystore Redis
        • Memorystore Memcached (beta)
      • Other Services
    • Observability
      • Trace
      • Logging
      • Metrics
      • Profiling
      • Debugging
    • DevOps
      • Artifact Repository
  • Deployment
    • Runtime Environments
    • Container
      • Container Image
      • Secure Container Image
      • Container Awareness
      • Vulnerability Scanning
      • Attestation
    • Kubernetes
      • Kubernetes Cluster
      • Deployment
      • Resources
      • Service
      • Health Checks
      • Load Balancing
        • External Load Balancing
        • Internal Load Balancing
      • Scheduling
      • Workload Identity
      • Binary Authorization
    • Istio
      • Getting Started
      • Sidecar Proxy
  • Additional Resources
    • Code Labs
    • Presentations / Videos
    • Cheat Sheets
Powered by GitBook
On this page
  • Memorystore Memcached Instance
  • Enable API
  • Enable Private Service Access
  • Create an Instance
  • Get Instance IP Address
  • Connect to Instance
  • Spring Boot Cache
  • Dependency
  • Configuration
  • Enable Caching
  • Cacheable

Was this helpful?

  1. Application Development
  2. Cloud Services
  3. Cache

Memorystore Memcached (beta)

PreviousMemorystore RedisNextOther Services

Last updated 4 years ago

Was this helpful?

Memorystore Memcached Instance

Enable API

gcloud services enable servicenetworking.googleapis.com
gcloud services enable memcache.googleapis.com

Enabling this API may take a few minutes.

Enable Private Service Access

Memorystore Memcached requires Private Services Access to be enabled. See documentation for more information.

Reserve an IP address range to be used in a VPC, so that the Memcached instance's IP address can be allocated within this range:

gcloud beta compute addresses create reserved-range \
  --global --prefix-length=24 \
  --description=description --network=default \
  --purpose=vpc_peering

This is a simplified range creation on the default VPC network. In a production environment, you should verify what the range should be and which VPC network to allocate in.

Establish peering so that Memorystore can allocate the IP address in the reserved range in the VPC.

gcloud services vpc-peerings connect \
  --service=servicenetworking.googleapis.com \
  --ranges=reserved-range --network=default

Create an Instance

Create an instance and attach it to the default VPC.

gcloud beta memcache instances create orders-cache \
  --node-count=1 --node-cpu=1 --node-memory=1G --region=us-central1

Creating a Memcached instance may take a few minutes.

Get Instance IP Address

gcloud beta memcache instances describe orders-cache \
  --region=us-central1 --format="value(memcacheNodes.host)"

The IP address is not a static IP address. If you create the instance, the IP address may be different.

Connect to Instance

Computing Environment

Compute Engine

Kubernetes Engine

App Engine Flexible

App Engine Standard

Cloud Run

Cloud Function

You can test quickly by creating a Compute Engine instance in a zone within the same region:

gcloud compute instances create test-memorystore-vm --zone=us-central1-c

SSH into the machine:

gcloud compute ssh test-memorystore-vm --zone=us-central1-c

Install redis-cli:

sudo apt-get update && sudo apt-get install -y telnet

Connect to the instance:

telnet <MEMORYSTORE_MEMCACHED_IP> 11211

You can try different Memcached commands, for example, stats:

Trying ...
Connected to 10.111.98.4.
Escape character is '^]'.
stats
STAT pid 1
STAT uptime 1020
STAT time 1594348128
...
END
quit
Connection closed by foreign host.

Spring Boot Cache

Spring Boot does not have a built-in Memcached support. However you can use a 3rd party Memcached starter to provide Spring Boot cache support, e.g.:

Dependency

Add the 3rd party Memcached Spring Boot starter:

<dependency>
    <groupId>io.sixhours</groupId>
    <artifactId>memcached-spring-boot-starter</artifactId>
    <version>2.1.2</version>
</dependency>
compile group: 'io.sixhours', name: 'memcached-spring-boot-starter:2.1.2'

Configuration

Configure the Memcached instance to connect to:

application.properties
memcached.cache.servers=<MEMORYSTORE_MEMCACHED_IP>:11211
memcached.cache.provider=static

Enable Caching

Turn on caching capability explicitly with the @EnableCaching annotation:

@SpringBootApplication
@EnableCaching
class DemoApplication {
  ...
}

Cacheable

Once you configured the Spring Boot with Redis and enabled caching, you can use the @Cacheable annotation to cache return values.

@Service
class OrderService {
  private final OrderRepository orderRepository;
  
  public OrderService(OrderRepository orderRepository) {
    this.orderRepository = orderRepository;
  }
  
  @Cacheable("order")
  public Order getOrder(Long id) {
    orderRepository.findById(id);
  }
}

See to see how to connect to a Memorystore instance from different computing environments.

See for more information.

Establishing a private services access connection
Memcached commands
https://github.com/sixhours-team/memcached-spring-boot
Guide
Guide
Additional Configuration
VPC Service Connector
VPC Service Connector
VPC Service Connector
Memorystore connectivity options