Best Cerberus-source code snippet using org.cerberus.crud.service.impl.ParameterService.purgeCacheEntry
Source:ParameterService.java
...60 public HashMap<String, Parameter> getCacheEntry() {61 return cacheEntry;62 }63 @Override64 public void purgeCacheEntry(String parameter) {65 if (StringUtil.isNullOrEmpty(parameter)) {66 cacheEntry.clear();67 LOG.debug("All Parameter cache entries purged.");68 } else {69 try {70 for (Map.Entry<String, Parameter> entry : cacheEntry.entrySet()) {71 String key = entry.getKey();72 Parameter value = entry.getValue();73 if (parameter == null || key.contains(parameter)) {74 cacheEntry.remove(key);75 LOG.debug("Purged Parameter " + key + " from cache entries.");76 }77 }78 } catch (ConcurrentModificationException e) {79 // If we failed to removed the parameter entries (ConcurrentModificationException) , we purge everything ;-).80 cacheEntry.clear();81 LOG.debug("All Parameter cache entries purged. (specific parameter execution failed).");82 }83 }84 }85 @Override86 public Parameter findParameterByKey(String key, String system) throws CerberusException {87 Parameter myParameter;88 /**89 * We try to get the parameter using the system parameter but if it does90 * not exist or empty, we get it with system="" which correspond to the91 * default global Cerberus Parameter.92 */93 LocalDateTime currentTime = LocalDateTime.now();94 String cacheKey = key + '#' + system;95 if (cacheEntry == null) {96 cacheEntry = new HashMap<>();97 }98 if (Parameter.CACHE_DURATION > 0 && !Parameter.VALUE_cerberus_queueexecution_enable.equals(key)) {99 if (cacheEntry.containsKey(cacheKey)100 && cacheEntry.get(cacheKey) != null101 && cacheEntry.get(cacheKey).getCacheEntryCreation() != null102 && cacheEntry.get(cacheKey).getCacheEntryCreation().plusSeconds(Parameter.CACHE_DURATION).isAfter(currentTime)) {103 LOG.debug("Return parameter from cache Value.");104 return cacheEntry.get(cacheKey);105 }106 }107 try {108 LOG.debug("Trying to retrieve parameter : " + key + " - [" + system + "]");109 myParameter = parameterDao.findParameterByKey(system, key);110 if (myParameter != null && myParameter.getValue().equalsIgnoreCase("")) {111 myParameter = parameterDao.findParameterByKey("", key);112 }113 } catch (CerberusException ex) {114 LOG.debug("Trying to retrieve parameter (default value) : " + key + " - []");115 myParameter = parameterDao.findParameterByKey("", key);116 if (myParameter != null) {117 LOG.debug("Insert parameter to cache.");118 myParameter.setCacheEntryCreation(currentTime);119 cacheEntry.put(cacheKey, myParameter);120 }121 return myParameter;122 }123 if (myParameter != null) {124 LOG.debug("Insert parameter to cache.");125 myParameter.setCacheEntryCreation(currentTime);126 cacheEntry.put(cacheKey, myParameter);127 }128 return myParameter;129 }130 @Override131 public boolean getParameterBooleanByKey(String key, String system, boolean defaultValue) {132 Parameter myParameter;133 boolean outPutResult = defaultValue;134 try {135 myParameter = this.findParameterByKey(key, system);136 outPutResult = StringUtil.parseBoolean(myParameter.getValue());137 } catch (CerberusException | NumberFormatException ex) {138 LOG.error("Error when trying to retreive parameter : '" + key + "' for system : '" + system + "'. Default value returned : '" + defaultValue + "'. Trace : " + ex, ex);139 }140 LOG.debug("Success loading parameter : '" + key + "' for system : '" + system + "'. Value returned : '" + outPutResult + "'");141 return outPutResult;142 }143 @Override144 public Integer getParameterIntegerByKey(String key, String system, Integer defaultValue) {145 Parameter myParameter;146 Integer outPutResult = defaultValue;147 if (Property.isSaaS() && Parameter.VALUE_queueexecution_global_threadpoolsize.equalsIgnoreCase(key)) {148 LOG.debug("Saas Mode is activated so parameter retrieved will be the master one.");149 key = Parameter.VALUE_queueexecution_global_threadpoolsize_master;150 try {151 myParameter = this.findParameterByKey(key, system);152 if (myParameter == null) {153 return outPutResult;154 }155 outPutResult = Integer.valueOf(myParameter.getValue());156 LOG.debug("Success loading parameter : '" + key + "' for system : '" + system + "'. Value returned : '" + outPutResult + "'");157 } catch (CerberusException | NumberFormatException ex) {158 LOG.debug("Error when trying to retrieve parameter : '" + key + "' for system : '" + system + "'. Default value returned : '" + defaultValue + "'. Trace : " + ex, ex);159 }160 return outPutResult;161 }162 try {163 myParameter = this.findParameterByKey(key, system);164 if (myParameter == null) {165 return outPutResult;166 }167 outPutResult = Integer.valueOf(myParameter.getValue());168 LOG.debug("Success loading parameter : '" + key + "' for system : '" + system + "'. Value returned : '" + outPutResult + "'");169 } catch (CerberusException | NumberFormatException ex) {170 LOG.error("Error when trying to retrieve parameter : '" + key + "' for system : '" + system + "'. Default value returned : '" + defaultValue + "'. Trace : " + ex, ex);171 }172 return outPutResult;173 }174 @Override175 public long getParameterLongByKey(String key, String system, long defaultValue) {176 Parameter myParameter;177 long outPutResult = defaultValue;178 try {179 myParameter = this.findParameterByKey(key, system);180 outPutResult = Long.parseLong(myParameter.getValue());181 } catch (CerberusException | NumberFormatException ex) {182 LOG.error("Error when trying to retrieve parameter : '" + key + "' for system : '" + system + "'. Default value returned : '" + defaultValue + "'. Trace : " + ex, ex);183 }184 LOG.debug("Success loading parameter : '" + key + "' for system : '" + system + "'. Value returned : '" + outPutResult + "'");185 return outPutResult;186 }187 @Override188 public float getParameterFloatByKey(String key, String system, float defaultValue) {189 Parameter myParameter;190 float outPutResult = defaultValue;191 try {192 myParameter = this.findParameterByKey(key, system);193 outPutResult = Float.valueOf(myParameter.getValue());194 } catch (CerberusException | NumberFormatException ex) {195 LOG.error("Error when trying to retrieve parameter : '" + key + "' for system : '" + system + "'. Default value returned : '" + defaultValue + "'. Trace : " + ex, ex);196 }197 LOG.debug("Success loading parameter : '" + key + "' for system : '" + system + "'. Value returned : '" + outPutResult + "'");198 return outPutResult;199 }200 @Override201 public String getParameterStringByKey(String key, String system, String defaultValue) {202 Parameter myParameter;203 String outPutResult = defaultValue;204 try {205 myParameter = this.findParameterByKey(key, system);206 outPutResult = myParameter.getValue();207 } catch (CerberusException ex) {208 LOG.error("Error when trying to retrieve parameter : '" + key + "' for system : '" + system + "'. Default value returned : '" + defaultValue + "'. Trace : " + ex, ex);209 }210 LOG.debug("Success loading parameter : '" + key + "' for system : '" + system + "'. Value returned : '" + outPutResult + "'");211 return outPutResult;212 }213 @Override214 public List<Parameter> findAllParameter() throws CerberusException {215 return parameterDao.findAllParameter();216 }217 @Override218 public List<Parameter> findAllParameterWithSystem1(String system, String system1) throws CerberusException {219 return parameterDao.findAllParameterWithSystem1(system, system1);220 }221 @Override222 public AnswerList<Parameter> readWithSystem1BySystemByCriteria(String system, String system1, int startPosition, int length, String columnName, String sort, String searchParameter, Map<String, List<String>> individualSearch) {223 return parameterDao.readWithSystem1BySystemByCriteria(system, system1, startPosition, length, columnName, sort, searchParameter, individualSearch);224 }225 @Override226 public AnswerItem<Parameter> readWithSystem1ByKey(String system, String key, String system1) {227 return parameterDao.readWithSystem1ByKey(system, key, system1);228 }229 @Override230 public AnswerList<String> readDistinctValuesWithSystem1ByCriteria(String system, String system1, String searchParameter, Map<String, List<String>> individualSearch, String columnName) {231 return parameterDao.readDistinctValuesWithSystem1ByCriteria(system, system1, searchParameter, individualSearch, columnName);232 }233 @Override234 public AnswerItem<Parameter> readByKey(String system, String param) {235 return parameterDao.readByKey(system, param);236 }237 @Override238 public Answer update(Parameter object) {239 Answer answer = parameterDao.update(object);240 if (MessageEventEnum.DATA_OPERATION_OK.equals(answer.getResultMessage().getSource())) {241 // A parameter is changed so we purge the cache.242 purgeCacheEntry(object.getParam());243 // If we activate the parameter to active the job, we trigger it directly.244 if (Parameter.VALUE_cerberus_queueexecution_enable.equals(object.getParam()) && ParameterParserUtil.parseBooleanParam(object.getValue(), false)) {245 try {246 // Run the Execution pool Job.247 executionThreadPoolService.executeNextInQueueAsynchroneously(false);248 } catch (CerberusException ex) {249 LOG.error("Exeption triggering the ThreadPool job.", ex);250 }251 }252 }253 return answer;254 }255 @Override256 public Answer setParameter(String parameterKey, String system, String value) {257 Answer answer = parameterDao.setParameter(parameterKey, system, value);258 if (MessageEventEnum.DATA_OPERATION_OK.equals(answer.getResultMessage().getSource())) {259 // A parameter is changed so we purge the cache.260 purgeCacheEntry(parameterKey);261 // If we activate the parameter to active the job, we trigger it directly.262 if (Parameter.VALUE_cerberus_queueexecution_enable.equals(parameterKey) && ParameterParserUtil.parseBooleanParam(value, false)) {263 try {264 // Run the Execution pool Job.265 executionThreadPoolService.executeNextInQueueAsynchroneously(false);266 } catch (CerberusException ex) {267 LOG.error("Exeption triggering the ThreadPool job.", ex);268 }269 }270 }271 return answer;272 }273 @Override274 public Answer create(Parameter object) {275 Answer answer = parameterDao.create(object);276 if (MessageEventEnum.DATA_OPERATION_OK.equals(answer.getResultMessage().getSource())) {277 purgeCacheEntry(object.getParam());278 }279 return answer;280 }281 @Override282 public Answer save(Parameter object, HttpServletRequest request) {283 Answer finalAnswer = new Answer();284 if (!hasPermissionsUpdate(object, request)) {285 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNAUTHORISED);286 msg.setDescription(msg.getDescription()287 .replace("%ITEM%", "Parameter")288 .replace("%OPERATION%", "update")289 .replace("%REASON%", "This parameter is protected and cannot be updated."));290 finalAnswer = new Answer(msg);291 LOG.warn("Attempt to modify Parameter '" + object.getParam() + "' to value '" + object.getValue() + "' refused !");...
purgeCacheEntry
Using AI Code Generation
1package org.cerberus.crud.service.impl;2import java.util.List;3import org.apache.logging.log4j.LogManager;4import org.apache.logging.log4j.Logger;5import org.cerberus.crud.dao.IParameterDAO;6import org.cerberus.crud.entity.Parameter;7import org.cerberus.crud.service.IParameterService;8import org.cerberus.exception.CerberusException;9import org.cerberus.util.answer.Answer;10import org.cerberus.util.answer.AnswerItem;11import org.cerberus.util.answer.AnswerList;12import org.cerberus.util.answer.AnswerUtil;13import org.springframework.beans.factory.annotation.Autowired;14import org.springframework.stereotype.Service;15import org.springframework.transaction.annotation.Transactional;16public class ParameterService implements IParameterService {17 private static final Logger LOG = LogManager.getLogger(ParameterService.class);18 private IParameterDAO parameterDAO;
purgeCacheEntry
Using AI Code Generation
1ParameterService parameterService = appContext.getBean(ParameterService.class);2List<Parameter> parameterList = parameterService.findAllParameter();3for (Parameter parameter : parameterList) {4 parameterService.purgeCacheEntry(parameter);5}6parameterService.getCache().isEmpty();7parameterService.getCache().containsKey("cerberus_application_environment");8parameterService.getCache().containsValue("DEV");9parameterService.getCache().get("cerberus_application_environment");10parameterService.getCache().getAll(parameterList);11parameterService.getCache().getAllKeys();12parameterService.getCache().getAllValues();13parameterService.getCache().getSize();14parameterService.getCache().getKeys();15parameterService.getCache().getValues(
purgeCacheEntry
Using AI Code Generation
1import org.springframework.cache.CacheManager;2import org.springframework.cache.concurrent.ConcurrentMapCacheManager;3import org.springframework.cache.Cache;4import org.springframework.cache.Cache.ValueWrapper;5import org.springframework.cache.Cache.ValueRetrievalException;6import org.springframework.cache.CacheManager;7import org.springframework.cache.Cache.ValueWrapper;8import org.springframework.cache.Cache.ValueRetrievalException;9import org.springframework.cache.concurrent.ConcurrentMapCacheManager;10import org.springframework.context.ApplicationContext;11import org.springframework.context.support.ClassPathXmlApplicationContext;12import org.cerberus.crud.service.impl.ParameterService;13public class RemoveCacheEntry {14 public static void main(String[] args) {15 ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");16 ParameterService parameterService = appContext.getBean(ParameterService.class);17 CacheManager cacheManager = appContext.getBean(ConcurrentMapCacheManager.class);18 Cache cache = cacheManager.getCache("parameter");19 ValueWrapper valueWrapper = cache.get("cerberus_defaultlanguage");20 String value = (String) valueWrapper.get();21 System.out.println("Value: " + value);22 parameterService.purgeCacheEntry("parameter", "cerberus_defaultlanguage");23 try {24 valueWrapper = cache.get("cerberus_defaultlanguage");25 value = (String) valueWrapper.get();26 System.out.println("Value: " + value);27 } catch (ValueRetrievalException e) {28 System.out.println("Cache entry was removed");29 }30 }31}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!