Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/reportportal/commons-dao
Browse files Browse the repository at this point in the history
…into rc/5.11.1

� Conflicts:
�	build.gradle
�	gradle.properties
�	src/main/java/com/epam/ta/reportportal/config/ApiKeyCacheResolver.java
�	src/main/java/com/epam/ta/reportportal/dao/ActivityRepository.java
  • Loading branch information
Pavel Bortnik committed Dec 15, 2023
2 parents 1da74f0 + 9c9ae37 commit 94ca2a9
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 16 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ on:
env:
GH_USER_NAME: github.actor
SCRIPTS_VERSION: 5.10.0
BOM_VERSION: 5.10.0
MIGRATIONS_VERSION: 5.10.0
RELEASE_VERSION: 5.10.0
BOM_VERSION: 5.11.0
MIGRATIONS_VERSION: 5.11.0
RELEASE_VERSION: 5.11.0

jobs:
release:
Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ dependencies {
annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
testCompileOnly "org.projectlombok:lombok:${lombokVersion}"
testAnnotationProcessor "org.projectlombok:lombok:${lombokVersion}"
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'com.github.ben-manes.caffeine:caffeine'

testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile 'org.flywaydb.flyway-test-extensions:flyway-spring-test:6.1.0'

}

dependencyCheck {
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=5.10.0
lombokVersion=1.18.30
version=5.11.1
lombokVersion=1.18.30
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
/*
* Copyright 2023 EPAM Systems
*
* 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 com.epam.ta.reportportal.config;

import com.epam.ta.reportportal.dao.ApiKeyRepository;
import com.epam.ta.reportportal.entity.user.ApiKey;
import java.util.ArrayList;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
Expand All @@ -23,25 +39,32 @@ public class ApiKeyCacheResolver implements CacheResolver {

private final CacheManager cacheManager;

public ApiKeyCacheResolver(ApiKeyRepository apiKeyRepository, CacheManager cacheManager) {
public ApiKeyCacheResolver(ApiKeyRepository apiKeyRepository,
@Qualifier("caffeineCacheManager") CacheManager cacheManager) {
this.apiKeyRepository = apiKeyRepository;
this.cacheManager = cacheManager;
}

@Override
@NonNull
public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) {
Long apiKeyId = (Long) context.getArgs()[0];
ApiKey apiKey = apiKeyRepository.findById(apiKeyId).orElse(null);

Cache cache = cacheManager.getCache("apiKeyCache");
Collection<Cache> caches = new ArrayList<>();
if (apiKey != null && context.getOperation().getCacheNames().contains("apiKeyCache")) {
Cache cache = cacheManager.getCache("apiKeyCache");
if (cache != null) {
cache.evict(apiKey.getHash());
caches.add(cache);

if (cache != null) {
caches.add(cache);

Object arg = context.getArgs()[0];

if (context.getMethod().getName().equals("deleteById") && arg instanceof Long) {
Long apiKeyId = (Long) arg;
ApiKey apiKey = apiKeyRepository.findById(apiKeyId).orElse(null);
if (apiKey != null) {
cache.evict(apiKey.getHash());
}
}
}

return caches;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2023 EPAM Systems
*
* 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 com.epam.ta.reportportal.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.concurrent.TimeUnit;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Configuration for cache manager.
*
* @author Ivan Kustau
*/
@Configuration
public class CacheConfiguration {

@Bean("caffeineCacheManager")
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
cacheManager.setCaffeine(cacheProperties());
return cacheManager;
}

private Caffeine<Object, Object> cacheProperties() {
return Caffeine.newBuilder().expireAfterWrite(30, TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface ApiKeyRepository
* @param hash hash of api key
* @return {@link ApiKey}
*/
@Cacheable(value = "apiKeyCache", key = "#hash")
@Cacheable(value = "apiKeyCache", key = "#hash", cacheResolver = "apiKeyCacheResolver")
ApiKey findByHash(String hash);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
import com.epam.reportportal.commons.TikaContentTypeResolver;
import com.epam.ta.reportportal.filesystem.DataEncoder;
import com.epam.ta.reportportal.util.FeatureFlagHandler;
import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -69,4 +73,15 @@ public DataEncoder dataEncoder() {
return new DataEncoder();
}

@Bean(name = "caffeineCacheManager")
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
cacheManager.setCaffeine(cacheProperties());
return cacheManager;
}

private Caffeine<Object, Object> cacheProperties() {
return Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES);
}

}

0 comments on commit 94ca2a9

Please sign in to comment.