Skip to content

Commit

Permalink
1.1.2 (#7)
Browse files Browse the repository at this point in the history
* Caffeine & Redis cache CompositeCache examples added
* OpenAPI HTTP Client securitySchema example added
  • Loading branch information
GoodforGod authored Jun 7, 2024
1 parent 8f0eb73 commit 62e3f2f
Show file tree
Hide file tree
Showing 41 changed files with 452 additions and 41 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
groupId=ru.tinkoff.kora
koraVersion=1.1.1
koraVersion=1.1.2


##### GRADLE #####
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.tinkoff.kora.example.cache.caffeine;

import ru.tinkoff.kora.cache.annotation.Cache;
import ru.tinkoff.kora.cache.caffeine.CaffeineCache;

@Cache("my-cache")
public interface CompositeCache extends CaffeineCache<CompositeCache.Key, Long> {

record Key(String userId, String traceId) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ru.tinkoff.kora.example.cache.caffeine;

import jakarta.annotation.Nonnull;
import ru.tinkoff.kora.cache.CacheKeyMapper;
import ru.tinkoff.kora.cache.annotation.CacheInvalidate;
import ru.tinkoff.kora.cache.annotation.CachePut;
import ru.tinkoff.kora.cache.annotation.Cacheable;
import ru.tinkoff.kora.common.Component;
import ru.tinkoff.kora.common.Mapping;
import ru.tinkoff.kora.common.annotation.Root;

import java.math.BigDecimal;
import java.util.concurrent.ThreadLocalRandom;

@Root
@Component
public class CompositeService {

public record UserContext(String userId, String traceId) {}

public static final class UserContextMapping implements CacheKeyMapper<CompositeCache.Key, UserContext> {

@Nonnull
@Override
public CompositeCache.Key map(UserContext arg) {
return new CompositeCache.Key(arg.userId(), arg.traceId());
}
}

@Mapping(UserContextMapping.class)
@Cacheable(CompositeCache.class)
public Long getMapping(UserContext context) {
return ThreadLocalRandom.current().nextLong(0, 100_000_000L);
}

@Cacheable(CompositeCache.class)
public Long get(String id, String traceId) {
return ThreadLocalRandom.current().nextLong(0, 100_000_000L);
}

@CachePut(value = CompositeCache.class, parameters = { "id", "traceId" })
public Long put(BigDecimal arg2, String arg3, String id, String traceId) {
return ThreadLocalRandom.current().nextLong(0, 100_000_000L);
}

@CacheInvalidate(CompositeCache.class)
public void delete(String id, String traceId) {}

@CacheInvalidate(value = CompositeCache.class, invalidateAll = true)
public void deleteAll() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
import ru.tinkoff.kora.cache.caffeine.CaffeineCache;

@Cache("my-cache")
public interface MyCache extends CaffeineCache<String, Long> {
public interface SimpleCache extends CaffeineCache<String, Long> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@Root
@Component
public class CachedService {
public class SimpleService {

public record UserContext(String userId, String traceId) {}

Expand All @@ -27,24 +27,24 @@ public String map(UserContext arg) {
}

@Mapping(UserContextMapping.class)
@Cacheable(MyCache.class)
@Cacheable(SimpleCache.class)
public Long getMapping(UserContext context) {
return ThreadLocalRandom.current().nextLong(0, 100_000_000L);
}

@Cacheable(MyCache.class)
@Cacheable(SimpleCache.class)
public Long get(String id) {
return ThreadLocalRandom.current().nextLong(0, 100_000_000L);
}

@CachePut(value = MyCache.class, parameters = { "id" })
@CachePut(value = SimpleCache.class, parameters = { "id" })
public Long put(BigDecimal arg2, String arg3, String id) {
return ThreadLocalRandom.current().nextLong(0, 100_000_000L);
}

@CacheInvalidate(MyCache.class)
@CacheInvalidate(SimpleCache.class)
public void delete(String id) {}

@CacheInvalidate(value = MyCache.class, invalidateAll = true)
@CacheInvalidate(value = SimpleCache.class, invalidateAll = true)
public void deleteAll() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package ru.tinkoff.kora.example.cache.caffeine;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import ru.tinkoff.kora.test.extension.junit5.KoraAppTest;
import ru.tinkoff.kora.test.extension.junit5.TestComponent;

import java.math.BigDecimal;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

@KoraAppTest(Application.class)
class CaffeineCompositeServiceTests {

@TestComponent
private CompositeService service;
@TestComponent
private CompositeCache cache;

@BeforeEach
void cleanup() {
cache.invalidateAll();
}

@Test
void get() {
// when
var origin = service.get("1", "1");

// then
var cached = service.get("1", "1");
assertEquals(origin, cached);
}

@Test
void getMapping() {
// given
var context = new CompositeService.UserContext("1", "2");

// when
var origin = service.getMapping(context);

// then
var cached = service.getMapping(context);
assertEquals(origin, cached);
}

@Test
void put() {
// when
var origin = service.put(BigDecimal.ONE, "12345", "1", "1");

// then
var cached = service.get("1", "1");
assertEquals(origin, cached);
}

@Test
void delete() {
// given
var origin = service.put(BigDecimal.ONE, "12345", "1", "1");
var cached1 = service.get("1", "1");
assertEquals(origin, cached1);

// when
service.delete("1", "1");

// then
var newValue = service.get("1", "1");
assertNotEquals(origin, newValue);
}

@Test
void deleteAll() {
// given
var origin1 = service.put(BigDecimal.ONE, "12345", "1", "1");
var cached1 = service.get("1", "1");
assertEquals(origin1, cached1);

var origin2 = service.put(BigDecimal.ONE, "12345", "2", "2");
var cached2 = service.get("2", "2");
assertEquals(origin2, cached2);

// when
service.deleteAll();

// then
var newValue1 = service.get("1", "1");
assertNotEquals(origin1, newValue1);

var newValue2 = service.get("2", "2");
assertNotEquals(origin2, newValue2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import ru.tinkoff.kora.test.extension.junit5.TestComponent;

@KoraAppTest(Application.class)
class CaffeineCachedServiceTests {
class CaffeineSimpleServiceTests {

@TestComponent
private CachedService service;
private SimpleService service;
@TestComponent
private MyCache cache;
private SimpleCache cache;

@BeforeEach
void cleanup() {
Expand All @@ -35,7 +35,7 @@ void get() {
@Test
void getMapping() {
// given
var context = new CachedService.UserContext("1", "2");
var context = new SimpleService.UserContext("1", "2");

// when
var origin = service.getMapping(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.tinkoff.kora.example.cache.caffeine;
package ru.tinkoff.kora.example.cache.redis;

import ru.tinkoff.kora.application.graph.KoraApplication;
import ru.tinkoff.kora.cache.redis.RedisCacheModule;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.tinkoff.kora.example.cache.redis;

import ru.tinkoff.kora.cache.annotation.Cache;
import ru.tinkoff.kora.cache.redis.RedisCache;

@Cache("my-cache")
public interface CompositeCache extends RedisCache<CompositeCache.Key, Long> {

record Key(String userId, String traceId) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ru.tinkoff.kora.example.cache.redis;

import jakarta.annotation.Nonnull;
import ru.tinkoff.kora.cache.CacheKeyMapper;
import ru.tinkoff.kora.cache.annotation.CacheInvalidate;
import ru.tinkoff.kora.cache.annotation.CachePut;
import ru.tinkoff.kora.cache.annotation.Cacheable;
import ru.tinkoff.kora.common.Component;
import ru.tinkoff.kora.common.Mapping;
import ru.tinkoff.kora.common.annotation.Root;

import java.math.BigDecimal;
import java.util.concurrent.ThreadLocalRandom;

@Root
@Component
public class CompositeService {

public record UserContext(String userId, String traceId) {}

public static final class UserContextMapping implements CacheKeyMapper<CompositeCache.Key, UserContext> {

@Nonnull
@Override
public CompositeCache.Key map(UserContext arg) {
return new CompositeCache.Key(arg.userId(), arg.traceId());
}
}

@Mapping(UserContextMapping.class)
@Cacheable(CompositeCache.class)
public Long getMapping(UserContext context) {
return ThreadLocalRandom.current().nextLong(0, 100_000_000L);
}

@Cacheable(CompositeCache.class)
public Long get(String id, String traceId) {
return ThreadLocalRandom.current().nextLong(0, 100_000_000L);
}

@CachePut(value = CompositeCache.class, parameters = { "id", "traceId" })
public Long put(BigDecimal arg2, String arg3, String id, String traceId) {
return ThreadLocalRandom.current().nextLong(0, 100_000_000L);
}

@CacheInvalidate(CompositeCache.class)
public void delete(String id, String traceId) {}

@CacheInvalidate(value = CompositeCache.class, invalidateAll = true)
public void deleteAll() {}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package ru.tinkoff.kora.example.cache.caffeine;
package ru.tinkoff.kora.example.cache.redis;

import ru.tinkoff.kora.cache.annotation.Cache;
import ru.tinkoff.kora.cache.redis.RedisCache;

@Cache("my-cache")
public interface MyCache extends RedisCache<String, Long> {
public interface SimpleCache extends RedisCache<String, Long> {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.tinkoff.kora.example.cache.caffeine;
package ru.tinkoff.kora.example.cache.redis;

import java.math.BigDecimal;
import java.util.concurrent.ThreadLocalRandom;
Expand All @@ -10,21 +10,21 @@

@Root
@Component
public class CachedService {
public class SimpleService {

@Cacheable(MyCache.class)
@Cacheable(SimpleCache.class)
public Long get(String id) {
return ThreadLocalRandom.current().nextLong(0, 100_000_000L);
}

@CachePut(value = MyCache.class, parameters = { "id" })
@CachePut(value = SimpleCache.class, parameters = { "id" })
public Long put(BigDecimal arg2, String arg3, String id) {
return ThreadLocalRandom.current().nextLong(0, 100_000_000L);
}

@CacheInvalidate(MyCache.class)
@CacheInvalidate(SimpleCache.class)
public void delete(String id) {}

@CacheInvalidate(value = MyCache.class, invalidateAll = true)
@CacheInvalidate(value = SimpleCache.class, invalidateAll = true)
public void deleteAll() {}
}
Loading

0 comments on commit 62e3f2f

Please sign in to comment.