Skip to content

Commit

Permalink
Using list instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagohora committed Sep 5, 2024
1 parent fbb2868 commit 2ff2e65
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,40 @@

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RBucketReactive;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RListReactive;
import org.redisson.api.RedissonReactiveClient;

import java.time.Duration;
import java.util.List;
import java.util.Optional;

@Slf4j
@RequiredArgsConstructor
class AuthCredentialsCacheService implements CacheService {

public static final String DELIMITER = ";;;";
public static final String KEY_FORMAT = "auth-%s-%s";
private final RedissonReactiveClient redissonClient;
private final int ttlInSeconds;

public Optional<AuthCredentials> resolveApiKeyUserAndWorkspaceIdFromCache(@NonNull String apiKey) {
RBucketReactive<String> bucket = redissonClient.getBucket(apiKey);
public Optional<AuthCredentials> resolveApiKeyUserAndWorkspaceIdFromCache(@NonNull String apiKey,
@NonNull String workspaceName) {
String key = KEY_FORMAT.formatted(apiKey, workspaceName);

return bucket.get()
RListReactive<String> bucket = redissonClient.getList(key);

return bucket
.readAll()
.blockOptional()
.filter(pair -> !pair.isBlank())
.map(pair -> pair.split(DELIMITER))
.filter(pair -> pair.length == 2)
.map(pair -> new AuthCredentials(pair[0], pair[1]));
.filter(pair -> pair.size() == 2)
.map(pair -> new AuthCredentials(pair.getFirst(), pair.getLast()));
}

public void cache(@NonNull String apiKey, @NonNull String userName, @NonNull String workspaceId) {
redissonClient.getBucket(apiKey)
.set("%s%s%s".formatted(userName, DELIMITER, workspaceId), Duration.ofSeconds(ttlInSeconds))
.block();
public void cache(@NonNull String apiKey, @NonNull String workspaceName, @NonNull String userName,
@NonNull String workspaceId) {
String key = KEY_FORMAT.formatted(apiKey, workspaceName);
redissonClient.getList(key).addAll(List.of(userName, workspaceId)).block();
redissonClient.getList(key).expire(Duration.ofSeconds(ttlInSeconds)).block();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ interface CacheService {
record AuthCredentials(String userName, String workspaceId) {
}

void cache(String apiKey, String userName, String workspaceId);
Optional<AuthCredentials> resolveApiKeyUserAndWorkspaceIdFromCache(String apiKey);
void cache(String apiKey, String workspaceName, String userName, String workspaceId);
Optional<AuthCredentials> resolveApiKeyUserAndWorkspaceIdFromCache(String apiKey, String workspaceName);
}

class NoopCacheService implements CacheService {

@Override
public void cache(String apiKey, String userName, String workspaceId) {
public void cache(String apiKey, String workspaceName, String userName, String workspaceId) {
// no-op
}

@Override
public Optional<AuthCredentialsCacheService.AuthCredentials> resolveApiKeyUserAndWorkspaceIdFromCache(
String apiKey) {
String apiKey, String workspaceName) {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,15 @@ private void authenticateUsingApiKey(HttpHeaders headers, String workspaceName)

if (credentials.shouldCache()) {
log.debug("Caching user and workspace id for API key");
cacheService.cache(apiKey, credentials.userName(), credentials.workspaceId());
cacheService.cache(apiKey, workspaceName, credentials.userName(), credentials.workspaceId());
}

setCredentialIntoContext(credentials.userName(), credentials.workspaceId());
}

private ValidatedAuthCredentials validateApiKeyAndGetCredentials(String workspaceName, String apiKey) {
Optional<AuthCredentials> credentials = cacheService.resolveApiKeyUserAndWorkspaceIdFromCache(apiKey);
Optional<AuthCredentials> credentials = cacheService.resolveApiKeyUserAndWorkspaceIdFromCache(apiKey,
workspaceName);

if (credentials.isEmpty()) {
log.debug("User and workspace id not found in cache for API key");
Expand Down

0 comments on commit 2ff2e65

Please sign in to comment.