Skip to content

Commit

Permalink
[OPIK-595]: Add Redis Caching part 1 (#1015)
Browse files Browse the repository at this point in the history
* [OPIK-595]: Add Redis Caching part 1

* Disable failing test

* Add more tests

* Support to collections

* Use compiler args

* Add validation

* Add support to Flux

* Make wrapper property more generic

* Fix format
  • Loading branch information
thiagohora authored Jan 14, 2025
1 parent 53fda9f commit 272dfb8
Show file tree
Hide file tree
Showing 24 changed files with 1,037 additions and 10 deletions.
13 changes: 13 additions & 0 deletions apps/opik-backend/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,16 @@ llmProviderClient:
# Default: 2023-06-01
# Description: Anthropic API version https://docs.anthropic.com/en/api/versioning
version: ${LLM_PROVIDER_ANTHROPIC_VERSION:-'2023-06-01'}

# Configuration for cache manager
cacheManager:
# Default: true
# Description: Whether or not cache manager is enabled
enabled: ${CACHE_MANAGER_ENABLED:-true}
# Default: PT1S
# Description: Time to live for cache entries in using the formats accepted are based on the ISO-8601: https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#parse-java.lang.CharSequence-
defaultDuration: ${CACHE_MANAGER_DEFAULT_DURATION:-PT1S}
caches:
# Default: {}
# Description: Dynamically created caches with their respective time to live in seconds
automationRules: ${CACHE_MANAGER_AUTOMATION_RULES_DURATION:-PT1S}
10 changes: 9 additions & 1 deletion apps/opik-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@
<artifactId>httpclient5</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.mvel</groupId>
<artifactId>mvel2</artifactId>
<version>2.5.2.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
Expand Down Expand Up @@ -290,7 +295,7 @@
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.7.0</version>
<version>${clickhouse-java.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -446,6 +451,9 @@
<configuration>
<source>21</source>
<target>21</target>
<compilerArgument>-parameters</compilerArgument>
<parameters>true</parameters>
<testCompilerArgument>-parameters</testCompilerArgument>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.comet.opik.infrastructure.bi.BiModule;
import com.comet.opik.infrastructure.bi.OpikGuiceyLifecycleEventListener;
import com.comet.opik.infrastructure.bundle.LiquibaseBundle;
import com.comet.opik.infrastructure.cache.CacheModule;
import com.comet.opik.infrastructure.db.DatabaseAnalyticsModule;
import com.comet.opik.infrastructure.db.IdGeneratorModule;
import com.comet.opik.infrastructure.db.NameGeneratorModule;
Expand Down Expand Up @@ -73,7 +74,7 @@ public void initialize(Bootstrap<OpikConfiguration> bootstrap) {
.withPlugins(new SqlObjectPlugin(), new Jackson2Plugin()))
.modules(new DatabaseAnalyticsModule(), new IdGeneratorModule(), new AuthModule(), new RedisModule(),
new RateLimitModule(), new NameGeneratorModule(), new HttpModule(), new EventModule(),
new ConfigurationModule(), new BiModule(), new LlmProviderClientModule())
new ConfigurationModule(), new BiModule(), new CacheModule(), new LlmProviderClientModule())
.installers(JobGuiceyInstaller.class)
.listen(new OpikGuiceyLifecycleEventListener())
.enableAutoConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public sealed interface AutomationRuleEvaluatorModel<T> extends AutomationRuleMo

@Json
T code();

AutomationRuleEvaluatorType type();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class AutomationRuleEvaluatorServiceImpl implements AutomationRuleEvaluatorServi
private final @NonNull TransactionTemplate template;

@Override
public <E, T extends AutomationRuleEvaluator<E>> T save(T inputRuleEvaluator,
public <E, T extends AutomationRuleEvaluator<E>> T save(@NonNull T inputRuleEvaluator,
@NonNull UUID projectId,
@NonNull String workspaceId,
@NonNull String userName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,12 @@ class LlmProviderApiKeyServiceImpl implements LlmProviderApiKeyService {
@Override
public ProviderApiKey find(@NonNull UUID id, @NonNull String workspaceId) {

ProviderApiKey providerApiKey = template.inTransaction(READ_ONLY, handle -> {
return template.inTransaction(READ_ONLY, handle -> {

var repository = handle.attach(LlmProviderApiKeyDAO.class);

return repository.fetch(id, workspaceId).orElseThrow(this::createNotFoundError);
});

return providerApiKey;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.comet.opik.infrastructure;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

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

@Data
public class CacheConfiguration {

@Valid @JsonProperty
private boolean enabled = false;

@Valid @JsonProperty
@NotNull private Duration defaultDuration;

@Valid @JsonProperty
private Map<String, Duration> caches;

public Map<String, Duration> getCaches() {
return Optional.ofNullable(caches).orElse(Map.of());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ public class OpikConfiguration extends JobConfiguration {

@Valid @NotNull @JsonProperty
private LlmProviderClientConfig llmProviderClient = new LlmProviderClientConfig();

@Valid @NotNull @JsonProperty
private CacheConfiguration cacheManager = new CacheConfiguration();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.comet.opik.infrastructure.cache;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface CacheEvict {

/**
* @return the name of the cache group.
* */
String name();

/**
* key is a SpEL expression implemented using MVEL. Please refer to the <a href="http://mvel.documentnode.com/">MVEL documentation for more information</a>.
*
* @return SpEL expression evaluated to generate the cache key.
* */
String key();
}
Loading

0 comments on commit 272dfb8

Please sign in to comment.