Memorystore Redis

Memorystore Redis Instance

Enable API

gcloud services enable servicenetworking.googleapis.com
gcloud services enable redis.googleapis.com
Enabling this API may take a few minutes.

Create an Instance

Create an instance and attach it to the default VPC.
gcloud redis instances create orders-cache \
--size=1 --region=us-central1
Creating a Redis instance may take a few minutes.

Get Instance IP Address

gcloud redis instances describe orders-cache \
--region=us-central1 --format="value(host)"
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
Guide
App Engine Standard
Guide
Cloud Run
Guide
Cloud Function
Guide
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 redis-tools
Connect to the instance:
redis-cli -h <MEMORYSTORE_REDIS_IP>
You can try different Redis commands, for example:
> PING
PONG
> SET greeting Hello
OK
> GET greeting
"Hello"
See redis-cli documentation for more information.

Spring Boot Cache

Spring Boot can use Redis to cache with annotations.

Dependency

Add the Spring Data Redis starter:
Maven
Gradle
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
compile group: 'org.springframework.cloud', name: 'spring-boot-starter-cache'
compile group: 'org.springframework.cloud', name: 'spring-boot-starter-data-redis'

Configuration

Configure the Redis instance to connect to:
application.properties
spring.redis.host=<MEMORYSTORE_REDIS_IP>
# Configure default TTL, e.g., 10 minutes
spring.cache.redis.time-to-live=600000

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);
}
}
Read Spring Boot documentation on Cacheable and Redis for more information.

Spring Boot Session

Dependency

Add the Spring Data Spanner starter:
Maven
Gradle
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
compile group: 'org.springframework.cloud', name: 'spring-session-data-redis'
compile group: 'org.springframework.cloud', name: 'spring-boot-starter-data-redis'

Configuration

Configure the Redis instance to connect to:
application.properties
spring.redis.host=<MEMORYSTORE_REDIS_IP>
# Configure default TTL, e.g., 10 minutes
spring.cache.redis.time-to-live=600000

Enable HTTP Session

Turn on caching capability explicitly with the @EnableSpringHttpSession annotation:
@SpringBootApplication
@EnableSpringHttpSession
class DemoApplication {
...
}

Samples