Skip to content

Commit

Permalink
#461 implement flexible per entry expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Buhtoyarov committed Apr 28, 2024
1 parent 7e360e7 commit 17991fb
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -77,13 +77,13 @@ Its key advantage lies in the configuration via properties or yaml files, elimin
### Supported JCache compatible(or similar) back-ends
In addition to local in-memory buckets, the Bucket4j supports clustered usage scenario on top of following back-ends:

| Back-end | Async supported | Flexible Per-entry expiration | Optimized serialization | Thin-client support | Documentation link |
| Back-end | Async supported | Flexible per-entry expiration | Optimized serialization | Thin-client support | Documentation link |
| :--- | :---: |:-----------------------------:|:-----------------------:|:-------------------:|:-------------------------------------------------------------------------------------:|
| ```JCache API (JSR 107)``` | No | No | No | No | [bucket4j-jcache](https://bucket4j.github.io/8.12.0/toc.html#bucket4j-jcache) |
| ```Hazelcast``` | Yes | Yes | Yes | No | [bucket4j-hazelcast](https://bucket4j.github.io/8.12.0/toc.html#bucket4j-hazelcast) |
| ```Apache Ignite``` | Yes | No | n/a | Yes | [bucket4j-ignite](https://bucket4j.github.io/8.12.0/toc.html#bucket4j-ignite) |
| ```Inifinispan``` | Yes | TODO | Yes | No | [bucket4j-infinispan](https://bucket4j.github.io/8.12.0/toc.html#bucket4j-infinispan) |
| ```Oracle Coherence``` | Yes | TODO | Yes | No | [bucket4j-coherence](https://bucket4j.github.io/8.12.0/toc.html#bucket4j-coherence) |
| ```Oracle Coherence``` | Yes | Yes | Yes | No | [bucket4j-coherence](https://bucket4j.github.io/8.12.0/toc.html#bucket4j-coherence) |

### Redis back-ends
| Back-end | Async supported | Redis cluster supported | Documentation link |
Expand Down
Expand Up @@ -55,6 +55,11 @@ public CoherenceProxyManager<K> build() {
return new CoherenceProxyManager<>(this);
}

@Override
public boolean isExpireAfterWriteSupported() {
return true;
}

}

}
Expand Up @@ -19,8 +19,11 @@
*/
package io.github.bucket4j.grid.coherence;

import com.tangosol.util.BinaryEntry;
import com.tangosol.util.InvocableMap;
import com.tangosol.util.processor.AbstractProcessor;

import io.github.bucket4j.distributed.ExpirationAfterWriteStrategy;
import io.github.bucket4j.distributed.remote.AbstractBinaryTransaction;
import io.github.bucket4j.distributed.remote.RemoteBucketState;
import io.github.bucket4j.distributed.remote.Request;
Expand All @@ -29,6 +32,7 @@

import java.io.Serial;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;


public class CoherenceProcessor<K, T> extends AbstractProcessor<K, byte[], byte[]> implements ComparableByContent {
Expand Down Expand Up @@ -65,7 +69,16 @@ protected byte[] getRawState() {

@Override
protected void setRawState(byte[] newStateBytes, RemoteBucketState newState) {
entry.setValue(newStateBytes);
ExpirationAfterWriteStrategy expirationStrategy = getExpirationStrategy();
long ttlMillis = expirationStrategy == null ? -1 : expirationStrategy.calculateTimeToLiveMillis(newState, getCurrentTimeNanos());
if (ttlMillis > 0) {
BinaryEntry<K, byte[]> binaryEntry = entry.asBinaryEntry();
binaryEntry.setValue(newStateBytes);
binaryEntry.expire(ttlMillis);
entry.setValue(newStateBytes);
} else {
entry.setValue(newStateBytes);
}
}
}.execute();
}
Expand Down
Expand Up @@ -124,4 +124,9 @@ protected CompletableFuture<Void> removeAsync(K key) {
return cache.async().remove(key).thenApply(oldState -> null);
}

@Override
public boolean isExpireAfterWriteSupported() {
return true;
}

}
Expand Up @@ -42,7 +42,7 @@ public static void prepareCache() throws InterruptedException {
"CoherenceProxyManager_JdkSerialization",
() -> UUID.randomUUID().toString(),
() -> Bucket4jCoherence.entryProcessorBasedBuilder(cache)
)
).checkExpiration()
);
}

Expand Down
Expand Up @@ -40,7 +40,7 @@ public static void prepareCache() throws InterruptedException {
"CoherenceProxyManager_PofSerialization",
() -> UUID.randomUUID().toString(),
() -> Bucket4jCoherence.entryProcessorBasedBuilder(cache)
)
).checkExpiration()
);
}

Expand Down

0 comments on commit 17991fb

Please sign in to comment.