Skip to content
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
Expand Up @@ -19,68 +19,71 @@
package org.apache.fineract.infrastructure.cache.api;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import java.util.Collection;
import java.util.UUID;
import java.util.function.Supplier;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.commands.domain.CommandWrapper;
import org.apache.fineract.commands.service.CommandWrapperBuilder;
import org.apache.fineract.commands.service.PortfolioCommandSourceWritePlatformService;
import org.apache.fineract.command.core.CommandPipeline;
import org.apache.fineract.infrastructure.cache.command.CacheSwitchCommand;
import org.apache.fineract.infrastructure.cache.data.CacheData;
import org.apache.fineract.infrastructure.cache.data.request.CacheRequest;
import org.apache.fineract.infrastructure.cache.data.CacheSwitchRequest;
import org.apache.fineract.infrastructure.cache.data.CacheSwitchResponse;
import org.apache.fineract.infrastructure.cache.service.RuntimeDelegatingCacheManager;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSerializer;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
import org.apache.fineract.infrastructure.core.service.DateUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Path("/v1/caches")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Component
@Tag(name = "Cache", description = "The following settings are possible for cache:\n" + "\n" + "No Caching: caching turned off\n"
+ "Single node: caching on for single instance deployments of platorm (works for multiple tenants but only one tomcat)\n"
+ "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.")
@Tag(name = "Cache", description = """
The following settings are possible for cache:

No Caching: caching turned off

Single node: caching on for single instance deployments of platorm (works for multiple tenants but only one tomcat).
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.
""")
@RequiredArgsConstructor
public class CacheApiResource {

private static final String RESOURCE_NAME_FOR_PERMISSIONS = "CACHE";

private final PlatformSecurityContext context;
private final DefaultToApiJsonSerializer<CacheData> toApiJsonSerializer;
private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService;
@Qualifier("runtimeDelegatingCacheManager")
private final RuntimeDelegatingCacheManager cacheService;
private final CommandPipeline commandPipeline;

@GET
@Operation(summary = "Retrieve Cache Types", description = "Returns the list of caches.\n" + "\n" + "Example Requests:\n" + "\n"
+ "caches")
@Operation(summary = "Retrieve Cache Types", description = """
Returns the list of caches.

Example Requests:

caches
""")
public Collection<CacheData> retrieveAll() {
this.context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSIONS);
return cacheService.retrieveAll();
}

@PUT
@Operation(summary = "Switch Cache", description = "Switches the cache to chosen one.")
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = CacheRequest.class)))
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CacheApiResourceSwagger.PutCachesResponse.class))) })
public CommandProcessingResult switchCache(@Parameter(hidden = true) CacheRequest cacheRequest) {
final CommandWrapper commandRequest = new CommandWrapperBuilder().updateCache()
.withJson(toApiJsonSerializer.serialize(cacheRequest)).build();
public CacheSwitchResponse switchCache(@Valid CacheSwitchRequest request) {
final var command = new CacheSwitchCommand();

command.setId(UUID.randomUUID());
command.setCreatedAt(DateUtils.getAuditOffsetDateTime());
command.setPayload(request);

final Supplier<CacheSwitchResponse> response = commandPipeline.send(command);

return this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
return response.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.fineract.infrastructure.cache.command;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.fineract.command.core.Command;
import org.apache.fineract.infrastructure.cache.data.CacheSwitchRequest;

@Data
@EqualsAndHashCode(callSuper = true)
public class CacheSwitchCommand extends Command<CacheSwitchRequest> {}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,20 @@
*/
package org.apache.fineract.infrastructure.cache.data;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.fineract.infrastructure.core.data.EnumOptionData;

@Builder
@Data
@NoArgsConstructor
@Accessors(chain = true)
@AllArgsConstructor
public final class CacheData {

@SuppressWarnings("unused")
private EnumOptionData cacheType;
@SuppressWarnings("unused")
private boolean enabled;

public static CacheData instance(final EnumOptionData cacheType, final boolean enabled) {
return new CacheData().setCacheType(cacheType).setEnabled(enabled);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,25 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.cache.api;

import io.swagger.v3.oas.annotations.media.Schema;

/**
* Created by sanyam on 28/7/17.
*/
final class CacheApiResourceSwagger {

private CacheApiResourceSwagger() {

}

@Schema(description = "PutCachesResponse")
public static final class PutCachesResponse {

private PutCachesResponse() {

}

public static final class PutCachechangesSwagger {

private PutCachechangesSwagger() {

}

@Schema(example = "2")
public Long cacheType;

}

public PutCachechangesSwagger cacheType;

}
package org.apache.fineract.infrastructure.cache.data;

import jakarta.validation.constraints.NotNull;
import java.io.Serial;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CacheSwitchRequest implements Serializable {

@Serial
private static final long serialVersionUID = 1L;

@NotNull(message = "{org.apache.fineract.cache.cache-type.not-null}")
private Integer cacheType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.cache.data.request;
package org.apache.fineract.infrastructure.cache.data;

import java.io.Serial;
import java.io.Serializable;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

public record CacheRequest(Long cacheType) implements Serializable {
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CacheSwitchResponse implements Serializable {

@Serial
private static final long serialVersionUID = 1L;

private Integer cacheType;
private Map<String, Object> changes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.fineract.infrastructure.cache.handler;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.command.core.Command;
import org.apache.fineract.command.core.CommandHandler;
import org.apache.fineract.infrastructure.cache.data.CacheSwitchRequest;
import org.apache.fineract.infrastructure.cache.data.CacheSwitchResponse;
import org.apache.fineract.infrastructure.cache.domain.CacheType;
import org.apache.fineract.infrastructure.cache.service.CacheWritePlatformService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Component
@RequiredArgsConstructor
public class CacheSwitchCommandHandler implements CommandHandler<CacheSwitchRequest, CacheSwitchResponse> {

private final CacheWritePlatformService cacheService;

@Transactional
@Override
public CacheSwitchResponse handle(final Command<CacheSwitchRequest> command) {
var request = command.getPayload();
var cacheType = CacheType.fromInt(request.getCacheType());
var changes = cacheService.switchToCache(cacheType);

return CacheSwitchResponse.builder().changes(changes).cacheType(request.getCacheType()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public Collection<CacheData> retrieveAll() {
final EnumOptionData noCacheType = CacheEnumerations.cacheType(CacheType.NO_CACHE);
final EnumOptionData singleNodeCacheType = CacheEnumerations.cacheType(CacheType.SINGLE_NODE);

final CacheData noCache = CacheData.instance(noCacheType, noCacheEnabled);
final CacheData singleNodeCache = CacheData.instance(singleNodeCacheType, ehCacheEnabled);
final CacheData noCache = CacheData.builder().cacheType(noCacheType).enabled(noCacheEnabled).build();
final CacheData singleNodeCache = CacheData.builder().cacheType(singleNodeCacheType).enabled(ehCacheEnabled).build();

return Arrays.asList(noCache, singleNodeCache);
}
Expand Down
Loading
Loading