Memorystore Redis Instance
Enable API
Copy 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.
Copy gcloud redis instances create orders-cache \
--size=1 --region=us-central1
Creating a Redis instance may take a few minutes.
Get Instance IP Address
Copy 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
You can test quickly by creating a Compute Engine instance in a zone within the same region:
Copy gcloud compute instances create test-memorystore-vm --zone=us-central1-c
SSH into the machine:
Copy gcloud compute ssh test-memorystore-vm --zone=us-central1-c
Install redis-cli
:
Copy sudo apt-get update && sudo apt-get install -y redis-tools
Connect to the instance:
Copy redis-cli -h <MEMORYSTORE_REDIS_IP>
You can try different Redis commands, for example:
Copy > PING
PONG
> SET greeting Hello
OK
> GET greeting
"Hello"
Spring Boot Cache
Dependency
Add the Spring Data Redis starter:
Maven Gradle
Copy <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>
Copy 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:
Copy 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:
Copy @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.
Copy @Service
class OrderService {
private final OrderRepository orderRepository;
public OrderService(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
@Cacheable("order")
public Order getOrder(Long id) {
orderRepository.findById(id);
}
}
Spring Boot Session
Dependency
Add the Spring Data Spanner starter:
Maven Gradle
Copy <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>
Copy 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:
Copy 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:
Copy @SpringBootApplication
@EnableSpringHttpSession
class DemoApplication {
...
}
Samples