Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,28 @@ public class SchoolQuotaCacheManager {
public void cacheSchoolMaxCapacities() {
List<SchoolJpaEntity> schools = schoolRepository.findAll();
for (SchoolJpaEntity school : schools) {
String key = REDIS_KEY_SCHOOL_MAX_CAPACITY + school.getSchoolName();
redisTemplate.opsForValue().set(key, school.getCapacity());
addSchoolMaxCapacity(school.getSchoolName(), school.getCapacity());
}
}

public void cacheSchoolCurrentApplicationCounts() {
List<SchoolApplicationProjection> schoolApplications = schoolRepository.countBySchoolNameGroupBy();
for (SchoolApplicationProjection projection : schoolApplications) {
String key = REDIS_KEY_SCHOOL_CURRENT_APPLICATIONS + projection.schoolName();
redisTemplate.opsForValue().set(key, projection.count());
addSchoolCurrentApplicationCount(projection.schoolName(), projection.count());
}
}

public void addSchoolMaxCapacity(String schoolName, Long capacity) {
String key = REDIS_KEY_SCHOOL_MAX_CAPACITY + schoolName;
redisTemplate.opsForValue().set(key, capacity);
}

public void addSchoolCurrentApplicationCount(String schoolName, Long currentCount) {
String key = REDIS_KEY_SCHOOL_CURRENT_APPLICATIONS + schoolName;
redisTemplate.opsForValue().set(key, currentCount);
}


public Long getSchoolApplicationCounts(String schoolName) {
return redisTemplate.opsForValue()
.get(REDIS_KEY_SCHOOL_CURRENT_APPLICATIONS + schoolName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public AvailableSchoolResponse getAvailableSchools() {
@Transactional
public void registerSchool(SchoolRegistrationRequest request) {
schoolJpaRepository.save(request.toEntity());
schoolQuotaCacheManager.addSchoolMaxCapacity(request.schoolName(), request.capacity());
schoolQuotaCacheManager.addSchoolCurrentApplicationCount(request.schoolName(), 0L);
Comment on lines +42 to +43

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The database save operation and these two Redis cache updates are not atomic. The @Transactional annotation only covers the JPA operation. If the database transaction commits successfully but one of the subsequent Redis operations fails (e.g., if the Redis server is temporarily unavailable), the application's data will be in an inconsistent state. The new school will exist in the database but will be missing from the cache, which could lead to runtime errors or incorrect behavior in parts of the application that rely on this cache data.

To ensure data consistency, it's better to couple the cache update to the transaction's lifecycle. A common pattern in Spring is to use TransactionSynchronizationManager to execute the cache updates only after the database transaction has successfully committed.

Here's an example of how you could apply this pattern:

@Transactional
public void registerSchool(SchoolRegistrationRequest request) {
    schoolJpaRepository.save(request.toEntity());

    TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
        @Override
        public void afterCommit() {
            schoolQuotaCacheManager.addSchoolMaxCapacity(request.schoolName(), request.capacity());
            schoolQuotaCacheManager.addSchoolCurrentApplicationCount(request.schoolName(), 0L);
        }
    });
}

This ensures that the cache is only modified if the database write is successful, preventing data inconsistency.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The value 0L is a "magic number". While its meaning is fairly clear in this context, extracting it into a named constant improves readability and maintainability. If this initial value ever needed to change, using a constant provides a single, clear place for the update.

Consider defining a constant within the SchoolService class and using it here:

private static final long INITIAL_APPLICATION_COUNT = 0L;

}

@Transactional
Expand Down