Skip to content

Commit e401af2

Browse files
committed
FINERACT-2241: New command processing - Cache Management (org.apache.fineract.infrastructure.cache)
1 parent ba06234 commit e401af2

File tree

10 files changed

+160
-169
lines changed

10 files changed

+160
-169
lines changed

fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/api/CacheApiResource.java

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,68 +19,71 @@
1919
package org.apache.fineract.infrastructure.cache.api;
2020

2121
import io.swagger.v3.oas.annotations.Operation;
22-
import io.swagger.v3.oas.annotations.Parameter;
23-
import io.swagger.v3.oas.annotations.media.Content;
24-
import io.swagger.v3.oas.annotations.media.Schema;
25-
import io.swagger.v3.oas.annotations.parameters.RequestBody;
26-
import io.swagger.v3.oas.annotations.responses.ApiResponse;
27-
import io.swagger.v3.oas.annotations.responses.ApiResponses;
2822
import io.swagger.v3.oas.annotations.tags.Tag;
23+
import jakarta.validation.Valid;
2924
import jakarta.ws.rs.Consumes;
3025
import jakarta.ws.rs.GET;
3126
import jakarta.ws.rs.PUT;
3227
import jakarta.ws.rs.Path;
3328
import jakarta.ws.rs.Produces;
3429
import jakarta.ws.rs.core.MediaType;
3530
import java.util.Collection;
31+
import java.util.UUID;
32+
import java.util.function.Supplier;
3633
import lombok.RequiredArgsConstructor;
37-
import org.apache.fineract.commands.domain.CommandWrapper;
38-
import org.apache.fineract.commands.service.CommandWrapperBuilder;
39-
import org.apache.fineract.commands.service.PortfolioCommandSourceWritePlatformService;
34+
import org.apache.fineract.command.core.CommandPipeline;
35+
import org.apache.fineract.infrastructure.cache.command.CacheSwitchCommand;
4036
import org.apache.fineract.infrastructure.cache.data.CacheData;
41-
import org.apache.fineract.infrastructure.cache.data.request.CacheRequest;
37+
import org.apache.fineract.infrastructure.cache.data.CacheSwitchRequest;
38+
import org.apache.fineract.infrastructure.cache.data.CacheSwitchResponse;
4239
import org.apache.fineract.infrastructure.cache.service.RuntimeDelegatingCacheManager;
43-
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
44-
import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSerializer;
45-
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
40+
import org.apache.fineract.infrastructure.core.service.DateUtils;
4641
import org.springframework.beans.factory.annotation.Qualifier;
4742
import org.springframework.stereotype.Component;
4843

4944
@Path("/v1/caches")
5045
@Consumes({ MediaType.APPLICATION_JSON })
5146
@Produces({ MediaType.APPLICATION_JSON })
5247
@Component
53-
@Tag(name = "Cache", description = "The following settings are possible for cache:\n" + "\n" + "No Caching: caching turned off\n"
54-
+ "Single node: caching on for single instance deployments of platorm (works for multiple tenants but only one tomcat)\n"
55-
+ "By default caching is set to No Caching. Switching between caches results in the cache been clear e.g. from Single node to No cache and back again would clear down the single node cache.")
48+
@Tag(name = "Cache", description = """
49+
The following settings are possible for cache:
50+
51+
No Caching: caching turned off
52+
53+
Single node: caching on for single instance deployments of platorm (works for multiple tenants but only one tomcat).
54+
By default caching is set to No Caching. Switching between caches results in the cache been clear e.g. from single
55+
node to no cache and back again would clear down the single node cache.
56+
""")
5657
@RequiredArgsConstructor
5758
public class CacheApiResource {
5859

59-
private static final String RESOURCE_NAME_FOR_PERMISSIONS = "CACHE";
60-
61-
private final PlatformSecurityContext context;
62-
private final DefaultToApiJsonSerializer<CacheData> toApiJsonSerializer;
63-
private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService;
6460
@Qualifier("runtimeDelegatingCacheManager")
6561
private final RuntimeDelegatingCacheManager cacheService;
62+
private final CommandPipeline commandPipeline;
6663

6764
@GET
68-
@Operation(summary = "Retrieve Cache Types", description = "Returns the list of caches.\n" + "\n" + "Example Requests:\n" + "\n"
69-
+ "caches")
65+
@Operation(summary = "Retrieve Cache Types", description = """
66+
Returns the list of caches.
67+
68+
Example Requests:
69+
70+
caches
71+
""")
7072
public Collection<CacheData> retrieveAll() {
71-
this.context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSIONS);
7273
return cacheService.retrieveAll();
7374
}
7475

7576
@PUT
7677
@Operation(summary = "Switch Cache", description = "Switches the cache to chosen one.")
77-
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = CacheRequest.class)))
78-
@ApiResponses({
79-
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CacheApiResourceSwagger.PutCachesResponse.class))) })
80-
public CommandProcessingResult switchCache(@Parameter(hidden = true) CacheRequest cacheRequest) {
81-
final CommandWrapper commandRequest = new CommandWrapperBuilder().updateCache()
82-
.withJson(toApiJsonSerializer.serialize(cacheRequest)).build();
78+
public CacheSwitchResponse switchCache(@Valid CacheSwitchRequest request) {
79+
final var command = new CacheSwitchCommand();
80+
81+
command.setId(UUID.randomUUID());
82+
command.setCreatedAt(DateUtils.getAuditOffsetDateTime());
83+
command.setPayload(request);
84+
85+
final Supplier<CacheSwitchResponse> response = commandPipeline.send(command);
8386

84-
return this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
87+
return response.get();
8588
}
8689
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.fineract.infrastructure.cache.command;
20+
21+
import lombok.Data;
22+
import lombok.EqualsAndHashCode;
23+
import org.apache.fineract.command.core.Command;
24+
import org.apache.fineract.infrastructure.cache.data.CacheSwitchRequest;
25+
26+
@Data
27+
@EqualsAndHashCode(callSuper = true)
28+
public class CacheSwitchCommand extends Command<CacheSwitchRequest> {}

fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/command/UpdateCacheCommandHandler.java

Lines changed: 0 additions & 89 deletions
This file was deleted.

fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/CacheData.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,20 @@
1818
*/
1919
package org.apache.fineract.infrastructure.cache.data;
2020

21+
import lombok.AllArgsConstructor;
22+
import lombok.Builder;
2123
import lombok.Data;
2224
import lombok.NoArgsConstructor;
23-
import lombok.experimental.Accessors;
2425
import org.apache.fineract.infrastructure.core.data.EnumOptionData;
2526

27+
@Builder
2628
@Data
2729
@NoArgsConstructor
28-
@Accessors(chain = true)
30+
@AllArgsConstructor
2931
public final class CacheData {
3032

3133
@SuppressWarnings("unused")
3234
private EnumOptionData cacheType;
3335
@SuppressWarnings("unused")
3436
private boolean enabled;
35-
36-
public static CacheData instance(final EnumOptionData cacheType, final boolean enabled) {
37-
return new CacheData().setCacheType(cacheType).setEnabled(enabled);
38-
}
39-
4037
}

fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/request/CacheRequest.java renamed to fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/CacheSwitchRequest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,25 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.fineract.infrastructure.cache.data.request;
19+
package org.apache.fineract.infrastructure.cache.data;
2020

21+
import jakarta.annotation.Nonnull;
2122
import java.io.Serial;
2223
import java.io.Serializable;
24+
import lombok.AllArgsConstructor;
25+
import lombok.Builder;
26+
import lombok.Data;
27+
import lombok.NoArgsConstructor;
2328

24-
public record CacheRequest(Long cacheType) implements Serializable {
29+
@Builder
30+
@Data
31+
@NoArgsConstructor
32+
@AllArgsConstructor
33+
public class CacheSwitchRequest implements Serializable {
2534

2635
@Serial
2736
private static final long serialVersionUID = 1L;
37+
38+
@Nonnull
39+
private Integer cacheType;
2840
}

fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/api/CacheApiResourceSwagger.java renamed to fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/CacheSwitchResponse.java

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,25 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.fineract.infrastructure.cache.api;
20-
21-
import io.swagger.v3.oas.annotations.media.Schema;
22-
23-
/**
24-
* Created by sanyam on 28/7/17.
25-
*/
26-
final class CacheApiResourceSwagger {
27-
28-
private CacheApiResourceSwagger() {
29-
30-
}
31-
32-
@Schema(description = "PutCachesResponse")
33-
public static final class PutCachesResponse {
34-
35-
private PutCachesResponse() {
36-
37-
}
38-
39-
public static final class PutCachechangesSwagger {
40-
41-
private PutCachechangesSwagger() {
42-
43-
}
44-
45-
@Schema(example = "2")
46-
public Long cacheType;
47-
48-
}
49-
50-
public PutCachechangesSwagger cacheType;
51-
52-
}
19+
package org.apache.fineract.infrastructure.cache.data;
20+
21+
import java.io.Serial;
22+
import java.io.Serializable;
23+
import java.util.Map;
24+
import lombok.AllArgsConstructor;
25+
import lombok.Builder;
26+
import lombok.Data;
27+
import lombok.NoArgsConstructor;
28+
29+
@Builder
30+
@Data
31+
@NoArgsConstructor
32+
@AllArgsConstructor
33+
public class CacheSwitchResponse implements Serializable {
34+
35+
@Serial
36+
private static final long serialVersionUID = 1L;
37+
38+
private Integer cacheType;
39+
private Map<String, Object> changes;
5340
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.fineract.infrastructure.cache.handler;
20+
21+
import lombok.RequiredArgsConstructor;
22+
import lombok.extern.slf4j.Slf4j;
23+
import org.apache.fineract.command.core.Command;
24+
import org.apache.fineract.command.core.CommandHandler;
25+
import org.apache.fineract.infrastructure.cache.data.CacheSwitchRequest;
26+
import org.apache.fineract.infrastructure.cache.data.CacheSwitchResponse;
27+
import org.apache.fineract.infrastructure.cache.domain.CacheType;
28+
import org.apache.fineract.infrastructure.cache.service.CacheWritePlatformService;
29+
import org.springframework.stereotype.Component;
30+
import org.springframework.transaction.annotation.Transactional;
31+
32+
@Slf4j
33+
@Component
34+
@RequiredArgsConstructor
35+
public class CacheSwitchCommandHandler implements CommandHandler<CacheSwitchRequest, CacheSwitchResponse> {
36+
37+
private final CacheWritePlatformService cacheService;
38+
39+
@Transactional
40+
@Override
41+
public CacheSwitchResponse handle(final Command<CacheSwitchRequest> command) {
42+
var request = command.getPayload();
43+
var cacheType = CacheType.fromInt(request.getCacheType());
44+
var changes = cacheService.switchToCache(cacheType);
45+
46+
return CacheSwitchResponse.builder().changes(changes).cacheType(request.getCacheType()).build();
47+
}
48+
}

fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/service/RuntimeDelegatingCacheManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public Collection<CacheData> retrieveAll() {
7878
final EnumOptionData noCacheType = CacheEnumerations.cacheType(CacheType.NO_CACHE);
7979
final EnumOptionData singleNodeCacheType = CacheEnumerations.cacheType(CacheType.SINGLE_NODE);
8080

81-
final CacheData noCache = CacheData.instance(noCacheType, noCacheEnabled);
82-
final CacheData singleNodeCache = CacheData.instance(singleNodeCacheType, ehCacheEnabled);
81+
final CacheData noCache = CacheData.builder().cacheType(noCacheType).enabled(noCacheEnabled).build();
82+
final CacheData singleNodeCache = CacheData.builder().cacheType(singleNodeCacheType).enabled(ehCacheEnabled).build();
8383

8484
return Arrays.asList(noCache, singleNodeCache);
8585
}

0 commit comments

Comments
 (0)