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

com.github.rexsheng.springboot.faster.system.locale.application.LocaleServiceImpl Maven / Gradle / Ivy

The newest version!
package com.github.rexsheng.springboot.faster.system.locale.application;

import com.github.rexsheng.springboot.faster.function.Tuple2;
import com.github.rexsheng.springboot.faster.i18n.event.LocaleChangedEvent;
import com.github.rexsheng.springboot.faster.system.locale.application.dto.LocaleDetailOutput;
import com.github.rexsheng.springboot.faster.system.locale.application.dto.RefreshLocaleSourceRequest;
import com.github.rexsheng.springboot.faster.system.locale.domain.DictDomainService;
import com.github.rexsheng.springboot.faster.system.locale.domain.LocaleRefreshEvent;
import com.github.rexsheng.springboot.faster.system.locale.domain.SysLocaleSource;
import com.github.rexsheng.springboot.faster.system.locale.domain.gateway.LocaleGateway;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@ConditionalOnClass(RedisTemplate.class)
public class LocaleServiceImpl implements LocaleService, ApplicationListener {

    private static final Logger logger= LoggerFactory.getLogger(LocaleServiceImpl.class);

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private DictDomainService dictDomainService;

    @Resource
    private LocaleGateway localeGateway;

    @Override
    public List getActivedLocaleSource() {
        List> listMap= (List>) redisTemplate.opsForHash().multiGet("locale",SysLocaleSource.activeLocaleRedisKeys());
        if(listMap!=null){
            if(listMap.size()>1){
                return SysLocaleSource.ofList(listMap.get(0),listMap.get(1))
                        .stream()
                        .map(LocaleDetailOutput::of).collect(Collectors.toList());
            }
        }
        return SysLocaleSource.filterByActived(getSourceDataList())
                .stream()
                .map(LocaleDetailOutput::of).collect(Collectors.toList());
    }

    private List getSourceDataList(){
        List dictList=dictDomainService.getDictList();
        List localeList=localeGateway.queryLocales();
        List list=new ArrayList<>();
        list.addAll(dictList);
        list.addAll(localeList);
        return list;
    }

    @Override
    public void refreshLocaleSource(RefreshLocaleSourceRequest request) {
        List sources=getSourceDataList();
        redisTemplate.opsForHash().putAll("locale",SysLocaleSource.toLocaleMap(sources));
        stringRedisTemplate.convertAndSend("localeSourceChanged","");
        logger.info("已更新数据源");
    }

    @Override
    @Async
    public void onApplicationEvent(LocaleRefreshEvent event) {
        this.refreshLocaleSource(new RefreshLocaleSourceRequest());
    }

    @Override
    public Tuple2, Map> getLocaleFromCache(Locale locale) {
        List> listMap= (List>) redisTemplate.opsForHash().multiGet("locale",Arrays.asList(locale.toLanguageTag(),locale.toLanguageTag()+"_1"));
        if(listMap!=null){
            if(listMap.size()>1){
                return new Tuple2<>(listMap.get(0),listMap.get(1));
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy