|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.pulsar.reactive.client.producercache; |
| 21 | + |
| 22 | +import java.time.Duration; |
| 23 | +import java.util.concurrent.CompletableFuture; |
| 24 | +import java.util.function.Function; |
| 25 | + |
| 26 | +import com.github.benmanes.caffeine.cache.AsyncCache; |
| 27 | +import com.github.benmanes.caffeine.cache.Caffeine; |
| 28 | +import com.github.benmanes.caffeine.cache.RemovalCause; |
| 29 | +import com.github.benmanes.caffeine.cache.Scheduler; |
| 30 | +import org.apache.pulsar.reactive.client.adapter.ProducerCacheProvider; |
| 31 | +import reactor.core.scheduler.Schedulers; |
| 32 | + |
| 33 | +/** |
| 34 | + * Producer cache provider that uses a shaded Caffeine {@link AsyncCache} to cache |
| 35 | + * entries. |
| 36 | + */ |
| 37 | +public class CaffeineShadedProducerCacheProvider implements ProducerCacheProvider { |
| 38 | + |
| 39 | + private final AsyncCache<Object, Object> cache; |
| 40 | + |
| 41 | + /** |
| 42 | + * Create a cache provider instance with default values. |
| 43 | + */ |
| 44 | + public CaffeineShadedProducerCacheProvider() { |
| 45 | + this(Duration.ofMinutes(1), Duration.ofMinutes(10), 1000L, 50); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Create a cache provider instance with the specified options. |
| 50 | + * @param cacheExpireAfterAccess time period after last access to expire unused |
| 51 | + * entries in the cache |
| 52 | + * @param cacheExpireAfterWrite time period after last write to expire unused entries |
| 53 | + * in the cache |
| 54 | + * @param cacheMaximumSize maximum size of cache (entries) |
| 55 | + * @param cacheInitialCapacity the initial size of cache |
| 56 | + */ |
| 57 | + public CaffeineShadedProducerCacheProvider(Duration cacheExpireAfterAccess, Duration cacheExpireAfterWrite, |
| 58 | + Long cacheMaximumSize, Integer cacheInitialCapacity) { |
| 59 | + this.cache = Caffeine.newBuilder().expireAfterAccess(cacheExpireAfterAccess) |
| 60 | + .expireAfterWrite(cacheExpireAfterWrite).maximumSize(cacheMaximumSize) |
| 61 | + .initialCapacity(cacheInitialCapacity).scheduler(Scheduler.systemScheduler()) |
| 62 | + .executor(Schedulers.boundedElastic()::schedule).removalListener(this::onRemoval).buildAsync(); |
| 63 | + } |
| 64 | + |
| 65 | + private void onRemoval(Object key, Object entry, RemovalCause cause) { |
| 66 | + if (entry instanceof AutoCloseable) { |
| 67 | + try { |
| 68 | + ((AutoCloseable) entry).close(); |
| 69 | + } |
| 70 | + catch (Exception ex) { |
| 71 | + throw new RuntimeException(ex); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public void close() { |
| 77 | + this.cache.synchronous().invalidateAll(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public <K, V> CompletableFuture<V> getOrCreateCachedEntry(K key, |
| 82 | + Function<K, CompletableFuture<V>> createEntryFunction) { |
| 83 | + return (CompletableFuture<V>) this.cache.get(key, |
| 84 | + (__, ___) -> (CompletableFuture) createEntryFunction.apply(key)); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments