-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9abaa77
commit a5c79f2
Showing
8 changed files
with
157 additions
and
109 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
.../com/giffing/bucket4j/spring/boot/starter/config/cache/AbstractCacheResolverTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.giffing.bucket4j.spring.boot.starter.config.cache; | ||
|
||
import com.giffing.bucket4j.spring.boot.starter.context.ConsumptionProbeHolder; | ||
import io.github.bucket4j.Bucket; | ||
import io.github.bucket4j.distributed.AsyncBucketProxy; | ||
import io.github.bucket4j.distributed.proxy.AbstractProxyManager; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
public abstract class AbstractCacheResolverTemplate<T> { | ||
|
||
public ProxyManagerWrapper resolve(String cacheName) { | ||
AbstractProxyManager<T> proxyManager = getProxyManager(cacheName); | ||
return ((key, numTokens, bucketConfiguration, metricsListener, version, replaceStrategy) -> { | ||
if(isAsync()) { | ||
AsyncBucketProxy bucket = proxyManager.asAsync() | ||
.builder() | ||
.withImplicitConfigurationReplacement(version, replaceStrategy) | ||
.build(castStringToCacheKey(key), () -> CompletableFuture.completedFuture(bucketConfiguration)) | ||
.toListenable(metricsListener); | ||
return new ConsumptionProbeHolder(bucket.tryConsumeAndReturnRemaining(numTokens)); | ||
} else { | ||
Bucket bucket = proxyManager | ||
.builder() | ||
.withImplicitConfigurationReplacement(version, replaceStrategy) | ||
.build(castStringToCacheKey(key), () -> bucketConfiguration) | ||
.toListenable(metricsListener); | ||
return new ConsumptionProbeHolder(bucket.tryConsumeAndReturnRemaining(numTokens)); | ||
} | ||
}); | ||
} | ||
|
||
public abstract T castStringToCacheKey(String key); | ||
|
||
public abstract boolean isAsync(); | ||
|
||
public abstract AbstractProxyManager<T> getProxyManager(String cacheName); | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 16 additions & 14 deletions
30
...ava/com/giffing/bucket4j/spring/boot/starter/config/cache/ignite/IgniteCacheResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,32 @@ | ||
package com.giffing.bucket4j.spring.boot.starter.config.cache.ignite; | ||
|
||
import com.giffing.bucket4j.spring.boot.starter.config.cache.AbstractCacheResolverTemplate; | ||
import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver; | ||
import com.giffing.bucket4j.spring.boot.starter.config.cache.ProxyManagerWrapper; | ||
import com.giffing.bucket4j.spring.boot.starter.context.ConsumptionProbeHolder; | ||
|
||
import io.github.bucket4j.distributed.AsyncBucketProxy; | ||
import io.github.bucket4j.distributed.proxy.AbstractProxyManager; | ||
import io.github.bucket4j.grid.ignite.thick.IgniteProxyManager; | ||
import org.apache.ignite.Ignite; | ||
|
||
public class IgniteCacheResolver implements AsyncCacheResolver { | ||
public class IgniteCacheResolver extends AbstractCacheResolverTemplate<String> implements AsyncCacheResolver { | ||
|
||
private final Ignite ignite; | ||
|
||
public IgniteCacheResolver(Ignite ignite) { | ||
this.ignite = ignite; | ||
} | ||
|
||
|
||
@Override | ||
public String castStringToCacheKey(String key) { | ||
return key; | ||
} | ||
|
||
@Override | ||
public boolean isAsync() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public ProxyManagerWrapper resolve(String cacheName) { | ||
public AbstractProxyManager<String> getProxyManager(String cacheName) { | ||
org.apache.ignite.IgniteCache<String, byte[]> cache = ignite.cache(cacheName); | ||
IgniteProxyManager<String> igniteProxyManager = new IgniteProxyManager<>(cache); | ||
return (key, numTokens, bucketConfiguration, metricsListener, version, replaceStrategy) -> { | ||
AsyncBucketProxy bucket = igniteProxyManager.asAsync().builder() | ||
.withImplicitConfigurationReplacement(version, replaceStrategy) | ||
.build(key, bucketConfiguration).toListenable(metricsListener); | ||
return new ConsumptionProbeHolder(bucket.tryConsumeAndReturnRemaining(numTokens)); | ||
}; | ||
return new IgniteProxyManager<>(cache); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 18 additions & 17 deletions
35
...com/giffing/bucket4j/spring/boot/starter/config/cache/redis/jedis/JedisCacheResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,43 @@ | ||
package com.giffing.bucket4j.spring.boot.starter.config.cache.redis.jedis; | ||
|
||
import com.giffing.bucket4j.spring.boot.starter.config.cache.AbstractCacheResolverTemplate; | ||
import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheResolver; | ||
import com.giffing.bucket4j.spring.boot.starter.config.cache.ProxyManagerWrapper; | ||
import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver; | ||
import com.giffing.bucket4j.spring.boot.starter.context.ConsumptionProbeHolder; | ||
import io.github.bucket4j.Bucket; | ||
import io.github.bucket4j.distributed.ExpirationAfterWriteStrategy; | ||
import io.github.bucket4j.distributed.proxy.ProxyManager; | ||
import io.github.bucket4j.distributed.proxy.AbstractProxyManager; | ||
import io.github.bucket4j.redis.jedis.cas.JedisBasedProxyManager; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import redis.clients.jedis.JedisPool; | ||
|
||
import java.time.Duration; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
/** | ||
* This class is the Redis implementation of the {@link CacheResolver}. | ||
* | ||
*/ | ||
public class JedisCacheResolver implements SyncCacheResolver { | ||
public class JedisCacheResolver extends AbstractCacheResolverTemplate<byte[]> implements SyncCacheResolver { | ||
|
||
private final JedisPool pool; | ||
|
||
public JedisCacheResolver(JedisPool pool) { | ||
this.pool = pool; | ||
} | ||
|
||
@Override | ||
public ProxyManagerWrapper resolve(String cacheName) { | ||
final ProxyManager<byte[]> proxyManager = JedisBasedProxyManager.builderFor(pool) | ||
@Override | ||
public boolean isAsync() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public byte[] castStringToCacheKey(String key) { | ||
return key.getBytes(UTF_8); | ||
} | ||
|
||
@Override | ||
public AbstractProxyManager<byte[]> getProxyManager(String cacheName) { | ||
return JedisBasedProxyManager.builderFor(pool) | ||
.withExpirationStrategy(ExpirationAfterWriteStrategy.basedOnTimeForRefillingBucketUpToMax(Duration.ofSeconds(10))) | ||
.build(); | ||
|
||
return (key, numTokens, bucketConfiguration, metricsListener, version, replaceStrategy) -> { | ||
Bucket bucket = proxyManager.builder() | ||
.withImplicitConfigurationReplacement(version, replaceStrategy) | ||
.build(key.getBytes(UTF_8), bucketConfiguration).toListenable(metricsListener); | ||
return new ConsumptionProbeHolder(bucket.tryConsumeAndReturnRemaining(numTokens)); | ||
}; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.