All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.smilego.tenant.service.ParameterCacheableService Maven / Gradle / Ivy

package io.smilego.tenant.service;

import io.smilego.tenant.model.Parameter;
import io.smilego.tenant.persistence.ParameterRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
@Slf4j
@RequiredArgsConstructor
public class ParameterCacheableService {

    private final ParameterRepository paramRepository;
    private final RedisTemplate redisTemplate;

    @Cacheable(value = "paramCache", key = "T(java.lang.String).valueOf(#p0).concat('-').concat(#p1)")
    public List getTenantParameters(String serviceName, String tenantId){
        return this.paramRepository.findParamByServiceNameAndTenantId(serviceName, tenantId)
                .orElseThrow(() -> new RuntimeException("Erro ao pesquisar o param por Service Name " + serviceName + " And Tenant ID " + tenantId));
    }

    public List getAllParams(){
        return paramRepository.findAll();
    }

    @CacheEvict(value = "paramCache", key = "T(java.lang.String).valueOf(#p0).concat('-').concat(#p1)")
    public void deleteTenantParametersByServiceId(String serviceName, String tenantId){

    }

    public Parameter save(Parameter param){
        return this.paramRepository.save(param);
    }

    public Page findAll(Example example, Pageable pageable) {
        return this.paramRepository.findAll(example, pageable);
    }

    public Optional findById(Long id) {
        return this.paramRepository.findById(id);
    }

    public void deleteById(Long id) {
        this.paramRepository.deleteById(id);
    }

    @Cacheable(value = "workerRunning", key = "T(java.lang.String).valueOf(#p0).concat('-').concat(#p1)")
    public int startWorker(String tenantId, String nameJob){
        return 1;
    }

    @CacheEvict(value = "workerRunning", key = "T(java.lang.String).valueOf(#p0).concat('-').concat(#p1)")
    public int stopWorker(String tenantId, String nameJob){
        return 0;
    }

    public boolean workerRunning(String tenantId, String nameJob){
        Integer value = 0;
        RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
        try {
            String key = "workerRunning::".concat(tenantId).concat("-").concat(nameJob);
            value = Integer.valueOf(new String(redisConnection.get(key.getBytes())));
        } catch (Exception e) {
        }finally {
            redisConnection.close();
        }
        return (Objects.nonNull(value) && value == 1)? true:false;
    }

    @CacheEvict(value = "workerRunning")
    public void clearWorkerRunning(){

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy