Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: deprecate AbstractCache that relies upon synchronization #315

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.xanthic.cache.core;

import io.github.xanthic.cache.api.Cache;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -24,7 +25,10 @@
*
* @param <K> The type of keys that form the cache
* @param <V> The type of values contained in the cache
* @deprecated no longer used by Xanthic; synchronization generally should be avoided
*/
@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "1.0.0")
public abstract class AbstractCache<K, V> implements Cache<K, V> {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.xanthic.cache.core;

import io.github.xanthic.cache.api.Cache;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -30,6 +31,7 @@
* @deprecated no longer used by Xanthic to implement any canonical cache provider
*/
@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "1.0.0")
public abstract class LockedAbstractCache<K, V> implements Cache<K, V> {

protected final ReadWriteLock lock = new ReentrantReadWriteLock();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package io.github.xanthic.cache.provider.androidx;

import io.github.xanthic.cache.api.Cache;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;

abstract class AbstractCache<K, V> implements Cache<K, V> {

@Override
public V computeIfAbsent(@NotNull K key, @NotNull Function<K, V> computeFunc) {
synchronized (getLock()) {
V old = this.get(key);
if (old != null) return old;
V computed = computeFunc.apply(key);
this.put(key, computed);
return computed;
}
}

@Nullable
@Override
public V compute(@NotNull K key, @NotNull BiFunction<? super K, ? super V, ? extends V> computeFunc) {
synchronized (getLock()) {
V oldValue = this.get(key);
V newValue = computeFunc.apply(key, oldValue);
if (newValue != null) {
this.put(key, newValue);
return newValue;
} else if (oldValue != null) {
this.remove(key);
}
}
return null;
}

@Override
public V computeIfPresent(@NotNull K key, @NotNull BiFunction<? super K, ? super V, ? extends V> computeFunc) {
synchronized (getLock()) {
V oldValue = this.get(key);
if (oldValue != null) {
V newValue = computeFunc.apply(key, oldValue);
if (newValue != null) {
this.put(key, newValue);
return newValue;
} else {
this.remove(key);
}
}
}
return null;
}

@Override
public V putIfAbsent(@NotNull K key, @NotNull V value) {
synchronized (getLock()) {
V old = this.get(key);
if (old == null) {
this.put(key, value);
}
return old;
}
}

@Override
public V merge(@NotNull K key, @NotNull V value, @NotNull BiFunction<V, V, V> mergeFunc) {
synchronized (getLock()) {
V old = putIfAbsent(key, value);
if (old == null) return value;
V merged = mergeFunc.apply(old, value);
this.put(key, merged);
return merged;
}
}

@Override
public boolean replace(@NotNull K key, @NotNull V value) {
synchronized (getLock()) {
V old = this.get(key);
if (old == null) return false;
put(key, value);
return true;
}
}

@Override
public boolean replace(@NotNull K key, @NotNull V oldValue, @NotNull V newValue) {
// noinspection ConstantConditions
if (oldValue == null) return false;
synchronized (getLock()) {
if (Objects.equals(oldValue, this.get(key))) {
this.put(key, newValue);
return true;
}
}
return false;
}

@NotNull
protected Object getLock() {
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.github.xanthic.cache.api.RemovalListener;
import io.github.xanthic.cache.api.domain.ExpiryType;
import io.github.xanthic.cache.api.domain.RemovalCause;
import io.github.xanthic.cache.core.AbstractCache;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.xanthic.cache.provider.androidx;

import androidx.collection.LruCache;
import io.github.xanthic.cache.core.AbstractCache;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand Down