theme | background | class | highlighter | lineNumbers | info | drawings | transition | title | mdc | |
---|---|---|---|---|---|---|---|---|---|---|
seriph |
text-center |
shikiji |
false |
## Cacheable Redis Spring Presentation
|
|
slide-left |
Cacheable Redis Spring Presentation |
true |
layout: image-right image: https://source.unsplash.com/collection/94734566/1920x1080
- @Cacheable - annotation provided by Spring
- Redis - how is works
layout: image-right image: https://source.unsplash.com/collection/94734566/1920x1080
- @Cacheable
- @CacheEvict
- @CachePut
layout: image-right image: https://source.unsplash.com/collection/94734566/1920x1080
Define a cache for the list of users
@Cacheable(value = "users-cache")
public List<User> getAllUsers() {
return userRepository.findAll();
}
layout: image-right image: https://source.unsplash.com/collection/94734566/1920x1080
Evict cache
@CacheEvict(value = "users-cache", key = "#id")
public User deleteUser(String id) {
return userRepository.deleteById(id);
}
layout: image-right image: https://source.unsplash.com/collection/94734566/1920x1080
Evict cache
@CacheEvict(value="users-cache",allEntries = true)
public List<User> updateUsers(List<User> users) {
return userRepository.saveAll(users);
}
layout: image-right image: https://source.unsplash.com/collection/94734566/1920x1080
Update cache
@CachePut(value = "users-cache", key = "#user.id")
public User updateUser(User user) {
return userRepository.save(user);
}
layout: image-right image: https://source.unsplash.com/collection/94734566/1920x1080
- distributed cache
- key value store
layout: image-right image: https://source.unsplash.com/collection/94734566/1920x1080
- redis docker container
- redis-cli command
layout: image-right image: https://source.unsplash.com/collection/94734566/1920x1080
version: '3.7'
services:
redis:
image: redis:latest
hostname: redis
container_name: redis
restart: always
command: sh -c "redis-server --save 20 1 --requirepass dev"
ports:
- "6379:6379"
And you run docker compose up -d
layout: image-right image: https://source.unsplash.com/collection/94734566/1920x1080
redis-cli -h localhost
# authenticate
localhost:6379> AUTH dev
OK
# get all keys
localhost:6379> KEYS *
1) "users-cache::1"
# get value for cache key
localhost:6379> GET "users-cache::1"
layout: image-right image: https://source.unsplash.com/collection/94734566/1920x1080
- Invocation of cacheable methods from the same class
- evict vs put
- reusing the same cache, but with different response
- use cache keys usage whenever possible