Skip to content

add the cache switch #2889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2024
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
@@ -1,13 +1,15 @@
/*
* Copyright 2019-2022 VMware, Inc.
* Copyright 2019-2023 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vip.messages.synch.schedule;

import java.util.List;

import com.vmware.vip.common.cache.SingletonCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
Expand All @@ -16,30 +18,32 @@
import com.alibaba.fastjson.JSON;
import com.vmware.vip.api.rest.l10n.L10NAPIV1;
import com.vmware.vip.common.cache.CacheName;
import com.vmware.vip.common.cache.TranslationCache3;
import com.vmware.vip.common.exceptions.VIPCacheException;
import com.vmware.vip.core.messages.service.singlecomponent.ComponentMessagesDTO;
import com.vmware.vip.messages.synch.model.UpdateSyncInfoResp;
import com.vmware.vip.messages.synch.utils.HttpsUtils;

/**
*
*
*
* @author shihu
*
*/
@Service
public class SynchInfoSchedule {
private final static long SYNCSECOND= 30000L;
private static Logger logger = LoggerFactory.getLogger(SynchInfoSchedule.class);
private static UpdateSyncInfoResp syncInfo =null;
private final static long SYNCSECOND= 30000L;
private static Logger logger = LoggerFactory.getLogger(SynchInfoSchedule.class);

private static UpdateSyncInfoResp syncInfo =null;
@Value("${source.cache.server.url}")
private String sourceCacheServerUrl;

@Value("${translation.synch.git.flag}")
private String translationSynchGitFlag;


@Autowired
private SingletonCache singletonCache;

@Scheduled(fixedDelay = SYNCSECOND)
@ConditionalOnProperty(value = "translation.synch.git.flag")
public void syncSourceToRemoteAndLocal() {
Expand All @@ -52,70 +56,70 @@ public void syncSourceToRemoteAndLocal() {
Thread.sleep(SYNCSECOND);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
logger.warn(e.getMessage(), e);
Thread.currentThread().interrupt();
logger.warn(e.getMessage(), e);
Thread.currentThread().interrupt();
}
}



}


private void queryL10n() {
String url = sourceCacheServerUrl+L10NAPIV1.SYNC_TRANSLATION_CACHE_L10N;
try {
logger.info("synch cache token url: {}", url);
String resultJson = HttpsUtils.doGet(url);
logger.info("result json: {}", resultJson);

UpdateSyncInfoResp resp =JSON.parseObject(resultJson, UpdateSyncInfoResp.class);

if(syncInfo!= null) {
logger.info("the local i18n cache token: {}", syncInfo.getUpdateCacheToken());
logger.info("the local i18n cache token: {}", syncInfo.getUpdateCacheToken());
long result = resp.getUpdateCacheToken()-syncInfo.getUpdateCacheToken();

if(result==1) {
removeProductCacheKey(resp);
}else if(result>1) {
removeAllCacheKey();
removeAllCacheKey();
}
}
}
syncInfo = resp;

} catch (Exception e) {
// TODO Auto-generated catch block
logger.error(e.getMessage(), e);
}

}

private void removeProductCacheKey(UpdateSyncInfoResp resp) {
logger.info("sync product cache key");
try {
List<String> singleList = TranslationCache3.getKeys(CacheName.ONECOMPONENT, ComponentMessagesDTO.class);
List<String> singleList = singletonCache.getKeys(CacheName.ONECOMPONENT, ComponentMessagesDTO.class);
for(String key: singleList) {
if(key.contains(resp.getUpdateCacheProductName()) && key.contains(resp.getUpdateCacheProductVersion())) {
TranslationCache3.deleteCachedObject(CacheName.ONECOMPONENT, key, ComponentMessagesDTO.class);
singletonCache.deleteCachedObject(CacheName.ONECOMPONENT, key, ComponentMessagesDTO.class);
}
}

} catch (VIPCacheException e) {
// TODO Auto-generated catch block
logger.error(e.getMessage(), e);
}
}

private void removeAllCacheKey() {
try {
logger.info("sync All product cache key");
TranslationCache3.removeAll(CacheName.ONECOMPONENT, ComponentMessagesDTO.class);
singletonCache.removeAll(CacheName.ONECOMPONENT, ComponentMessagesDTO.class);
} catch (VIPCacheException e) {
// TODO Auto-generated catch block
logger.error(e.getMessage(), e);
}
}



}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 VMware, Inc.
* Copyright 2019-2023 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vip.messages.synch.service;
Expand All @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Map;

import com.vmware.vip.common.cache.SingletonCache;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -19,7 +20,6 @@

import com.vmware.vip.common.cache.CacheName;
import com.vmware.vip.common.cache.CachedKeyGetter;
import com.vmware.vip.common.cache.TranslationCache3;
import com.vmware.vip.common.exceptions.VIPCacheException;
import com.vmware.vip.core.messages.service.singlecomponent.ComponentMessagesDTO;
import com.vmware.vip.messages.data.dao.exception.DataException;
Expand All @@ -28,16 +28,19 @@
@Service
public class SynchServiceImpl implements SynchService{
private static Logger logger = LoggerFactory.getLogger(SynchServiceImpl.class);

@Autowired
private SynchComponentDao syschComponentDao;


@Autowired
private SingletonCache singletonCache;

@Override
public List<String> updateTranslationBatch(List<ComponentMessagesDTO> comps) {
// TODO Auto-generated method stub

List<String> result = new ArrayList<String>();

for(ComponentMessagesDTO dto: comps) {
File fileResult;
try {
Expand All @@ -64,91 +67,91 @@ public List<String> updateTranslationBatch(List<ComponentMessagesDTO> comps) {
}else {
return null;
}



}





public File updateTranslation(ComponentMessagesDTO componentMessagesDTO)
throws DataException, ParseException, VIPCacheException {
String key = CachedKeyGetter.getOneCompnentCachedKey(componentMessagesDTO);
File updateFile;
ComponentMessagesDTO result = TranslationCache3.getCachedObject(CacheName.ONECOMPONENT, key, ComponentMessagesDTO.class);
// merge with local bundle file
SyncI18nMsg syncMsg = mergeComponentMessagesDTOWithFile(componentMessagesDTO);

if (StringUtils.isEmpty(result)) {
updateFile = syschComponentDao.update(componentMessagesDTO.getProductName(),
componentMessagesDTO.getVersion(),
componentMessagesDTO.getComponent(),
componentMessagesDTO.getLocale(),
syncMsg.getMessages());
} else {
updateFile = syschComponentDao.update(componentMessagesDTO.getProductName(),
componentMessagesDTO.getVersion(),
componentMessagesDTO.getComponent(),
componentMessagesDTO.getLocale(),
syncMsg.getMessages());

componentMessagesDTO.setMessages(syncMsg.getMessages());
TranslationCache3.updateCachedObject(CacheName.ONECOMPONENT, key,ComponentMessagesDTO.class, componentMessagesDTO);
}
return updateFile;




public File updateTranslation(ComponentMessagesDTO componentMessagesDTO)
throws DataException, ParseException, VIPCacheException {
String key = CachedKeyGetter.getOneCompnentCachedKey(componentMessagesDTO);
File updateFile;
ComponentMessagesDTO result = singletonCache.getCachedObject(CacheName.ONECOMPONENT, key, ComponentMessagesDTO.class);
// merge with local bundle file
SyncI18nMsg syncMsg = mergeComponentMessagesDTOWithFile(componentMessagesDTO);

if (StringUtils.isEmpty(result)) {
updateFile = syschComponentDao.update(componentMessagesDTO.getProductName(),
componentMessagesDTO.getVersion(),
componentMessagesDTO.getComponent(),
componentMessagesDTO.getLocale(),
syncMsg.getMessages());
} else {
updateFile = syschComponentDao.update(componentMessagesDTO.getProductName(),
componentMessagesDTO.getVersion(),
componentMessagesDTO.getComponent(),
componentMessagesDTO.getLocale(),
syncMsg.getMessages());

componentMessagesDTO.setMessages(syncMsg.getMessages());
singletonCache.updateCachedObject(CacheName.ONECOMPONENT, key,ComponentMessagesDTO.class, componentMessagesDTO);
}



/**
* Merge the translation in the componentMessagesDTO and in the local
* bundle.
*
* @param componentMessagesDTO
* the object of ComponentMessagesDTO, containing the latest
* translation.
* @return ComponentMessagesDTO a DTO object of ComponentMessagesDTO,
* containing the all translation.
* @throws ParseException
* @throws DataException
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private SyncI18nMsg mergeComponentMessagesDTOWithFile(ComponentMessagesDTO componentMessagesDTO){


SyncI18nMsg result;
try {
result = syschComponentDao.get(componentMessagesDTO.getProductName(), componentMessagesDTO.getVersion(), componentMessagesDTO.getComponent(), componentMessagesDTO.getLocale());
} catch (DataException e) {
// TODO Auto-generated catch block
result = null;
return updateFile;
}



/**
* Merge the translation in the componentMessagesDTO and in the local
* bundle.
*
* @param componentMessagesDTO
* the object of ComponentMessagesDTO, containing the latest
* translation.
* @return ComponentMessagesDTO a DTO object of ComponentMessagesDTO,
* containing the all translation.
* @throws ParseException
* @throws DataException
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private SyncI18nMsg mergeComponentMessagesDTOWithFile(ComponentMessagesDTO componentMessagesDTO){


SyncI18nMsg result;
try {
result = syschComponentDao.get(componentMessagesDTO.getProductName(), componentMessagesDTO.getVersion(), componentMessagesDTO.getComponent(), componentMessagesDTO.getLocale());
} catch (DataException e) {
// TODO Auto-generated catch block
result = null;
}

if (result != null) {
Iterator<Map.Entry<String, Object>> it = ((Map) componentMessagesDTO.getMessages()).entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
result.getMessages().put(entry.getKey(), (String) entry.getValue());
}

if (result != null) {
Iterator<Map.Entry<String, Object>> it = ((Map) componentMessagesDTO.getMessages()).entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
result.getMessages().put(entry.getKey(), (String) entry.getValue());
}

return result;
} else {
result = new SyncI18nMsg();
Iterator<Map.Entry<String, Object>> it = ((Map) componentMessagesDTO.getMessages()).entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
result.getMessages().put(entry.getKey(), (String) entry.getValue());
}

return result;

return result;
} else {
result = new SyncI18nMsg();
Iterator<Map.Entry<String, Object>> it = ((Map) componentMessagesDTO.getMessages()).entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
result.getMessages().put(entry.getKey(), (String) entry.getValue());
}

return result;
}
}








}
Loading