-
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.
* init redis support * fix readme
- Loading branch information
Showing
7 changed files
with
160 additions
and
7 deletions.
There are no files selected for viewing
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
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
19 changes: 19 additions & 0 deletions
19
...m/giffing/bucket4j/spring/boot/starter/config/cache/redis/RedisBucket4jConfiguration.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,19 @@ | ||
package com.giffing.bucket4j.spring.boot.starter.config.cache.redis; | ||
|
||
import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
|
||
@Configuration | ||
@ConditionalOnClass(RedisTemplate.class) | ||
@ConditionalOnBean(RedisTemplate.class) | ||
public class RedisBucket4jConfiguration { | ||
|
||
@Bean | ||
public SyncCacheResolver bucket4RedisResolver(RedisTemplate redisTemplate) { | ||
return new RedisCacheResolver(redisTemplate); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
.../java/com/giffing/bucket4j/spring/boot/starter/config/cache/redis/RedisCacheResolver.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,23 @@ | ||
package com.giffing.bucket4j.spring.boot.starter.config.cache.redis; | ||
|
||
import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheResolver; | ||
import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver; | ||
import io.github.bucket4j.distributed.proxy.ProxyManager; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
|
||
/** | ||
* This class is the Redis implementation of the {@link CacheResolver}. | ||
* | ||
*/ | ||
public class RedisCacheResolver implements SyncCacheResolver { | ||
|
||
private RedisTemplate<String, byte[]> redisTemplate; | ||
|
||
public RedisCacheResolver(RedisTemplate<String, byte[]> redisTemplate) { | ||
this.redisTemplate = redisTemplate; | ||
} | ||
|
||
public ProxyManager<String> resolve(String cacheName) { | ||
return new RedisProxyManager(redisTemplate, cacheName); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...n/java/com/giffing/bucket4j/spring/boot/starter/config/cache/redis/RedisProxyManager.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,98 @@ | ||
package com.giffing.bucket4j.spring.boot.starter.config.cache.redis; | ||
|
||
import io.github.bucket4j.distributed.proxy.AbstractProxyManager; | ||
import io.github.bucket4j.distributed.proxy.ClientSideConfig; | ||
import io.github.bucket4j.distributed.remote.AbstractBinaryTransaction; | ||
import io.github.bucket4j.distributed.remote.CommandResult; | ||
import io.github.bucket4j.distributed.remote.Request; | ||
import io.github.bucket4j.distributed.serialization.InternalSerializationHelper; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* The extension of Bucket4j library addressed to support Redis. | ||
*/ | ||
class RedisProxyManager extends AbstractProxyManager<String> { | ||
|
||
private final RedisTemplate<String, byte[]> redisTemplate; | ||
private final String cacheName; | ||
|
||
protected RedisProxyManager(RedisTemplate<String, byte[]> redisTemplate, String cacheName) { | ||
super(ClientSideConfig.getDefault()); | ||
this.redisTemplate = redisTemplate; | ||
this.cacheName = cacheName; | ||
} | ||
|
||
@Override | ||
public <T> CommandResult<T> execute(String key, Request<T> request) { | ||
byte[] resultBytes = new BucketProcessor<T>(redisTemplate).process(buildRedisKey(key), request); | ||
return InternalSerializationHelper.deserializeResult(resultBytes, request.getBackwardCompatibilityVersion()); | ||
} | ||
|
||
@Override | ||
public void removeProxy(String key) { | ||
redisTemplate.delete(buildRedisKey(key)); | ||
} | ||
|
||
@Override | ||
public boolean isAsyncModeSupported() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public <T> CompletableFuture<CommandResult<T>> executeAsync(String key, Request<T> request) { | ||
// not supported yet | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
protected CompletableFuture<Void> removeAsync(String key) { | ||
// not supported yet | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
private String buildRedisKey(String key) { | ||
return cacheName + "." + key; | ||
} | ||
|
||
private static class BucketProcessor<T> { | ||
|
||
private final RedisTemplate<String, byte[]> redisTemplate; | ||
|
||
public BucketProcessor(RedisTemplate<String, byte[]> redisTemplate) { | ||
this.redisTemplate = redisTemplate; | ||
} | ||
|
||
public byte[] process(String key, Request<T> request) { | ||
return new RedisTransaction(redisTemplate, key, InternalSerializationHelper.serializeRequest(request)).execute(); | ||
} | ||
} | ||
|
||
private static class RedisTransaction extends AbstractBinaryTransaction { | ||
|
||
private final RedisTemplate<String, byte[]> redisTemplate; | ||
private final String key; | ||
|
||
private RedisTransaction(RedisTemplate<String, byte[]> redisTemplate, String key, byte[] requestBytes) { | ||
super(requestBytes); | ||
this.redisTemplate = redisTemplate; | ||
this.key = key; | ||
} | ||
|
||
@Override | ||
public boolean exists() { | ||
return redisTemplate.hasKey(key); | ||
} | ||
|
||
@Override | ||
protected byte[] getRawState() { | ||
return redisTemplate.opsForValue().get(key); | ||
} | ||
|
||
@Override | ||
protected void setRawState(byte[] stateBytes) { | ||
redisTemplate.opsForValue().set(key, stateBytes); | ||
} | ||
} | ||
} |
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