From 9468925f89b5eea24246410db4c632ba6bf8b04c Mon Sep 17 00:00:00 2001 From: KaltrinaKrasniqi Date: Tue, 20 May 2025 04:53:02 +0200 Subject: [PATCH] Added Redis Caching for Authorization Services --- pom.xml | 6 ++++ ...sourceManagementSystemBackApplication.java | 2 ++ .../config/RedisConfig.java | 29 +++++++++++++++++++ .../service/TenantPermissionService.java | 29 +++++++++++++++++++ .../service/tenant/RolePermissionService.java | 6 ++++ .../service/tenant/RoleService.java | 22 ++++++++++++++ .../service/tenant/UserRoleService.java | 4 +++ src/main/resources/application.yml | 8 +++++ 8 files changed, 106 insertions(+) create mode 100644 src/main/java/com/hrms/Human_Resource_Management_System_Back/config/RedisConfig.java diff --git a/pom.xml b/pom.xml index 4c769e4..8045c0f 100644 --- a/pom.xml +++ b/pom.xml @@ -104,6 +104,12 @@ 1.14.0 + + org.springframework.boot + spring-boot-starter-data-redis + + + org.projectlombok diff --git a/src/main/java/com/hrms/Human_Resource_Management_System_Back/HumanResourceManagementSystemBackApplication.java b/src/main/java/com/hrms/Human_Resource_Management_System_Back/HumanResourceManagementSystemBackApplication.java index 8452874..0dc69af 100644 --- a/src/main/java/com/hrms/Human_Resource_Management_System_Back/HumanResourceManagementSystemBackApplication.java +++ b/src/main/java/com/hrms/Human_Resource_Management_System_Back/HumanResourceManagementSystemBackApplication.java @@ -3,9 +3,11 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.transaction.annotation.EnableTransactionManagement; +@EnableCaching @SpringBootApplication @EntityScan(basePackages = { "com.hrms.Human_Resource_Management_System_Back.model", diff --git a/src/main/java/com/hrms/Human_Resource_Management_System_Back/config/RedisConfig.java b/src/main/java/com/hrms/Human_Resource_Management_System_Back/config/RedisConfig.java new file mode 100644 index 0000000..786b7d6 --- /dev/null +++ b/src/main/java/com/hrms/Human_Resource_Management_System_Back/config/RedisConfig.java @@ -0,0 +1,29 @@ +package com.hrms.Human_Resource_Management_System_Back.config; + +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheManager; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializationContext; + +@Configuration +@EnableCaching +public class RedisConfig { + + @Bean + public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) { + RedisSerializationContext.SerializationPair valueSerializationPair = + RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()); + + RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() + .serializeValuesWith(valueSerializationPair); + + return RedisCacheManager.builder(connectionFactory) + .cacheDefaults(config) + .build(); + } +} + diff --git a/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/TenantPermissionService.java b/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/TenantPermissionService.java index 0fb8da0..4c04bd9 100644 --- a/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/TenantPermissionService.java +++ b/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/TenantPermissionService.java @@ -3,8 +3,12 @@ import com.hrms.Human_Resource_Management_System_Back.model.TenantPermission; import com.hrms.Human_Resource_Management_System_Back.repository.TenantPermissionRepository; import lombok.AllArgsConstructor; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; +import java.util.List; + /** * Service class for handling business logic related to tenant permissions. *

@@ -17,6 +21,7 @@ public class TenantPermissionService extends BaseService { private final TenantPermissionRepository repo; + /** * Returns the tenant permission repository. *

@@ -30,4 +35,28 @@ public class TenantPermissionService extends BaseService findAll() { + return super.findAll(); + } + + /** + * Evict cache when a permission is added or updated. + */ + @Override + @CacheEvict(value = "permissions", allEntries = true) + public TenantPermission save(TenantPermission entity) { + return super.save(entity); + } + + /** + * Evict cache when a permission is deleted. + */ + @Override + @CacheEvict(value = "permissions", allEntries = true) + public void deleteById(Integer id) { + super.deleteById(id); + } } diff --git a/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/tenant/RolePermissionService.java b/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/tenant/RolePermissionService.java index c1e92ed..2c9d371 100644 --- a/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/tenant/RolePermissionService.java +++ b/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/tenant/RolePermissionService.java @@ -10,6 +10,8 @@ import com.hrms.Human_Resource_Management_System_Back.repository.tenant.RoleRepository; import com.hrms.Human_Resource_Management_System_Back.service.BaseService; import lombok.AllArgsConstructor; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -72,8 +74,10 @@ protected RolePermissionRepository getRepository() { * @return a list of {@link UserRolePermissionDto} representing the filtered and processed permissions for the user tenant */ @Transactional + @Cacheable(value = "user-role-permissions", key = "#id") public List getUserRolePermissions(Integer id) { List list = repo.findScopedPermissionsByUserTenantId(id); + System.out.println("KALTRINAAAAA?????????!!!!!!!!!"); HashMap hashMap = new HashMap<>(); @@ -130,6 +134,7 @@ private String bytesToHex(byte[] bytes) { * @param roleId the role ID to retrieve permissions for * @return a list of {@link RolePermission} associated with the given role ID */ + @Cacheable(value = "role-permissions", key = "#roleId") public List findByRoleId(Integer roleId) { return repo.findAllByRole_RoleId(roleId); } @@ -145,6 +150,7 @@ public List findByRoleId(Integer roleId) { * @param req the request containing the new permission and target role IDs */ @Transactional + @CacheEvict(value = "user-role-permissions", key = "#roleId") public void replacePermissions(Integer roleId, RolePermissionReplaceRequest req) { repo.deleteByRoleId(roleId); // JPQL @Modifying DELETE, inside Tx diff --git a/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/tenant/RoleService.java b/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/tenant/RoleService.java index f1d9fa6..75c98a0 100644 --- a/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/tenant/RoleService.java +++ b/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/tenant/RoleService.java @@ -4,8 +4,12 @@ import com.hrms.Human_Resource_Management_System_Back.repository.tenant.RoleRepository; import com.hrms.Human_Resource_Management_System_Back.service.BaseService; import lombok.AllArgsConstructor; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; +import java.util.List; + /** * Service class for handling business logic related to roles. *

@@ -35,4 +39,22 @@ public class RoleService extends BaseService { protected RoleRepository getRepository() { return repo; } + + @Override + @Cacheable("roles") + public List findAll() { + return super.findAll(); + } + + @Override + @CacheEvict(value = "roles", allEntries = true) + public Role save(Role entity) { + return super.save(entity); + } + + @Override + @CacheEvict(value = "roles", allEntries = true) + public void deleteById(Integer id) { + super.deleteById(id); + } } diff --git a/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/tenant/UserRoleService.java b/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/tenant/UserRoleService.java index 7d241cd..a669407 100644 --- a/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/tenant/UserRoleService.java +++ b/src/main/java/com/hrms/Human_Resource_Management_System_Back/service/tenant/UserRoleService.java @@ -8,6 +8,8 @@ import com.hrms.Human_Resource_Management_System_Back.service.BaseService; import jakarta.transaction.Transactional; import lombok.AllArgsConstructor; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.List; @@ -75,6 +77,7 @@ public List getUserRoles(Integer userId) { * @param userTenantId the ID of the user tenant whose roles are to be fetched * @return a list of {@link UserRole} associated with the user tenant ID */ + @Cacheable(value = "user-roles", key = "#userTenantId") public List findByUserTenantId(Integer userTenantId) { return repo.findAllByUserTenant_UserTenantId(userTenantId); } @@ -90,6 +93,7 @@ public List findByUserTenantId(Integer userTenantId) { * @param roleIds a list of role IDs to assign to the user tenant */ @Transactional + @CacheEvict(value = "user-roles", key = "#userTenantId") public void replaceRoles(Integer userTenantId, List roleIds) { // Delete existing roles for the user tenant repo.deleteByUserTenantId(userTenantId); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a546fde..00870e7 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -9,6 +9,14 @@ spring: password: 1234 driver-class-name: org.postgresql.Driver + cache: + type: redis + + data: + redis: + host: localhost + port: 6379 + jpa: transaction-manager: transactionManager