Memorystore Memcached (beta)

Memorystore Memcached Instance

Enable API

1
gcloud services enable servicenetworking.googleapis.com
2
gcloud services enable memcache.googleapis.com
Copied!
Enabling this API may take a few minutes.

Enable Private Service Access

Memorystore Memcached requires Private Services Access to be enabled. See Establishing a private services access connection 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:
1
gcloud beta compute addresses create reserved-range \
2
--global --prefix-length=24 \
3
--description=description --network=default \
4
--purpose=vpc_peering
Copied!
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.
1
gcloud services vpc-peerings connect \
2
--service=servicenetworking.googleapis.com \
3
--ranges=reserved-range --network=default
Copied!

Create an Instance

Create an instance and attach it to the default VPC.
1
gcloud beta memcache instances create orders-cache \
2
--node-count=1 --node-cpu=1 --node-memory=1G --region=us-central1
Copied!
Creating a Memcached instance may take a few minutes.

Get Instance IP Address

1
gcloud beta memcache instances describe orders-cache \
2
--region=us-central1 --format="value(memcacheNodes.host)"
Copied!
The IP address is not a static IP address. If you create the instance, the IP address may be different.

Connect to Instance

See Memorystore connectivity options to see how to connect to a Memorystore instance from different computing environments.
Computing Environment
Compute Engine
Guide
Kubernetes Engine
Guide
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:
1
gcloud compute instances create test-memorystore-vm --zone=us-central1-c
Copied!
SSH into the machine:
1
gcloud compute ssh test-memorystore-vm --zone=us-central1-c
Copied!
Install redis-cli:
1
sudo apt-get update && sudo apt-get install -y telnet
Copied!
Connect to the instance:
1
telnet <MEMORYSTORE_MEMCACHED_IP> 11211
Copied!
You can try different Memcached commands, for example, stats:
1
Trying ...
2
Connected to 10.111.98.4.
3
Escape character is '^]'.
4
stats
5
STAT pid 1
6
STAT uptime 1020
7
STAT time 1594348128
8
...
9
END
10
quit
11
Connection closed by foreign host.
Copied!
See Memcached commands for more information.

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:
Maven
Gradle
1
<dependency>
2
<groupId>io.sixhours</groupId>
3
<artifactId>memcached-spring-boot-starter</artifactId>
4
<version>2.1.2</version>
5
</dependency>
Copied!
1
compile group: 'io.sixhours', name: 'memcached-spring-boot-starter:2.1.2'
Copied!

Configuration

Configure the Memcached instance to connect to:
application.properties
1
memcached.cache.servers=<MEMORYSTORE_MEMCACHED_IP>:11211
2
memcached.cache.provider=static
Copied!

Enable Caching

Turn on caching capability explicitly with the @EnableCaching annotation:
1
@SpringBootApplication
2
@EnableCaching
3
class DemoApplication {
4
...
5
}
Copied!

Cacheable

Once you configured the Spring Boot with Redis and enabled caching, you can use the @Cacheable annotation to cache return values.
1
@Service
2
class OrderService {
3
private final OrderRepository orderRepository;
4
5
public OrderService(OrderRepository orderRepository) {
6
this.orderRepository = orderRepository;
7
}
8
9
@Cacheable("order")
10
public Order getOrder(Long id) {
11
orderRepository.findById(id);
12
}
13
}
Copied!