Skip to content
Open
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@
<version>1.14.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>


<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> valueSerializationPair =
RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer());

RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(valueSerializationPair);

return RedisCacheManager.builder(connectionFactory)
.cacheDefaults(config)
.build();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
Expand All @@ -17,6 +21,7 @@
public class TenantPermissionService extends BaseService<TenantPermission, Integer> {
private final TenantPermissionRepository repo;


/**
* Returns the tenant permission repository.
* <p>
Expand All @@ -30,4 +35,28 @@ public class TenantPermissionService extends BaseService<TenantPermission, Integ
protected TenantPermissionRepository getRepository() {
return repo;
}

@Override
@Cacheable("permissions")
public List<TenantPermission> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<UserRolePermissionDto> getUserRolePermissions(Integer id) {
List<UserRolePermissionDto> list = repo.findScopedPermissionsByUserTenantId(id);
System.out.println("KALTRINAAAAA?????????!!!!!!!!!");

HashMap<String, UserRolePermissionDto> hashMap = new HashMap<>();

Expand Down Expand Up @@ -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<RolePermission> findByRoleId(Integer roleId) {
return repo.findAllByRole_RoleId(roleId);
}
Expand All @@ -145,6 +150,7 @@ public List<RolePermission> 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
Expand Down Expand Up @@ -35,4 +39,22 @@ public class RoleService extends BaseService<Role, Integer> {
protected RoleRepository getRepository() {
return repo;
}

@Override
@Cacheable("roles")
public List<Role> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,6 +77,7 @@ public List<UserRole> 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<UserRole> findByUserTenantId(Integer userTenantId) {
return repo.findAllByUserTenant_UserTenantId(userTenantId);
}
Expand All @@ -90,6 +93,7 @@ public List<UserRole> 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<Integer> roleIds) {
// Delete existing roles for the user tenant
repo.deleteByUserTenantId(userTenantId);
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down