Memorystore Redis

Memorystore Redis Instance

Enable API

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

Create an Instance

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

Get Instance IP Address

1
gcloud redis instances describe orders-cache \
2
--region=us-central1 --format="value(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
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:
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 redis-tools
Copied!
Connect to the instance:
1
redis-cli -h <MEMORYSTORE_REDIS_IP>
Copied!
You can try different Redis commands, for example:
1
> PING
2
PONG
3
> SET greeting Hello
4
OK
5
> GET greeting
6
"Hello"
Copied!
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
1
<dependency>
2
<groupId>org.springframework.cloud</groupId>
3
<artifactId>spring-boot-starter-cache</artifactId>
4
</dependency>
5
<dependency>
6
<groupId>org.springframework.boot</groupId>
7
<artifactId>spring-boot-starter-data-redis</artifactId>
8
</dependency>
Copied!
1
compile group: 'org.springframework.cloud', name: 'spring-boot-starter-cache'
2
compile group: 'org.springframework.cloud', name: 'spring-boot-starter-data-redis'
Copied!

Configuration

Configure the Redis instance to connect to:
application.properties
1
spring.redis.host=<MEMORYSTORE_REDIS_IP>
2
3
# Configure default TTL, e.g., 10 minutes
4
spring.cache.redis.time-to-live=600000
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!
Read Spring Boot documentation on Cacheable and Redis for more information.

Spring Boot Session

Dependency

Add the Spring Data Spanner starter:
Maven
Gradle
1
<dependency>
2
<groupId>org.springframework.cloud</groupId>
3
<artifactId>spring-session-data-redis</artifactId>
4
</dependency>
5
<dependency>
6
<groupId>org.springframework.boot</groupId>
7
<artifactId>spring-boot-starter-data-redis</artifactId>
8
</dependency>
Copied!
1
compile group: 'org.springframework.cloud', name: 'spring-session-data-redis'
2
compile group: 'org.springframework.cloud', name: 'spring-boot-starter-data-redis'
Copied!

Configuration

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

Enable HTTP Session

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

Samples

Last modified 1yr ago