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

no.bekk.dbscheduler.ui.util.Caching Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) Bekk
 *
 * 

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of the License at * *

http://www.apache.org/licenses/LICENSE-2.0 * *

Unless required by applicable law or agreed to in writing, software distributed under the * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing permissions and * limitations under the License. */ package no.bekk.dbscheduler.ui.util; import com.github.kagkarlsson.scheduler.ScheduledExecution; import com.github.kagkarlsson.scheduler.ScheduledExecutionsFilter; import com.github.kagkarlsson.scheduler.Scheduler; import com.github.kagkarlsson.scheduler.task.TaskInstanceId; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import no.bekk.dbscheduler.ui.model.LogModel; import no.bekk.dbscheduler.ui.model.TaskDetailsRequestParams; import no.bekk.dbscheduler.ui.service.LogLogic; import org.springframework.stereotype.Component; @Component public class Caching { private final Map taskStatusCache = new ConcurrentHashMap<>(); private final List> taskDataCache = new ArrayList<>(); private final Map logCache = new ConcurrentHashMap<>(); public List> getExecutionsFromCacheOrDB( boolean isRefresh, Scheduler scheduler) { if (isRefresh || !taskDataCache.isEmpty()) { List> executions = getExecutionsFromDBWithoutUpdatingCache(scheduler); updateCache(executions); return executions; } else { return taskDataCache; } } public List> getExecutionsFromDBWithoutUpdatingCache( Scheduler scheduler) { List> executions = scheduler.getScheduledExecutions(); executions.addAll( scheduler.getScheduledExecutions(ScheduledExecutionsFilter.all().withPicked(true))); return executions; } public void updateCache(List> executions) { taskStatusCache.clear(); taskDataCache.clear(); taskDataCache.addAll(executions); for (ScheduledExecution execution : executions) { taskStatusCache.put(getUniqueId(execution), getStatus(execution)); } } public String getUniqueId(ScheduledExecution task) { return task.getTaskInstance().getTaskName() + "_" + task.getTaskInstance().getId(); } public String getStatus(ScheduledExecution task) { return (task.getConsecutiveFailures() > 0 ? "1" : "0") + (task.getPickedBy() != null ? "1" : "0"); } public String getStatusFromCache(TaskInstanceId taskInstance) { String uniqueId = taskInstance.getTaskName() + "_" + taskInstance.getId(); return taskStatusCache.get(uniqueId); } public List getLogsFromCacheOrDB( boolean isRefresh, LogLogic logLogic, TaskDetailsRequestParams requestParams) { if (isRefresh || logCache.isEmpty()) { List logs = logLogic.getLogsDirectlyFromDB(requestParams); updateLogCache(logs); return logs; } else { return new ArrayList<>(logCache.values()); } } public void updateLogCache(List logs) { logCache.clear(); for (LogModel log : logs) { logCache.put(log.getId(), log); } } public boolean checkLogCacheForKey(Long key) { return logCache.containsKey(key); } }